Results 1 to 15 of 15
  1. #1
    Cod
    Cod is offline Member
    Join Date
    Nov 2009
    Location
    Honolulu, HI
    Posts
    59
    Rep Power
    0

    Default 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 ('+'||'-'||'*'):
    I keep getting an error which states "operator || cannot be used with char,char". I've read through my text multiple times and a few places online, but cannot find any references to comparing chars with any operator, such as OR. Can someone please point me in the correct direction?

    Any guidance is greatly appreciated.

    Thanks.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    What do you want the line to do?

    Perhaps (just a guess):

    Java Code:
    case '+':
    case '-':
    case '*':
        // code any of the three options

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by pbrockway2 View Post
    What do you want the line to do?

    Perhaps (just a guess):

    Java Code:
    case '+':
    case '-':
    case '*':
        // code any of the three options
    You sir have a naughty browser!

  5. #5
    Cod
    Cod is offline Member
    Join Date
    Nov 2009
    Location
    Honolulu, HI
    Posts
    59
    Rep Power
    0

    Default

    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);
    This is the error I'm getting:
    Java Code:
    '.class' expected
    
    cannot find symbol
      symbol:   class evaluateExpression
      location: class Project2
    
    unexpected type
      required: value
      found:    class
    evaluateExpression is a method within the same class and seems correct. I believe the issue has to do with the type "Complex result" since Complex is a separate class, but I'm unsure. Any guidance is greatly appreciated.

    Thanks.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by Cod View Post
    I thought you had to have statements for each case, I didn't know you could group them in the way y'all provided.
    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
    Looks like you are treating it like a class or a variable. Did you forget brackets?

  7. #7
    Cod
    Cod is offline Member
    Join Date
    Nov 2009
    Location
    Honolulu, HI
    Posts
    59
    Rep Power
    0

    Default

    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;
            }
    }
    Like I stated earlier, the entire project compiles correctly, except for the one line in question. I can't figure out the error, other than the explanation given by the IDE. Of course, with my luck, I'll get this part figured out and find many more issues arise.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Not the method itself. Where you call the method.

  9. #9
    Cod
    Cod is offline Member
    Join Date
    Nov 2009
    Location
    Honolulu, HI
    Posts
    59
    Rep Power
    0

    Default

    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"));
    As far as I can tell, everything is bracketed correctly. I'm so worried I'm going to find out the entire project is messed up...

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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?

  11. #11
    Cod
    Cod is offline Member
    Join Date
    Nov 2009
    Location
    Honolulu, HI
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    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?
    I looked at the code further, but I cannot find what I did different. However, the line where I call the fromString method is different. The two methods are in two separate classes though, so adding Complex. instead of new will not accomplish anything.

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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?

  13. #13
    Cod
    Cod is offline Member
    Join Date
    Nov 2009
    Location
    Honolulu, HI
    Posts
    59
    Rep Power
    0

    Default

    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);
    I'm looking at an old text and feel as if I have it right, but obviously I don't. The example I'm looking at is for testing and returning the max number of entered integers. For example, from the text:

    Java Code:
    int k = max(i, j);
    So int k calls the method max(int num1, int num2) and returns the highest number. I feel like I'm doing the same sort of thing with my code, but obviously I'm not.

    Thanks for your help thus far.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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
    Or maybe it is something else you have wrong. Without seeing the exact error message and the relevant code I am only guessing.

  15. #15
    Cod
    Cod is offline Member
    Join Date
    Nov 2009
    Location
    Honolulu, HI
    Posts
    59
    Rep Power
    0

    Default

    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

  1. help with switch statement
    By java__beginner in forum New To Java
    Replies: 4
    Last Post: 03-19-2009, 03:22 PM
  2. Demonstration of the switch statement
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:08 PM
  3. Method in a Switch Statement
    By cart1443 in forum New To Java
    Replies: 6
    Last Post: 03-14-2008, 04:48 AM
  4. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 06:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •