Results 1 to 15 of 15
- 03-23-2011, 05:22 AM #1
Member
- Join Date
- Nov 2009
- Location
- Honolulu, HI
- Posts
- 59
- Rep Power
- 0
Error With Portion of Switch Statement
I'm having an issue with a single line of code. The portion of the switch statement in questions is below:
Java Code:case ('+'||'-'||'*'):
Any guidance is greatly appreciated.
Thanks.
- 03-23-2011, 05:25 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,717
- Rep Power
- 16
What do you want the line to do?
Perhaps (just a guess):
Java Code:case '+': case '-': case '*': // code any of the three options
- 03-23-2011, 05:31 AM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 11
Yep you can't apply those operators to chars or numeric types.
You can fall though the cases with
Java Code:case '+': case '-': case '*': // something that applies to both
- 03-23-2011, 05:34 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 11
- 03-23-2011, 10:00 PM #5
Member
- Join Date
- Nov 2009
- Location
- Honolulu, HI
- Posts
- 59
- Rep Power
- 0
Thanks for the response y'all. I thought you had to have statements for each case, I didn't know you could group them in the way y'all provided. That portion compiles now; however, I noticed another error in the same switch statement. The code in question is below:
Java Code:Complex result = new evaluateExpression(c1, c2, char);
Java Code:'.class' expected cannot find symbol symbol: class evaluateExpression location: class Project2 unexpected type required: value found: class
Thanks.
- 03-23-2011, 10:07 PM #6
You cannot really group them together. What the code is actually doing is: if the case is '+' do nothing and since there is no break statement it falls through to the next case for '-' which also does nothing and no break statement so it once again falls through into the next case for '*' where it now has something to do.
evaluateExpression is a method
- 03-23-2011, 10:26 PM #7
Member
- Join Date
- Nov 2009
- Location
- Honolulu, HI
- Posts
- 59
- Rep Power
- 0
Thanks for the explanation about the case statements within the switch. As for my error, I'm not missing any brackets. Here is the method in question:
Java Code:public static Complex evaluateExpression(Complex c1, Complex c2, char operator){ if (operator == '+'){ Complex result = Complex.add(c1, c2); return result; } else if (operator == '-'){ Complex result = Complex.subtract(c1, c2); return result; } else { Complex result = Complex.multiply(c1, c2); return result; } }
- 03-23-2011, 10:27 PM #8
Not the method itself. Where you call the method.
- 03-23-2011, 10:33 PM #9
Member
- Join Date
- Nov 2009
- Location
- Honolulu, HI
- Posts
- 59
- Rep Power
- 0
Here's the entire do-while loop in question:
Java Code:do { System.out.println("Enter a postfix expression of complex numbers: "); StringTokenizer input = new StringTokenizer(keyboard.nextLine()); while(input.hasMoreTokens()) { charConv = input.nextToken(); char nextChar = charConv.charAt(0); switch(nextChar) { case '(': StringTokenizer expression = new StringTokenizer(input.nextToken(")")); Complex newComplex = Complex.fromString(expression); theStack.push(newComplex); break; case '+': case '-': case '*': //pop two operands from the stack Complex c1 = (Complex) theStack.pop(); Complex c2 = (Complex) theStack.pop(); //evaluate [COLOR="Red"]Complex result = new evaluateExpression(c1, c2, char);[/COLOR] theStack.push(result); break; } //pop the final result off the stack Complex finalResult = (Complex) theStack.pop(); System.out.println("The result of your input is:" + finalResult.toString()); System.out.println("Evaluate another expression? (Y = Yes):"); more = keyboard.next(); } } while(more.equalsIgnoreCase("y"));
- 03-23-2011, 10:35 PM #10
OK now that I have seen your code I know what is wrong. Compare the line of code where you call the evaluateExpression method with every other line of code where you call a method. Do you notice something different about it? What doesn't belong?
- 03-24-2011, 01:35 AM #11
Member
- Join Date
- Nov 2009
- Location
- Honolulu, HI
- Posts
- 59
- Rep Power
- 0
- 03-24-2011, 01:39 AM #12
Really????
Your line of code
Complex result = new evaluateExpression(c1, c2, char);
Normal method calls
String s = getUserInput();
int pos = s.indexOf("H");
doStuff(42);
You still cannot see what is wrong with your code?
- 03-24-2011, 02:00 AM #13
Member
- Join Date
- Nov 2009
- Location
- Honolulu, HI
- Posts
- 59
- Rep Power
- 0
No, I just don't see it. Regardless of the code I try, I get the same error message. I thought the code below would work, but I get the same message.
Java Code:Complex result = evaluateExpression(c1, c2, char);
Java Code:int k = max(i, j);
Thanks for your help thus far.
- 03-24-2011, 02:11 AM #14
Obviously the new keyword was wrong. What you were trying to do was create a new evaluateExpression object not call the evaluateExpression method. If you still have an error then it is due to something else. Is the evaluateExpression method located in a different class? If so then you need to call the method on an instance of that class. Just like you cannot just call the charAt method. You need a string to call it on.
Java Code:char c = charAt(0); // wrong String s = "hello"; char c = s.charAt(0); // correct
- 03-24-2011, 11:25 AM #15
Member
- Join Date
- Nov 2009
- Location
- Honolulu, HI
- Posts
- 59
- Rep Power
- 0
After going over what you said, I changed the line to the following:
Java Code:Complex newResult = evaluateExpression(c1, c2, char)
I'm still getting the same error though, value expected and class seen. I'm questioning whether or not it has to do with the Complex class. Throughout the Complex class, the methods are all methodName(complex c1, complex c2)...there is no char variable in any method except for the main and evaluateExpression methods (both in Primary class, not Complex class).
Could this be the case?
Similar Threads
-
help with switch statement
By java__beginner in forum New To JavaReplies: 4Last Post: 03-19-2009, 02:22 PM -
Demonstration of the switch statement
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:08 PM -
Method in a Switch Statement
By cart1443 in forum New To JavaReplies: 6Last Post: 03-14-2008, 03:48 AM -
Switch Statement Help
By bluegreen7hi in forum New To JavaReplies: 6Last Post: 02-06-2008, 05:16 AM
Bookmarks