Why is my String names accepting integers instead of letters?

1
import java.util.Scanner;

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

      final int qtdAlunos = 5;
      String [] nomes = new String[qtdAlunos];
      double[] av1 = new double[qtdAlunos];
      double[] av2 = new double[qtdAlunos];

      int indiceAluno = 0;

    boolean continuar = true;


    do{


  Scanner s = new Scanner(System.in);
    System.out.println("---Bem Vindo---");
    System.out.println("1. Cadastrar Alunos");
    System.out.println("2. Buscar Aluno");
    System.out.println("3. Consultar Notas");
    System.out.println("4. Sair");  

    String opcao= s.nextLine();

    switch (opcao) {

    case "1":
       if (indiceAluno < qtdAlunos){
           System.out.println("Opcao 1 escolhida.");       
           Scanner t= new Scanner(System.in);

           System.out.println("Digite o codigo do aluno.");
           //int codigo = t.nextInt();

           System.out.println("Digite o nome do Aluno.");       
           nomes[indiceAluno]= t.nextLine();
           System.out.println("Digite a nota av1:");
           av1[indiceAluno] = t.nextDouble();
           System.out.println("Digite a nota av2:");
           av2 [indiceAluno]= t.nextDouble();
           System.out.println(" nota do aluno"+ indiceAluno);
           indiceAluno = indiceAluno  + 1;
        }
    break;

    case "2":

    Scanner s2 = new Scanner(System.in);
    int indice = s2.nextInt();
      for (int i = 0;i < nomes.length; i++){
      if(indiceAluno <qtdAlunos){
      System.out.println("Nome do aluno: " + nomes[indice]);
      System.out.println("Nota do Av1: " + av1[indice]);
      System.out.println("Nota do Av2: " + av2[indice]);

      }else{
      System.out.println("");
    }
}
    
asked by anonymous 08.11.2016 / 20:14

1 answer

1

You have to use next() in reading the name instead of nextLine() . And also in option 2 you have to use the variable i instead of indice in System.out.println of the student. The code is incomplete for what I checked, but this piece would look like this:

public class Tp1 {

  public static void main(String[] args) {

    final int qtdAlunos = 5;
    String[] nomes = new String[qtdAlunos];
    double[] av1 = new double[qtdAlunos];
    double[] av2 = new double[qtdAlunos];

    int indiceAluno = 0;

    boolean continuar = true;

    do {

      Scanner s = new Scanner(System.in);
      System.out.println("---Bem Vindo---");
      System.out.println("1. Cadastrar Alunos");
      System.out.println("2. Buscar Aluno");
      System.out.println("3. Consultar Notas");
      System.out.println("4. Sair");

      String opcao = s.nextLine();

      switch (opcao) {

        case "1":
          if (indiceAluno < qtdAlunos) {

            System.out.println("Opcao 1 escolhida.");
            Scanner t = new Scanner(System.in);

            System.out.println("Digite o codigo do aluno.");
            int codigo = t.nextInt();
            System.out.println("Digite o nome do Aluno.");
            nomes[indiceAluno] = t.next();
            System.out.println("Digite a nota av1:");
            av1[indiceAluno] = t.nextDouble();
            System.out.println("Digite a nota av2:");
            av2[indiceAluno] = t.nextDouble();

            System.out.println(" nota do aluno" + indiceAluno);
            indiceAluno = indiceAluno + 1;
          }
          break;

        case "2":

          Scanner s2 = new Scanner(System.in);
          int indice = s2.nextInt();
          for (int i = 0; i < nomes.length; i++) {
            if (indiceAluno < qtdAlunos) {
              System.out.println("Nome do aluno: " + nomes[i]);
              System.out.println("Nota do Av1: " + av1[i]);
              System.out.println("Nota do Av2: " + av2[i]);

            } else {
              System.out.println("");
            }
          }
      }

    } while (true);
  }
}
    
08.11.2016 / 20:33