0
Scanner sc = new Scanner(System.in);
length = sc.nextLine();
length_to_play = Integer.parseInt(length);

I have tried using length.trim() and length.replaceAll() to discard the whitespaces, but didn't work. I'm having Exception in thread "main" java.lang.NumberFormatException.

Exception in thread "main" java.lang.NumberFormatException: For input string: "y"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at speedwords.first_activity(speedwords.java:27)
    at speedwords.main(speedwords.java:338)
5
  • do you get any error? Commented Apr 13, 2015 at 4:56
  • @ArhatBaid post complete stacktrace, it will show you the string also. Commented Apr 13, 2015 at 4:57
  • yes.. whenever i input a character i'm getting a "main" java.lang.NumberFormatException. Commented Apr 13, 2015 at 4:57
  • @ArhatBaid character or number you are inputting Commented Apr 13, 2015 at 4:58
  • i'm trying to input an integer, but whenever a character is input a print message should be shown to the user( I'm using if-else). But on inputting a character this error is coming. Commented Apr 13, 2015 at 5:00

4 Answers 4

3

I think you have misunderstood thefunctionality of

Integer.parseInt();

From the Java Docs

  Parses the string argument as a signed decimal integer. The 
  characters in the string must all be decimal digits, except that 
  the first character may be an ASCII minus sign <code>'-'</code> 
  (<code>'&#92;u002D'</code>) to indicate a negative value. The resulting 
  integer value is returned, exactly as if the argument and the radix 
  10 were given as arguments to the 
  {@link #parseInt(java.lang.String, int)} method.

  @param s       a <code>String</code> containing the <code>int</code>
              representation to be parsed
  @return     the integer value represented by the argument in decimal.
  @exception  NumberFormatException  if the string does not contain a
                parsable integer.
public static int parseInt(String s) throws NumberFormatException {
    return parseInt(s,10);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

But shouldn't parseInt() return the ASCII integer value of the string?
@ArhatBaid look for the inner implementation , you will get all your answers
1

When the user is inputting numbers, you should use sc.nextInt(). This way you don't need to write the parsing yourself.

If you're expecting strings of characters and want to convert them into ints, you can use Character.getNumericValue(myChar) on each character of the input string individually.

4 Comments

if i use sc.nextInt () , then how can i print a message to the user using a IF-ELSE block? I was hoping that on inputting a character i can convert it to it's ASCII integer value and check in the IF block
Okay, you might want to edit the question to mention that you want to convert characters specifically.
but isn't a character also a possible string? even if i input, a string for eg "tyf" the parseInt() doesn't return the ASCII integer values.
No, that's not how it works. What would you expect as an int result for "tyf"? The sum of its characters' ASCII values or a concatenation of the values as a string? I've edited my answer.
0

You should have a look at the documentation on Integer.parseInt(String s)

As you can see the parsed String is converted into integer value and if the value is not an integer it throws NumberFormatException.

Why are you expecting it should return ASCII integer value?

Answer: For your kind information we are passing String, not a char which has independent ASCII value. If you want the ASCII of each character in your String then you can have a look at this question.

1 Comment

yes that is what I did at last in a little different way.
0

okay, I'm posting the answer that I found worked best for the case i want. But thank you all for your help.

Scanner sc = new Scanner(System.in);
length = sc.nextLine();
//length_to_play = Integer.valueOf(length); didn't work
char ch = length.charAt(0);
length_to_play=Character.getNumericValue(ch); //worked

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.