#Please help, weird thing with java scanner
16 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @open relic! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
private static int readInt() {
while (true) {
try {
print(" Ingresar cantidad de alumnos: ");
return reader.nextInt();
} catch (InputMismatchException e) {
System.out.println(" \u001B[31m El valor debe ser un numero entero.\u001B[0m");
}
}
}
private static void registerStuds(int max) {
students = new String[max];
grades = new double[max];
String line;
for (int i = 0; i < max; i++) {
while (true) {
try {
print(" Ingresar Alumno: ");
line = reader.nextLine();
if (line.matches("[A-Za-z ]+")) {
students[i] = line;
try {
print(" Ingresar Nota: ");
grades[i] = reader.nextDouble();
break;
} catch (InputMismatchException e) {
println(" \u001B[31m El valor debe ser un numero decimal.\u001B[0m");
}
} else {
println(" \u001B[31m El valor debe ser alfabetico.\u001B[0m");
}
} catch (NoSuchElementException e) {
println(" \u001B" + e.getMessage() + "\u001B[0m");
}
}
}
}
I don't understand why this is being triggered
That should only happen if I input something wrong
But i didn't input anything
Only the "4" in readInt()
But when it loads registerStuds() that happens
This was not happening some moments before and i don't know what could it be
And this keeps happening
Wait
Nevermind
im so dumb
I moved the reader = new Scanner(System.in); from the loop to test if it would behave normally and i forgot
aagg