Boss, something weird is happening

  • 2
  • Locked
Other people's code is unknown territory. What awaits you there? Perhaps enlightened wolves who chant mantras, or bloodthirsty deer ready to tear your throat open... We don't want to frighten you, but we must warn you that other people's code is full of surprises. Let's dig into the program. In theory, it should sum up two numbers. Let's see what's wrong with it.
You can't complete this task, because you're not signed in.
Comments (6)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Joe M
Level 47 , Owings Mills, United States
17 October 2020, 18:03
both vars read as strings. use parseInt on both, save as new variables and then sum them
Brandon Leirer
Level 7 , Keller, United States
8 June 2020, 18:36
Can anyone explain why we keep using Bufferedreader for these scenarios instead of Scanner? According to Head First Java, and a lot of other places I have checked, Scanner is the newer method that can read int and string without conversion. Bufferedreader only makes sense to use when reading entire files at once because it has a larger buffer than Scanner. But it seems silly to keep having to convert string to numbers when there is a newer more efficient way available.
EugeneEmory
Level 8 , San Antonio, United States
3 March 2021, 16:08
I'm assuming they want us to learn the hard way. You can also bypass it and use Scanner. In the field you're going to use Scanner mostly anyway because it's more flexible and writes cleaner code
Christian Djanea
Level 7 , Mount Laurel, United States
20 September 2019, 12:21
The input from the keyboard is read as a String so you have to convert it to an integer before you do the math. I believe that is the goal of the exercise.
Joe M
Level 47 , Owings Mills, United States
17 October 2020, 18:04
yes this is right, thank you
Marlin Johnson
Level 9 , Bloomington, United States
23 May 2019, 11:17
Need some clear understanding with read() method. I have modified the code as above, but the original code is as follows, int a = reader.readLine(); int b = reader.read(); int sum = a + b; System.out.println("Sum = " + sum); have some doubt while running the original code, 1. I used Integer.parseInt() for int a but while using for int b, it is showing error as int cannot be converted to String 2.When using reader.read() for both the int a, b with 0, 0 as input getting 48 as sum, Why?