469,323 Members | 1,611 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

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.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class O2a{
  4.  
  5.     public static void main (String[] args){
  6.  
  7.     Scanner scan = new Scanner(System.in);
  8.     while (true){
  9.  
  10.         System.out.println("\nPlease enter two integers ");
  11.         try{
  12.             int a=scan.nextInt();
  13.             int b=scan.nextInt();
  14.  
  15.             // Solution:
  16.             System.out.println("a + b = " + (a+b));
  17.             System.out.println("a - b = " + (a-b));
  18.             System.out.println("a * b = " + (a*b));
  19.             System.out.println("a / b = " + (a/b));
  20.             System.out.println("a % b = " + (a%b));
  21.             System.out.println("(a / b) * b + a % b = " + (a+(a%b)));
  22.         }
  23.         catch (InputMismatchException exception){
  24.             System.out.println("Are you trying to quit the program? (y/n) ");
  25.             String inAnswer=scan.nextLine();
  26.             if (inAnswer=="n"){
  27.             continue;
  28.             }
  29.             else if (inAnswer=="y"){
  30.             break;
  31.             }
  32.         }
  33.         finally{
  34.             continue;
  35.         }
  36.     }
  37.     }
  38. }
Sep 9 '09 #1
5 11731
Tassos Souris
152 100+
You must import the exception
Expand|Select|Wrap|Line Numbers
  1. import java.util.InputMismatchException;
  2.  
Also doing
Expand|Select|Wrap|Line Numbers
  1. 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.
Sep 9 '09 #2
Yup, thanks a bunch! Now I just have to figure out how to do the stringcomparison 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!
Sep 9 '09 #3
JosAH
11,448 Expert 8TB
@BurnTard
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
Sep 10 '09 #4
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
Sep 10 '09 #5
Frinavale
9,735 Expert Mod 8TB
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....there 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.
Sep 21 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

14 posts views Thread by NormD | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.