473,729 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with a method exception!

1 New Member
Hi there i want a basic menu which the user inputs a number to chose from the menu. Thing is that i need to make sure that the input IS an integer. So i have an InputMismatchEx ception catch but when it occurs, the program just keeps outputting the menu over and over??

Expand|Select|Wrap|Line Numbers
  1.  static Scanner sc = new Scanner(System.in);
  2.  
  3.   public static void menu (){
  4.     System.out.println("Please enter a number to select from the following:\n\t1. Create new Array\n\t2. Load array from MyObject.dat");
  5.     int choice = 0;
  6.     try{ 
  7.       choice = sc.nextInt();
  8.       switch(choice){ 
  9.         case 1:
  10.           start();
  11.           break;
  12.         case 2:
  13.           loadArray();
  14.           break;
  15.         default :
  16.           System.out.println("invalid");
  17.       }
  18.     }catch (InputMismatchException e){
  19.       System.out.println("Please enter a valid NUMBER from the menu... Resetting menu.");
  20.     }
  21.     menu();
  22.   }
Mar 30 '08 #1
2 1206
JosAH
11,448 Recognized Expert MVP
Suppose you make your scanner read a nextInt(). If there isn't an int in the input
the scanner doesn't read anything and throws an exception as you have noticed.
If you immediately make it read another nextInt() the scanner sees the same
input and the story repeats itself (i.e. nothing read and an exception thrown) etc.

Simply skip the line that doesn't contain an integer and caused an exception
to be thrown.

kind regards,

Jos
Mar 30 '08 #2
hsn
237 New Member
so read it as a string, make sure it is not a char then convert it to integer by using the parse function......

good luck

hsn
Mar 30 '08 #3

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

Similar topics

0
9873
by: James Hong | last post by:
Help please, I try to sending an email from my html page using the java applet. but it give error on most of the PC only very few work, what is the error i make the java applet show as below ********************************** package Celcom.Client;
4
1827
by: Johannes Hansen | last post by:
What are the best practice on handling an exception caused by a Dispose method when its called from inside a loop? Wrap the entire loop in a try-catch or do the try-catch on each iteration to get as many of the object Disposed as possible? Take the following example: Bitmap bmpArray = ... // Some fancy bitmap array. try {
8
2612
by: John Bowman | last post by:
Hello, Does anyone have a good/reliable approach to implementing an IsNumeric() method that accepts a string that may represent a numerical value (eg. such as some text retrieved from an XML file)? Thus if you pass it "1" or "5.12345" it returns true, but "1b3q" it returns false? I know VB has this sort of function, but was wondering how to implement something similar in C#. I suppose I could use the Convert methods and catch any...
17
2674
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this cboFont_DrawItem event handler? Why does it need to be cast? -hazz ,................. cboFont.Items.AddRange(FontFamily.Families); } private void cboFont_DrawItem(object sender,
1
2741
by: MikeM | last post by:
We are getting a behavior on a Response.Redirect("SomeUrl", True) that I'm hoping someone can explain. This all refers to the code snip at the end. By the way, this is all VB ASP.NET v1.0 code. So we have the Page_Load event of an ASPX page fire which in the snip you see calls "SomeMethod". Also you see in Page_Load two Catch statements. The first is "SpecialException". We created our own extended exception (inherits from...
2
1596
by: Charles | last post by:
I need to find a way to share information between two classes, one is an employee class and the other is a custom error class that inherits from ApplicationException. These two classes are part of a ClassLibrary project. The other project has all the web page classes. My project allows employees to log in using Forms Auth, I do some checking on the person that is logging in and then after that is good I create an employee object and...
2
4442
by: Keith Kowalski | last post by:
I anm opening up a text file reading the lines of the file that refer to a tif image in that file, If the tif image does not exist I need it to send an email stating that the file doesn't exist then skip this file and move onto the next file (line). If file is there then move to a sirectory. Here is the code I have (Feel free to make corrections as needed. If possible make changes in red)
9
13212
by: Bill Borg | last post by:
Hello, I call a function recursively to find an item that exists *anywhere* down the chain. Let's say I find it five layers deep. Now I've got what I need and want to break out of that whole stack and continue execution at the point of the initial call. Is that possible? Thanks, Bill
2
4265
by: ad | last post by:
I use login control of VS2005 in my web application. The web applicaiton is developed in my notebook. It run ok in my notebook, but when I login in from another PC with the user ID and password, it can't login in. I tried again and again. When I login with incorrect password, the login control display the FailureText. But when I enter the correct password, the Failure Text disappear but it still stay in the login page.
0
10772
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9281
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.