-
Java Scanner help
So i was reading the posts on my latest thread (is Eclipse nessesery?) and i downloaded eclipse and have been working on Java for about 2 months now but i ran into a problem, so i want player to input a integer but the Scanner isn't working. Here's the code:
Code:
import java.util.Scanner;
public class Character_Creation {
public static void main(String[] args) {
Scanner name = new Scanner(System.in);
Scanner a = new Scanner(System.in);
int age = a.hasNextInt();
if (age < 18) {
System.out.println("Too young!");
}else{
System.out.println("Welcome " + name.nextLine());
}
}
}
-
Code:
import java.util.Scanner;
public class Character_Creation {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String name;
System.out.print("Enter your name: ");
name = scan.next();
System.out.print("Test this age: ");
int age;
try{
age = scan.nextInt();
if (age < 18) {
System.out.println("Too young!");
}else{
System.out.println("Welcome "+name.toString());
}
}catch(Exception e){
System.out.println("User failed to enter a number.");
}
}
}