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)
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Arhat Baid
  • 1,011
  • 10
  • 18

4 Answers4

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);
    }
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
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.

Mick Mnemonic
  • 7,808
  • 2
  • 26
  • 30
  • 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 – Arhat Baid Apr 13 '15 at 05:07
  • Okay, you might want to edit the question to mention that you want to convert characters specifically. – Mick Mnemonic Apr 13 '15 at 05:10
  • 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. – Arhat Baid Apr 13 '15 at 05:15
  • 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. – Mick Mnemonic Apr 13 '15 at 05:21
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.

Community
  • 1
  • 1
Naman Gala
  • 4,670
  • 1
  • 21
  • 55
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
Arhat Baid
  • 1,011
  • 10
  • 18