+ 1

Error type

One of the SoloLearn challenge questions left me puzzled. Question: Choose the true answer for this code. int value = Integer.parse(“12.7”); Why the correct answer is “Runtime Exception”? (My answer was Compile Time error but marked wrong) Thanks.

20th May 2020, 5:42 PM
GG128
11 Respostas
+ 4
yeah, Jayakrishna🇼🇳 is right here! i mentioned parseToInt as a shortcut to Integer.parse() my mistake there for not being clear with that. sorry about that... i think it's Integer.parseInt("12.7"); but that is actually a compilation error if i think about it twice!
20th May 2020, 7:23 PM
Sebastian Pacurar
Sebastian Pacurar - avatar
+ 3
GE12 EDIT!! my answer is wrong! please see Jayakrishna🇼🇳 's explanation!! you can't parse a string to int which contains any other symbol except + or - symbols. in order to parse that one to integer, first you need to parse it to float and after, parse the result to integer. why is it a runtime error? because it doesn't mark it as an error only when the code runs, after it passes the compilation stage. if you do this int a = "5.4" it is a compilation error since it doesn't reach runtime (compilation errors are the first to be pointed out followed by runtime errors) since you have no compilation error, when the code reaches the line where you parseToInt("12.7"), it is already passed compilation stage and it happens during runtime stage
20th May 2020, 6:58 PM
Sebastian Pacurar
Sebastian Pacurar - avatar
+ 2
In java, It is compile time error only.. But challenge has typo error.. There is no parse method in Integer class.... It is once reported as wrong answer but not removed yet.. You can report it again to Sololearn.. Edit: @GE12 @Abhay, @Sebastian Pacurar If it is Integer.parseInt("12.7"); then it is run time error.. But there missing int, Integer.parse("12.7); there is no parse method in Integer class, and no parseToInt(" 12.7") method also.. So it is compile time error only.
20th May 2020, 6:58 PM
Jayakrishna 🇼🇳
+ 1
GE12 you're welcome... Yes. It's typo mistake there.. Not about the concept. Sebastian Pacurar It is already discussed once, I did same earlier.. I have replied same without clearly examining in hurry, then some one corrected my mistake..
20th May 2020, 7:36 PM
Jayakrishna 🇼🇳
+ 1
String parsed to integer !
21st May 2020, 11:24 AM
Shahghasi Adil
Shahghasi Adil - avatar
0
Becoz it is runte error not a compiler error
20th May 2020, 5:51 PM
Abhay
Abhay - avatar
0
GE12 A runtime error happens during the running of the program. A compiler error happens when you try to compile the code. If you are unable to compile your code, that is a compiler error. If you compile and run your code, but then it fails during execution, that is runtime https://stackoverflow.com/questions/9471837/what-is-the-difference-between-run-time-error-and-compiler-error
20th May 2020, 5:55 PM
Abhay
Abhay - avatar
0
Yes I understand the difference, but here is the thing, I even typed this code in Eclipse to try and the compiler immediately gave warning, because parse should be parseInt. I’m gettting a compile error even before running the code.
20th May 2020, 6:00 PM
GG128
0
GE12 Stop down voting my ans
20th May 2020, 6:03 PM
Abhay
Abhay - avatar
0
Funny that you upvoted your own answer.
20th May 2020, 6:16 PM
GG128
0
Thanks Jayakrishna🇼🇳 ! Finally someone understood the issue. As I said earlier, it was giving me a compile error when I type the question as it was asked even before running it. I will report it. Thanks Sebastian Pacurar . Good tip that strings in decimal forms can’t be converted to int directly.
20th May 2020, 7:19 PM
GG128