Reputation: 11
I'm trying to set up a program that prompts the user to enter a math equation only containing addition and place it in parentheses. My code is meant to search for these equations and give back the sum of the equation.
The part I am having trouble with is when I try to split the addition signs from the code, and parse it so I can turn it into a int. But when I try to split, I get an error that says cannot convert String[] to String
.
Here is the coding I have thus far:
String userinput = in.nextLine();
int parentheses;
int parenthesesclose, parse;
String usersubstring;
String split;
while (parentheses >= 0) {
parentheses = userinput.indexOf("(");
parenthesesclose = userinput.indexOf(")");
usersubstring = userinput.substring(parentheses + 1, parenthesesclose);
split = usersubstring.split(+);
split.trim();
if (split.isdigit) {
parse = Interger.parseInt(split);
}
}
Upvotes: 0
Views: 4248
Reputation: 2113
You should declare variable split as String[]. split() will return you an array of Strings.
String userinput=in.nextLine();
int parentheses;
int parenthesesclose, parse;
String usersubstring;
String[] split;
while ( parentheses >= 0){
parentheses = userinput.indexOf("(");
parenthesesclose = userinput.indexOf(")");
usersubstring = userinput.substring( parentheses + 1, parenthesesclose);
split = usersubstring.split("+");
}
Upvotes: 2
Reputation: 670
Method split returns an string array, so you should change the type of your split variable to an array.
Also, the multiply symbol not in brakets.
Is this declaration of variables local? If yes, you should define them, otherwise there are possible errors in the heap.
Upvotes: 0
Reputation: 359936
Exactly as the error message tells you, String#split()
returns a String[]
, which is a string array. Change your declaration to this:
String[] split;
Upvotes: 3