What's wrong?

+2 votes
asked Oct 22 by Yan Victor Brito Gomes (190 points)
import java.util.Scanner;

public class Main
{
    public static void main(String[] args) {
        int conf, idade[] = new int[5];
        String nomes[] = new String[5];
        
        Scanner ler = new Scanner(System.in);
        
        for (int i=0; i < 5; i++) {
            System.out.println("Digite aqui o nome: ");
            nomes[i] = ler.nextLine();
            System.out.println("Digite aqui sua idade: ");
            idade[i] = ler.nextInt();
        }
        
        System.out.println("Digite qual dos usuários deseja saber: (0-4)");
        conf = ler.nextInt();
        
        while(conf < 0 || conf > 4) {
            System.out.println("Posição inválida, digite novamente");
            conf = ler.nextInt();
        }
        
        System.out.println("=====================");
        System.out.println("Nome: " + nomes[conf]);
        System.out.println("Idade: " + idade[conf]);
    }
}

I tried to do this some days ago and I can't, before the second scanner the code skip a scanner and after the program crashes.

3 Answers

+1 vote
answered Oct 23 by Peter Minarik (12,220 points)
selected Oct 23 by Yan Victor Brito Gomes
 
Best answer

The Problem

Scanner.nextInt() reads the input as long as it is reading a number. And then stops reading any longer. This means, it will not read the end of line characters.
The next time your call Scanner.nextLine() (to read the name), it detects that the input stream has content (after the number, which had been just read before) and it returns this data. It wouldn't even ask the user to start entering input.

The Solution

The idea here is not to try to parse the input stream for an int, but rather read everything the user entered and store it in a String. After that, parse the number from this line entered by the user:
idade[i] = Integer.parseInt(ler.nextLine());

Disclaimer

I'm not a Java programmer. There may be other solutions for the problem.

0 votes
answered Oct 23 by Bruna Logatti (180 points)
import java.util.Scanner;

public class Main
{
    public static void main(String[] args) {
        int conf, idade[] = new int[5];
        String age;
        String nomes[] = new String[5];
        
        Scanner ler = new Scanner(System.in);
        
        for (int i=0; i < 5; i++) {
            System.out.println("Digite aqui o nome: ");
            nomes[i] = ler.nextLine();
            System.out.println("Digite aqui sua idade: ");
            age = ler.nextLine();
            idade[i] = Integer.parseInt(age);
        }
        
        System.out.println("Digite qual dos usuários deseja saber: (0-4)");
        conf = ler.nextInt();
        
        while(conf < 0 || conf > 4) {
            System.out.println("Posição inválida, digite novamente");
            conf = ler.nextInt();
        }
        
        System.out.println("=====================");
        System.out.println("Nome: " + nomes[conf]);
        System.out.println("Idade: " + idade[conf]);
    }
}
0 votes
answered Oct 23 by Bruna Logatti (180 points)

Olá Yan, as vezes a leitura através do nextInt dá problema, principalmente no Java. Sempre tente ler resultados em formato de String e depois convertê-los, foi isso que fiz. Espero poder ter te ajudado. laugh

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...