• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

problem NumberFormatException For input String ""

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

I am coding a menu system the basic idea is that I have a main menu and then other sub menu's and each one has option to return to main. The code works fine until I try to return to the main menu from one of the sub menu's. I think this is due to the lines:

String sc=in.readLine();
int b=Integer.parseInt(sc);-- this line produces the error

It's not stopping to let me input a number, i think that this is due to int b already having a value. I have tried to initialise this variable at the top of the method (b=o;sc=null but this still doesn't work.

If i try to catch the exception it automatically opens the previous sub menu I came from even if the value is set to 0.

Hope I have given enough detail of this problem. Let me know if you need to see the code, just didn't want to make this post hard to read.

Regards


Chris J
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chris,

Welcome to JavaRanch!

You need to check the String to see if it's empty, and decide what to do based on that -- i.e.,



I also showed how you could deal with several kinds of erroneous input.
[ November 20, 2004: Message edited by: Ernest Friedman-Hill ]
 
Chris G Johnson
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, will try that tommorow and let you know how I get on.

P.s Thanks for the fast response, I have posted that problem on 3 other forums and not one has replied in over 2 days. Will definately recommend this one.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chris G Johnson:
String sc=in.readLine();
int b=Integer.parseInt(sc);-- this line produces the error

It's not stopping to let me input a number, i think that this is due to int b already having a value. I have tried to initialise this variable at the top of the method (b=o;sc=null but this still doesn't work.

The value held by b will have no effect on the above two lines, and it certainly wouldn't stop in.readLine() from working. Are you reading a character at another point but hitting a character and the enter key when you run it? It's been a long, long while since I've done any keyboard input directly with Java.

Ernest is spot-on though about looping until something valid is entered. I think you'll continue to find this forum is filled with many helpful people!
 
Chris G Johnson
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Just to let you know that the first solution worked like a charm. Thanks for the help, by the way do you know if you can get the posts from this forum sent directly to your e-mail address.

Thanks again.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chris G Johnson:
by the way do you know if you can get the posts from this forum sent directly to your e-mail address.



Not all the posts, but you can get notified when someone replies to a post you made. When you're starting a thread, look on the editor screen for a checkbox that turns this on.
 
Chris G Johnson
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks I will do that next time. I now have another problem with my code.
I have a loop which displays all of the orders in the order Queue.

This code seems to work ok even though it does look very messy, its probably more complicated than it needs to be. However, I can only get it to display the items in the first order, this is due to item[1][1] not having a value in it. The way I am assigning this variable is:



The orderID is getting incremented as a new order is started but for some reason I can't acces it.

Hope you have enough detail there to help.

Thanks
 
Chris G Johnson
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry ignore that last reply I had made a very simple error and i'm not telling you what it was cause you will think I am very thick. lol.

Thanks for all the help everyone.
 
She's out of the country right now, toppling an unauthorized dictatorship. Please leave a message with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic