473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 13097
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

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

Similar topics

3
by: Rutger Claes | last post by:
Code and problem are from PHP5: When I execute the following piece of code, the DomException is thrown with a message: Not Found Exception. I assume this means that the node I extracted from the...
2
by: Kevin R. | last post by:
I have been ignoring this problem for a few weeks now, but it's becoming a bit annoying not to mention unproductive. Here it goes: I compile my project with no errors. Then after I debug/run it,...
14
by: NormD | last post by:
We have a client-server app using Web Services on an IIS machine. The trace below shows that .NET is searching around for some things (e.g., SystemDrawing.DLL and System.Drawing.EXE) and taking a...
0
by: cwho.work | last post by:
Hi! We are using apache ibatis with our MySQL 5.0 database (using innodb tables), in our web application running on Tomcat 5. Recently we started getting a number of errors relating to...
5
by: Amit_Basnak | last post by:
Dear Friends I have been getting the following error Error 185: "WorkFlow_dce.cpp", line 58 # Left side of '->' requires a pointer to class; type found was 'struct WF_SEARCH_WU'....
1
Dököll
by: Dököll | last post by:
Greetings, Good buddies! I am for the first time, since I started learning VB, going to build an application I wanted to build for my first son, a language and activities program that will allow...
1
by: jimgym1989 | last post by:
I dont get it..why is the error: Exception in thread "main" java.util.InputMismatchException this is my code /** * @(#)textFileRead.java * * * @author * @version 1.00 2008/10/17 */
1
by: sora | last post by:
Hi, I've developed a MFC program under VS 6.0. My debugger *was* working fine and I've used it often for my project. Then, one day, the errors below appear and they prevent me from using the...
1
by: jimgym1989 | last post by:
Why is it that it is InputMismatchException? I keep on tracing my code but i find it Ok and runnable but every time I run it it says: Exception in thread "main" java.util.InputMismatchException ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.