Reputation: 15
I am trying to check if my scanner has multiple tokens before separating the tokens. I am currently trying to use scanner.hasNext() (my scanner's name is sc). If the user inputs "string int int" then I don't want to print "enter age" or "enter grade"
System.out.println("Enter name or command: ");
name = sc.next();
if(name.toLowerCase().equals("next")) {
students.remove(0);
}else if(name.toLowerCase().equals("end")) {
running = false;
}else {
//This is where it gets dicey
if(!sc.hasNext())
System.out.println("Enter age: ");
age = sc.nextInt();
if(!sc.hasNext())
System.out.println("Enter grade: ");
grade = sc.nextInt();
if(first) {
students.add(new Student(name, age, grade));
first = false;
}else {
addStudent(new Student(name, age, grade), students.get(students.size()-1));
}
}
Currently, my program skips past the if statements no matter the input. If I remove the !s then the lines print no matter what, even if input is a single line. They also seem to print after the nextInt is taken which I found to be odd. I would be very grateful for some help!
Upvotes: 0
Views: 2301
Reputation: 3166
I had to make some modifications, as it wouldn't compile. When i ran it, it gave me what you're saying you want to have:
C:\Users\..snip..\stackOverflow>java src\stackOverflowTest.java
Enter name or command:
Peter 78 34
addStudent(new Student(name, age, grade), students.get(students.size()-1));
However, if I only put in one parameter, it just hangs (in accordance to the javaDoc: "This method may block while waiting for input to scan."):
C:\Users\..snip..\stackOverflow>java src\stackOverflowTest.java
Enter name or command:
hello
This is the compilable modification:
import java.util.Scanner;
public class StackOverflowTest {
static String name;
static int age;
static int grade;
static boolean first;
static boolean running;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter name or command: ");
name = sc.next();
if(name.toLowerCase().equals("next")) {
System.out.println("students.remove(0);");
// students.remove(0);
}else if(name.toLowerCase().equals("end")) {
running = false;
}else {
//This is where it gets dicey
if(!sc.hasNext())
System.out.println("Enter age: ");
age = sc.nextInt();
if(!sc.hasNext())
System.out.println("Enter grade: ");
grade = sc.nextInt();
if(first) {
System.out.println("students.add(new Student(name, age, grade));");
// students.add(new Student(name, age, grade));
first = false;
}else {
System.out.println("addStudent(new Student(name, age, grade), students.get(students.size()-1));");
// addStudent(new Student(name, age, grade), students.get(students.size()-1));
}
}
}
}
If you want to keep all options open, you may do something like this (note there's no exception handling here):
import java.util.Scanner;
public class StackOverflowTest {
static String name = "";
static int age = -1;
static int grade = -1;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter name or command: ");
String[] input = sc.nextLine().split(" ");
switch (input.length) {
case 3: grade = Integer.valueOf(input[2]);
// fallthrough
case 2: age = Integer.valueOf(input[1]);
// fallthrough
case 1: name = input[0];
};
if (name.isEmpty()) {
System.out.println("Enter name: ");
name = sc.next();
}
if (age == -1) {
System.out.println("Enter age: ");
age = sc.nextInt();
}
if (grade == -1) {
System.out.println("Enter grade: ");
grade = sc.nextInt();
}
}
}
Upvotes: 1