InputMismatchException not found?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BurnTard
    New Member
    • May 2007
    • 52

    InputMismatchException not found?

    Why on earth does the catch statement return an error? It's pretty much cut and paste from a java schoolbook! What the program does isn't all that important, without the try/catch, the code compiles fine... But as soon as the catch is introduced, I get an error saying "cannot find symbol," on line 23, the catch line.

    [CODE=java]
    import java.util.Scann er;

    public class O2a{

    public static void main (String[] args){

    Scanner scan = new Scanner(System. in);
    while (true){

    System.out.prin tln("\nPlease enter two integers ");
    try{
    int a=scan.nextInt( );
    int b=scan.nextInt( );

    // Solution:
    System.out.prin tln("a + b = " + (a+b));
    System.out.prin tln("a - b = " + (a-b));
    System.out.prin tln("a * b = " + (a*b));
    System.out.prin tln("a / b = " + (a/b));
    System.out.prin tln("a % b = " + (a%b));
    System.out.prin tln("(a / b) * b + a % b = " + (a+(a%b)));
    }
    catch (InputMismatchE xception exception){
    System.out.prin tln("Are you trying to quit the program? (y/n) ");
    String inAnswer=scan.n extLine();
    if (inAnswer=="n") {
    continue;
    }
    else if (inAnswer=="y") {
    break;
    }
    }
    finally{
    continue;
    }
    }
    }
    }[/CODE]
  • Tassos Souris
    New Member
    • Aug 2008
    • 152

    #2
    You must import the exception
    Code:
    import java.util.InputMismatchException;
    Also doing
    Code:
    if ( str == "a string here" ){  }
    isn't the correct way of
    comparing two strings. Look at the methods of the String class.

    Hope i helped.

    Comment

    • BurnTard
      New Member
      • May 2007
      • 52

      #3
      Yup, thanks a bunch! Now I just have to figure out how to do the stringcompariso n in java... Why can't java just work like the other languages? I've barely used it for a week, and I've found so many unnecessary quirks already it's driving me bonkers!

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by BurnTard
        Why can't java just work like the other languages? I've barely used it for a week, and I've found so many unnecessary quirks already it's driving me bonkers!
        None of the languages work alike; otherwise there wouldn't be any need for a language that works just like other languages do.

        kind regards,

        Jos

        Comment

        • N002199B
          New Member
          • Feb 2007
          • 41

          #5
          simply put, "You cannot build a house from the roof downwards". Java is a very stable language, if only you start with the basics and cement a good foundation.

          Try reading someting entitled, Introduction to Java

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            All the information you need on Strings and any other Java controls can be found in the Java API Documentation. This resource should get you pointed in the right direction....th ere are multiple articles on the Strings there. I recommend bookmarking the Java API Documentation and using it as your primary resource when developing in Java.

            Comment

            Working...