Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Lost, need help

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Please help me figure out what is wrong with this code. I am getting errors that I dont know how to debug.
thanx.




//JAM -- Fixed [/CODE] tag
[ March 29, 2005: Message edited by: Joel McNary ]
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are the errors? Have you made sure you've imported all the required classes?
 
Sarah Rainier
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the error occurs when i put in the required input at the command line. it is not capturing or storing the string that i type. the error message is

Exception in thread "main" java.lang.NumberFormatException for input string
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the required input? Please show us exactly what you typed and what error you got and why it is not what you expect or need.

I tried running your code and it works for one number, not for a non-numeric string, and not for a line with multiple numbers.

 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As noted above, you need to show us exactly what you did to cause the error. Copy and paste is your friend!

Also, your code should go IN BETWEEN the [ CODE ] and [ /CODE ] tags that are created by the UBB instant code button for them to be any good.

Layne
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can see how this would only work for one number and not for say '99 99'. Your call to readLine() will take the full String value '99 99' and try to convert it to a single number. This is where your number format exception is coming from.

Without trying the code myself, what happens if you hit enter between the numbers? You may have to re-work your calls to read in the numbers to loop until you either get no data or some sort of user-defined terminator.
 
Bartender
Posts: 1843
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SunFlawa:

Welcome to JavaRanch! We hope that you find the place helpful. However, please note our JavaRanch Naming Policy, to which your name unfortunately does not comply. We are looking for real-sounding names (first and last names and not obviously fictitous...) Please Change your display name to comply with this.

Thanks
 
Sarah Rainier
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everyone!

thanx for your help. the purpose of the code is to capture and store string input and then parse this stored string to integer.

the error below was generated when i typed in the string input of "jjss"


Exception in thread "main" java.lang.NumberFormatException: For input string: "jjss"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:477)
at java.lang.Integer.parseInt(Integer.java:518)
at RandomizeString.main(RandomizeString.java:21)
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The exception's stack trace itself says you the error.
Use try catch block & when ever invalid input is give you assign a default value or loop until a valid value is found.
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An alternative way is to check whether the input string is a "number" before passing it to the method call.

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

u r reading the input through the statement:

s1 = mark.readLine();

readLine() accepts a line of input(i.e until u press enter).
Now u r entering the following numbers:

99 99

(observe there's a space between the 2 numbers, u'll asume it as 2 numbers but readLine() reads it as a line of characters) so the variable s1 contains the string "99 99" and so the statement

Integer.parseInt(s1)

throws NumberFormatException because there's a space between the 2 numbers(99 99).
 
Sarah Rainier
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you Ravi,

I understand what you are saying about reading the input, however, how would you explain why it throws the same error even when i put in strings or characters with no space between them?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your characters are non-numeric you'll get this error. A space is not the only value which will cause it.
 
Ravi Kishore
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sarah,

what paul has said is correct, i.e when u r converting a string in to a integer using the wrapper class method(Integer.parseInt(string)), the string must contain only numerals but not non-numerals.

can u say "99e99" or "67 87" as numbers? so while converting these strings to numbers u get NumberFormatException.

the best programming practise is to use try-catch block surrounding such kind of operations.
[ March 30, 2005: Message edited by: Ravi Kishore ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sarah Rainier:
thank you Ravi,

I understand what you are saying about reading the input, however, how would you explain why it throws the same error even when i put in strings or characters with no space between them?



I strongly suggest you bookmark and learn how to navigate the Java API docs. In this case, you can easily find out the cause of this exception by examining the documentation for the Integer class. Scroll down until you find the parseInt() method and you will see the possible reasons that it will thrown NumberFormatException. The docs say this exception is thrown "if the string does not contain a parsable integer" just as others have described here. As you can see, you will often be able to find the answers to your questions from this documentation. In my opinion, knowing how to use the Java API docs is an essential skill for any Java programmer.

HTH

Layne
 
I'm all tasted up for a BLT! This tiny ad wants a monte cristo!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic