0

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);
    }
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
Mike Hirt
  • 11
  • 1
  • 2

3 Answers3

3

Exactly as the error message tells you, String#split() returns a String[], which is a string array. Change your declaration to this:

String[] split;
Makoto
  • 104,088
  • 27
  • 192
  • 230
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
2

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("+");
}
David Duponchel
  • 3,959
  • 3
  • 28
  • 36
naresh
  • 2,113
  • 20
  • 32
  • okay thaat makes sense, i guess I could create a for loop that checks all of the individual array positions to see if they are didgets and if they are i can add them into seperate int and add them. – Mike Hirt Sep 25 '13 at 02:17
0

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.

DenisD
  • 620
  • 6
  • 9