I recently ran into some trouble with Java's Scanner. When calling methods such as nextInt()
, it doesn't consume the newline
after pressing Enter. My solution is:
package scannerfix;
import java.util.Scanner;
public class scannerFix {
static Scanner input = new Scanner(System.in);
public static int takeInt() {
int retVal = input.nextInt();
input.nextLine();
return retVal;
}
public static void main(String[] args) {
int num = scannerFix.takeInt();
String word = input.nextLine();
System.out.println("\n" + num + "\t" + word);
}
}
It works, but here's my question: Is this in any way bad?