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.
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.
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
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)
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.