So i wrote the code and ran it, and got the error saying "java.lang.NumberFormatException: Solution.java, method main, line: 18" and "Error converting a string to a number (invalid format)." Thats what you get i think when you enter letters instead of number (Even though i entered the digit, not letter) and try to parse it. And got stuck for half hour trying to figure whats wrong with the code, and then i just tried to verify it as it is, and it passed?!?!? If you can please check if something is wrong with the task.
This task has some errors or something, please check it
Under discussion
Comments (7)
- Popular
- New
- Old
You must be signed in to leave a comment
Fadi AlSaidi
20 January 2019, 17:38
what method are you using to read from the keyboard? if you are using :-
BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
then the keyboard will read String by default. You will need to convert that to an int using Intger.parseInt();
you can either have the reader nested in the Integr.parseInt();, or you can convert the String variable after:
1-Nested : int x = Integer.parseInt(reader.readLine());
2-Non nested: String s = reader.readLine());
int x = Integer.parseInt(s);
I hope thats what you were looking for
0
Guadalupe Gagnon
20 January 2019, 14:33
You are right in you assessment. number format exception is thronw when parsing incorrect values.
You need to attach your code when you write a post though. Use the slidebar that says the same.
0
dD
20 January 2019, 20:28
The task passed verification with that error so i cant attach the code, since its verified, so ill just copy the code here in comment. I just wanted to inform you to check for error if there is one, so that nobody else have the problem like me, and to think that their code is bad even if it is not ( i still dont know if my code is correct, cause its verified but when you run it there is an error :) ) Delete this comment if you need to, since it has the full code in it.
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
int s = 0;
while(true){
String input = r.readLine();
int a = Integer.parseInt(input);
if (a!=-1){
s += a;
}
else{
s += a;
System.out.println(s);
break;
}
}
}
}
+2
Guadalupe Gagnon
21 January 2019, 14:18
i don't see anything that would cause problems, unless you were actually entering letters instead of numbers, but it sounds like you know the difference.
It sounds like you are not using intelliJ, are you just using the online compiler? The online compiler cannot handle the code necessary to complete tasks above above a certain level.
0
dD
21 January 2019, 15:52
Yes i was using online compiler, cause i was at work, and i dont have intelliJ there. Sorry for causing the trouble with this, i just didnt know that the online compiler cant handle it. I will delete the question. Please can you just tell me if this plugin works with netbeans? I like it more than intelliJ and want to use it if i can.
0
Guadalupe Gagnon
21 January 2019, 16:04
no need to delete. I'm glad i was able to figure out your problem. Maybe your question will help someone else in the future, so i would encourage you to leave it up.
I don't use netbeans, so can't answer that question. However, you should be able to copy your code from netbeans into the online compiler and have it validated. When i said it doesn't handle code i should have specified that it wont run the code, however the validation system is the same and will be able to validate the code from either compiler.
0
dD
21 January 2019, 16:18
Ok, thanks for your help, ill leave the question.
+1