Print twice "Enter the student's name" within the for [duplicate] cycle

1

I need you to ask the name of the students after having declared the number of students, but when entering the amount does not allow me to enter the name of the first student and I have one round of the cycle, for example: if I say that I have 5 students just let me write 4 and the first is empty How could you solve that?

 import java.util.Scanner;

public class Cali{
 public static void main(String args[]){

  int cantidad = 0;
  int calificacion = 0;
  String alumno = "";

  Scanner user = new Scanner(System.in);

  System.out.println("Ingrese la cantidad de alumnos");
  cantidad = user.nextInt();

  System.out.println("");

  int alumnos[] = new int[cantidad];
  int calificaciones[] = new int[cantidad];
  String nombres[] = new String[cantidad];

  for(int i = 0; i < alumnos.length; i++)
 {
   System.out.println("Ingrese el nombre del alumno: ");
   alumno = user.nextLine();
   nombres[i] = alumno;  
 }

  System.out.println("");

  for(int i = 0; i < alumnos.length; i++)
 { 
   System.out.print("Ingrse la calificacion del alumno(a) " + nombres[i] + ":");
   calificacion = user.nextInt();
   calificaciones[i] = calificacion;
 }

 System.out.println("");

  for(int i = 0; i < alumnos.length; i++)
 {
  if(calificaciones[i] <= 5)
   {
    System.out.println("El(La) alumno(a) " + nombres[i] + " reprobo con: " + calificaciones[i]);
   } else if(calificaciones[i] == 10) 
     {
    System.out.println("El(La) alumno(a) " + nombres[i] + " aprobo con una excelente calficacion de " + calificaciones[i]);
     } else
       {
    System.out.println("El(La) alumno(a) " + nombres[i] + " aprobo con: " + calificaciones[i]);
       }
 }

 }
}
    
asked by Gerardo 23.05.2018 в 03:02
source

2 answers

2

The problem is that when you request the number of students, then you call the user.nextInt() method to get that amount. So far all right, as long as you really enter a whole number and nothing else. Then you start to request the names of the students and to get them you use the user.nextLine() method, which the first time you call it will return an empty string. This happens because when you enter a number in the console and then give enter, this enter is translated into an end-of-line character that the user.nextInt() method ignores in its reading, but stays in the buffer and therefore when trying to read a new line, you get the empty string. Solving it is very simple, add a call to user.nextLine() just after the user.nextInt() .

System.out.println("Ingrese la cantidad de alumnos");
cantidad = user.nextInt();
user.nextLine();
    
answered by 23.05.2018 в 03:19
1

The problem is with .nextInt() . When you enter a number and press "Enter", .nextInt() only consumes the number and not the end of the line. The end of every line is \n . Therefore, when you execute .nextLine() you consume the "end of the line" and you will be ready to read the next String you need.

There are two ways to solve this, I explained how on the following link: Problem with String in Java using Scanner

Quick solution to your problem: Add an% extra% co, between .nextLine() and .nextInt() . The .nextLine() extra will consume the "end of the line" always.

Fixed code:

System.out.println("Ingrese la cantidad de alumnos");
cantidad = user.nextInt();

// Consume "\n"
user.nextLine();
    
answered by 23.05.2018 в 03:54