+ 1
Why this thing getting errors ?
7 Respostas
+ 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
+ 1
:D
+ 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");
}
+ 1
my phone said this had no answers, my bad haha
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
0
Also replace nextLine() with next(). Try it out
0
Kevin your answer is better but i was faster ;( hahahhah