Java. how to display a string array from a menu option?

Asked

Viewed 314 times

0

I created 3 vectors, one for name, and the others for day and month of birth.

I fill the 3 through the first menu option I made with the switch case, the second option would be to display only the names of those who are registered (inserted) in the array of names: agenda, but it does not work, but when it is outside the conditional structure it works.

When I put in any structure the vector is not displayed, as if it were empty. What do?

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author lab1
 */
import java.util.Scanner;
public class q3 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner scan = new Scanner(System.in);
          int tecla = 1;
          while (tecla != 27){

          System.out.println("**********Agenda de aniversariantes**************");

        System.out.println("1.Cadastrar pessoa na agenda");
        System.out.println("2.Excluir pessoa a partir do nome");
        System.out.println("3.Consultar aniversariantes de uma data (dia e mês)");
        System.out.println("4.Consultar aniversariantes por mês");
        System.out.println("5.Consultar aniversariantes pela letra inicial do nome");
        System.out.println("6.Mostrar toda a agenda ordenada pelo nome");
        System.out.println("7.Mostrar toda a agenda ordenada pelo mes");
        System.out.println("8.Sair");
        System.out.println("A agenda pode suportar até 10 pessoas");


        String agenda[] = new String[10];
        int dia[] = new int[10];
        int mes[] = new int[10];
        System.out.print("Digite uma opção:");
        int opcao = scan.nextInt();

        switch(opcao){
            case 1:
                int i = 0;

               // if(i < 10){
                    System.out.print("Digite o nome: ");
                    agenda[i] = scan.next();
                    System.out.print("Digite o dia do aniversario: ");
                    dia[i] = scan.nextInt();
                    System.out.print("Digite o mes do aniversario: ");
                    mes[i] = scan.nextInt();
                i++;
                //}
               // else
                 //   System.out.println("Agenda cheia");
            break;   
            case 2:
                int j;
                for (j=0;j<agenda.length;j++)
                if(agenda[0] == null)
                    System.out.println("Agenda vazia");
                else{
                    System.out.println("Agenda de aniversários");
                    for (j=0;j<agenda.length;j++){
                        System.out.println(agenda[j]);
                        System.out.println(dia[j]);
                        System.out.println(mes[j]);
                }
                }
            break;    
    }

}
}
}

1 answer

0

The error happens because you instantiate the vectors every time you rotate the while.

Follow the correction, made by bringing the lines of the vectors above in the code:

package br.com.alura.teste;

import java.util.Scanner;

public class q3 {

    public static void main(String[] args) {
        // TODO code application logic here
        Scanner scan = new Scanner(System.in);
        int tecla = 1;

        //Preste atenção aqui: 
        String agenda[] = new String[10];
        int dia[] = new int[10];
        int mes[] = new int[10];
        while (tecla != 27) {

            System.out.println("**********Agenda de aniversariantes**************");

            System.out.println("1.Cadastrar pessoa na agenda");
            System.out.println("2.Excluir pessoa a partir do nome");
            System.out.println("3.Consultar aniversariantes de uma data (dia e mês)");
            System.out.println("4.Consultar aniversariantes por mês");
            System.out.println("5.Consultar aniversariantes pela letra inicial do nome");
            System.out.println("6.Mostrar toda a agenda ordenada pelo nome");
            System.out.println("7.Mostrar toda a agenda ordenada pelo mes");
            System.out.println("8.Sair");
            System.out.println("A agenda pode suportar até 10 pessoas");

            System.out.print("Digite uma opção:");
            int opcao = scan.nextInt();

            switch (opcao) {
            case 1:
                int i = 0;

                // if(i < 10){
                System.out.print("Digite o nome: ");
                agenda[i] = scan.next();
                System.out.print("Digite o dia do aniversario: ");
                dia[i] = scan.nextInt();
                System.out.print("Digite o mes do aniversario: ");
                mes[i] = scan.nextInt();
                i++;
                // }
                // else
                // System.out.println("Agenda cheia");
                break;
            case 2:
                int j;
                for (j = 0; j < agenda.length; j++)
                    if (agenda[0] == null)
                        System.out.println("Agenda vazia");
                    else {
                        System.out.println("Agenda de aniversários");
                        for (j = 0; j < agenda.length; j++) {
                            // E aqui também fiz uma pequena alteração
                            if(null != agenda[j]) {
                                System.out.println(agenda[j]);
                                System.out.println(dia[j]);
                                System.out.println(mes[j]);
                            }
                        }
                    }
                break;
            }

        }
    }
}
  • It worked, but when I add another person, it takes the place of the person previously registered

  • int i = 0;. It is your code error, not your doubt, change this line to some integer that counts the number of users entered.

Browser other questions tagged

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