473,394 Members | 2,160 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,394 software developers and data experts.

Try... Catch

Hello everyone, this is my first time posting, so play nice >.>

I have a loop with a try-catch statement in it that is giving me issues., here is the code for it:
Expand|Select|Wrap|Line Numbers
  1. do
  2.         {
  3.             System.out.print("Number of courses being entered: ");
  4.             try
  5.             {
  6.                 numCourses = userIn.nextInt();
  7.                 failed = false;
  8.             }
  9.             catch (InputMismatchException ime)
  10.             {
  11.                 failed = true;    
  12.             }
  13.             if (numCourses < 1 || numCourses > 8 || failed)
  14.                 System.out.println("Invalid Number Entered.  Please enter a valid number between 1 and 8.");
  15.         }
  16.         while (numCourses < 1 || numCourses > 8 || failed);
The idea being, control the range, and check to see if they entered a non-numercial character, thus giving me a InputMismatchException. This is all being done in the console window, btw. My problem is this:
When I run this, I am given the first prompt, asking me num of courses, fine, I enter an integer, and the rest of the program works just fine. If I enter a letter, or any other non-numerical character, it throws and exception, which it catches, and sets failure to true. It then goes to the if statement, prints the line because failure is true, and then goes to the while(), finds that failure is true, so it goes back to the top, but it DOES NOT ask for another input, it skips the entire try-catch statement, and does the if statement again, giving me an infinite loop.

So, my question: Why doesn't it redo the try-catch statement once I've been throught the loop once. Why is it skipping try-catch every subsequent run throught the loop, going straight to the if statement?

Much appreciated, try-catch is still new to me, so it's probaly some property of try-catch I don't know about...

Thanks again!
Oct 30 '08 #1
4 2078
JosAH
11,448 Expert 8TB
As a matter of fact, it doesn't have anything to do with try ... catch statements:
I assume you're using a Scanner and a Scanner doesn't read anything when it
needs to read a number when there isn't any; e.g. it needs a number and you
type "foo". An exception is throws and nothing is read so the next time you
read something the same "foo" will still be in the input buffer.

The solution is simple: when reading fails skip the entire line; read the Scanner
API documentation how it can read an entire line (which you discard).

kind regards,

Jos
Oct 30 '08 #2
I'll look into that, thank you for the timely reply.

Hokai, so, I took a look, I don't seem to be able to figuire out what I should be doing, perhaps one more hint in the right direction?
Oct 31 '08 #3
JosAH
11,448 Expert 8TB
I'll look into that, thank you for the timely reply.

Hokai, so, I took a look, I don't seem to be able to figuire out what I should be doing, perhaps one more hint in the right direction?
If your program executes the statement(s) in the catch clause an exception must
have been thrown and nothing was read. You have to get rid of the current line
then. Add a statement there that reads the entire (faulty) line. Read the API
documentation for the Scanner class.

kind regards,

Jos
Oct 31 '08 #4
AHA!!
Got it, works exactly as needed. Thank you for the help JosAH.
And thanks for not giving it away straight up, made me work for my answer, I learned more this way :)
Nov 1 '08 #5

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

Similar topics

10
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
11
by: kaeli | last post by:
Hey all, I'd like to start using the try/catch construct in some scripts. Older browsers don't support this. What's the best way to test for support for this construct so it doesn't kill...
4
by: Abhishek Srivastava | last post by:
Hello All, I have seen code snippets like try { ..... } catch {
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
13
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
23
by: pigeonrandle | last post by:
Hi, Does this bit of code represent complete overkill?! try { //create a treenode TreeNode tn = new TreeNode(); //add it to a treeview tv.Nodes.Add(tn);
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: 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...
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: 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
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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.