error converting a string to a number
package com.codegym.task.task04.task0412;
/*
Positive and negative numbers
*/
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
//write your code here
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sNum = reader.readLine();
int nNum = Integer.parseInt(sNum);
nNum =(int)System.in.read();
if (nNum > 0) {
nNum =nNum * 2;
System.out.println(nNum);}
else
if( nNum < 0){
nNum = nNum +1;
System.out.println(nNum);
}
else
{nNum = nNum;
System.out.println(nNum);}
}
}
errror converting to a number
Under discussion
Comments (10)
- Popular
- New
- Old
You must be signed in to leave a comment
Henrik Krogsholm
12 October 2018, 15:47
no, but it says, that task is completed, but it is not possible to run the program-it just give an error message.
0
John Michael Montuya
11 October 2018, 22:04
Did you figure it out yet?
0
Henrik Krogsholm
10 October 2018, 19:43
It still does not work:
package com.codegym.task.task04.task0412;
/*
Positive and negative numbers
*/
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
//write your code here
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sNum;
int nNum =Integer.parseInt(sNum);
if (nNum > 0) {
nNum =nNum * 2;
System.out.println(nNum);}
else
if( nNum < 0){
nNum = nNum +1;
System.out.println(nNum);
}
else
{nNum = nNum;
System.out.println(nNum);}
}
}
0
Ankush Rajput
11 October 2018, 02:39
You are not storing any input in sNum. I told you to remove System.in.read and not readLine.
Do this -
Rest everything is fine.
When you reply to some comment, please do it by clicking on reply option below that comment. In this way, they get a notification and can get back to you.
If you make a new comment in reply to someone, they don't get a notification. 0
Henrik Krogsholm
11 October 2018, 20:15
I changed it again as you wrote, but still i get errors in "Run"
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
//write your code here
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sNum = reader.readLine();
int nNum =Integer.parseInt(sNum);
if (nNum > 0) {
nNum =nNum * 2;
System.out.println(nNum);}
else
if( nNum < 0){
nNum = nNum +1;
System.out.println(nNum);
}
else
{
System.out.println(nNum);}
}
}
0
Ankush Rajput
12 October 2018, 02:07
I just copy-pasted your program and is running as expected.
Are you entering a number in text box while running the program? It might be showing NullPointerException due to that.
Directly verify, it should pass. It is working.
If some other error is showing, let me know.
0
Henrik Krogsholm
10 October 2018, 18:33
I tried this, but it still does not work:(
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sNum;
int nNum =Integer.parseInt(sNum);
0
Ankush Rajput
10 October 2018, 18:39
Please share full code.
When you reply to some comment, please do it by clicking on reply option below that comment. In this way, they get a notification and can get back to you.
If you make a new comment in reply to someone, they don't get a notification.
0
Henrik Krogsholm
10 October 2018, 16:37
Because that was the way it was written in "Keyboard input2 Example 1. I just copied the example??
But thanks for your help -i will look at it.
0
Ankush Rajput
10 October 2018, 13:34
Why are you doing this nNum =(int)System.in.read(); after Integer.parseInt ? Integer.parseInt is enough to convert string to integer. Other line is not required.
Also, System.in.read is wrong syntax. You've wrapped system.in with bufferedreader, now use it for taking input as in reader.read or reader.readLine();
0