Why this thing getting errors ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
3rd Jun 2019, 12:26 PM
Anuj Raghu
Anuj Raghu - avatar
7 Answers
+ 1
import java.util.Scanner ; public class Program { public static void main(String[] args) { try{ Scanner input = new Scanner(System.in); int num = input.nextInt(); System.out.println(num); String name = input.next(); System.out.println(name); } catch(Exception e){ System.out.println("There is an error"); } } } (Correct Code) Some tips: - You need only one Scanner - Use next(), nextLine() sometimes triggers errors, because it want to read a line and not only one word
3rd Jun 2019, 12:36 PM
Kevin
Kevin - avatar
+ 1
:D
3rd Jun 2019, 12:40 PM
Kevin
Kevin - avatar
+ 1
try(Scanner input = new Scanner(System.in)) { int num = input.nextInt(); String name = input.next(); System.out.println(num); System.out.println(name); } catch(Exception e) { System.out.println("There is an error"); }
3rd Jun 2019, 12:47 PM
Jake
Jake - avatar
+ 1
my phone said this had no answers, my bad haha
3rd Jun 2019, 12:47 PM
Jake
Jake - avatar
0
Remove useless out Scanner and use for all input. You don't need to declare two Scanners. This is the main reason as i researched
3rd Jun 2019, 12:36 PM
DolphieDude
DolphieDude - avatar
0
Also replace nextLine() with next(). Try it out
3rd Jun 2019, 12:38 PM
DolphieDude
DolphieDude - avatar
0
Kevin your answer is better but i was faster ;( hahahhah
3rd Jun 2019, 12:39 PM
DolphieDude
DolphieDude - avatar