473,385 Members | 1,523 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,385 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;
  3. public class O2a{
  5.     public static void main (String[] args){
  7.     Scanner scan = new Scanner(System.in);
  8.     while (true){
  10.         System.out.println("\nPlease enter two integers ");
  11.         try{
  12.             int a=scan.nextInt();
  13.             int b=scan.nextInt();
  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 13085
Tassos Souris
152 100+
You must import the exception
Expand|Select|Wrap|Line Numbers
  1. import java.util.InputMismatchException;
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
11,448 Expert 8TB
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,

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

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...
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,...
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...
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...
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'....
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...
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 */
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...
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 ...
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.