-3

Getting NumberFormatException while Converting String to Integer and trying to print it.

My code:

BufferedReader bur=new BufferedReader (new InputStreamReader(System.in));   
String s=bur.readLine();        

int a=Integer.parseInt(s);
System.out.println(a);

input-rakib

  • Please take the [Tour](https://stackoverflow.com/tour), read [How To Ask](https://stackoverflow.com/help/how-to-ask) and create an [MCVE](https://stackoverflow.com/help/mcve). Have you consulted your [Rubber Duck](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)? – Hermann Döppes Jan 15 '17 at 18:08
  • 3
    What's your input from `bur`? Could you print `s` and share it with us? – Mureinik Jan 15 '17 at 18:09
  • That means that `s` is a set of characters like this : "characters" – Null Jan 15 '17 at 18:09
  • 1
    I suppose you use Java so it would be a good idea to use this Tag. – Sascha Jan 15 '17 at 18:12
  • 1
    Possible duplicate of [number format exception](http://stackoverflow.com/questions/5604532/number-format-exception) – Joe C Jan 15 '17 at 18:56
  • That code works fine for me. What are you inputting? – Chai T. Rex Jan 15 '17 at 22:17
  • @ChaiT.Rex That code works fine for specific input string , try to input string "FOOBAR" and it will result NumberFormatException – Oghli Jan 15 '17 at 22:20
  • 1
    That's why I asked what was inputted. – Chai T. Rex Jan 15 '17 at 22:24
  • i am sorry. i am a noob and new in StackOverflow .@Sascha – Emranul Haque Rakib Jan 16 '17 at 10:06
  • i was inputting character string like "rakib" . i wanted to convert character string into binary – Emranul Haque Rakib Jan 16 '17 at 10:08
  • @EmranulHaqueRakib _character string into binary_. What is that supposed to mean? The integer value of a character as binary digit? Then you should have a look at `Character.digit()` and `Integer.toBinaryString()` – Würgspaß Jan 16 '17 at 10:47
  • thanks @ Würgspaß ,i didn't ask my question properly. – Emranul Haque Rakib Jan 16 '17 at 12:40
  • @EmranulHaqueRakib ok, then I think you should edit your question, so that others get the right idea and do not have to read all these comments – Würgspaß Jan 16 '17 at 13:13
  • Possible duplicate of [What is a NumberFormatException and how can I fix it?](http://stackoverflow.com/questions/39849984/what-is-a-numberformatexception-and-how-can-i-fix-it) – xenteros Jan 25 '17 at 06:45

2 Answers2

0

You can get chars from String, than can obtain integer value for each char as below

BufferedReader bur=new BufferedReader (new InputStreamReader(System.in));   
String str = bur.readLine();   
char[] chars = str.toCharArray();
for(char c: chars){
    System.out.println((int)c);
}
Anil Agrawal
  • 2,748
  • 1
  • 24
  • 31
0

To print out the numerical vaĺue of each character of a text input to binary digit, replace your last two lines with this:

for (int i=0; i<s.length(); i++) {
    int charValueAsInt = (int)s.charAt(i);
    System.out.println(Integer.toBinaryString(charValueAsInt)); 
}

Retrieve the char value of each character, convert it to int (widening conversion) and then convert to binary digit.

Würgspaß
  • 4,660
  • 2
  • 25
  • 41