Why is my String names accepting whole numbers instead of letters?

Asked

Viewed 86 times

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("");
    }
}
  • A String accepts numeric values, but will treat them as text. Example 1 != "1"

  • when I try to put when I try to type the name in the console for example Matt, it gives an error as if" name" is a variable of type int

  • 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 the student’s System.out.println

  • thank you very much , I had confused myself with the name of the variable

  • Has any response helped solve the problem and can address similar questions from other users? If yes, do not forget to mark the answer as accepted. To do this just click on the left side of it).

1 answer

1

You have to use next() reading the name instead of nextLine(). And also in option 2 you have to use the variable i instead of indice us System.out.println student. The code is incomplete from what I checked, but that 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);
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.