java - How to properly call a String converted to a Double method? -
i have created method converts string value new double. want create if statement tests whether said method returns null suppose do. code far:
content class
public class content { double x; string string = "b"; public void testparsing() { if (//call xmethod == null) { system.out.println("ovalid operator success");} else { system.out.println("invalid operator fail"); } } /* * chops input on ' ' decides whether add or multiply. * if string not contain valid format returns null. */ public double x(string x) { string[] parsed; if (x.contains("*")) { // * special character in regex parsed = x.split("\\*"); return double.parsedouble(parsed[0]) * double.parsedouble(parsed[1]); } else if (x.contains("+")) { // + again special character in regex parsed = x.split("\\+"); return double.parsedouble(parsed[0]) + double.parsedouble(parsed[1]); } return null; } } main class
public class mainclass { public static void main(string[] args) { content call = new content(); call.testparsing(); } } i know following line compiles , outputs success: (line 9)
if (x("") == null) { but don't think doing asking do, asking check if outcome of method in x pointing returns null or not. clarification on how call method check condition appreciated, thanks.
but don't think doing asking do
you checking if result null. logic correct.
you may want store result if intend on using later, though.
double val = x(""); if (val == null) { // invalid } else { system.out.println("valid! result: " + val); }
Comments
Post a Comment