Options menu in the JAVA console

Asked

Viewed 3,147 times

-1

Hello, I’m new to the language, I’m an intern at a programming company and my boss asked me to perform a small program to exercise my learning, asked me to make a menu on the console in java.

-each item needs to have a description, example: entries -each item can have other items, such as people, products. -menu items are conditioned to be displayed only when the user is allowed to do so. -the challenge is to create the program so that it is possible to insert the menu structure and the program to assemble it according to the informed structure. -a structure is also required to indicate which items the user has access to.

I realized the menu so far, the first difficulty is that I choose an option and the program ends, I need it to continue working to select other options, I’m trying to save what the person type on the keyboard for an arraylist in case the registrations, the permission part I have no idea how to do, someone give me a light please kkkk

my code until then:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

        Scanner menu = new Scanner (System.in);

        System.out.print("##--Teste Estrutura de Menu--##\n\n");
        System.out.print("|-----------------------------|\n");
        System.out.print("| Opção 1 - Novo Cadastro     |\n");
        System.out.print("| Opção 2 - Clientes          |\n");
        System.out.print("| Opção 3 - Produtos          |\n");
        System.out.print("| Opção 4 - Sair              |\n");
        System.out.print("|-----------------------------|\n");
        System.out.print("Digite uma opção: ");

        int opcao = menu.nextInt();


        switch (opcao) {
        case 1:
            System.out.print("\nOpção Novo Cadastro Selecionado");
            break;

        case 2:
            System.out.print("\nOpção Clientes Selecionado\n");
            break;

        case 3:
            System.out.print("\nOpção Produtos Selecionado\n");
            break;

        default:
            System.out.print("\nOpção Inválida!");
            break;

        case 4:
            System.out.print("\nAté logo!");
            menu.close();       
        }
    }
}

Then I found out I had to wear a loop, I think the best would be the do.. while in my case, however I am unable to implement in my code.

package programaJava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

            Scanner menu = new Scanner (System.in);

            do {
            System.out.print("##--Teste Estrutura de Menu--##\n\n");
            System.out.print("|-----------------------------|\n");
            System.out.print("| Opção 1 - Novo Cadastro     |\n");
            System.out.print("| Opção 2 - Clientes          |\n");
            System.out.print("| Opção 3 - Produtos          |\n");
            System.out.print("| Opção 4 - Sair              |\n");
            System.out.print("|-----------------------------|\n");
            System.out.print("Digite uma opção: ");

            int opcao = menu.nextInt();

            if (opcao == 4) {
                System.out.print("\nAté logo!");
                menu.close(); 
            }

            switch (opcao) {
            case 1:
                System.out.print("\nOpção Novo Cadastro Selecionado");
                break;

            case 2:
                System.out.print("\nOpção Clientes Selecionado\n");
                break;

            case 3:
                System.out.print("\nOpção Produtos Selecionado\n");
                break;

            default:
                System.out.print("\nOpção Inválida!");
                break;
        }
    }
}
  • What do you want? https://answall.com/q/146875/101 Or this? https://answall.com/q/390450/101

  • menu.close() closes the Scanner, which in turn closes the System.in. But you should not close the System.in, it is a "special" resource, managed by JVM and once closed, you can’t reopen it.

1 answer

0

Put a "while(true)" in place of the "do", or a "while" with condition.

The code stays like this:

/*
 * 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.
 */
package javaapplication8;

import java.util.Scanner;

/**
 *
 * @author Matheus Markies
 */
public class JavaApplication8 {

    public static void main(String[] args) {

            Scanner menu = new Scanner (System.in);

            while (true) {            

            System.out.print("##--Teste Estrutura de Menu--##\n\n");
            System.out.print("|-----------------------------|\n");
            System.out.print("| Opção 1 - Novo Cadastro     |\n");
            System.out.print("| Opção 2 - Clientes          |\n");
            System.out.print("| Opção 3 - Produtos          |\n");
            System.out.print("| Opção 4 - Sair              |\n");
            System.out.print("|-----------------------------|\n");
            System.out.print("Digite uma opção: ");

            int opcao = menu.nextInt();

            if (opcao == 4) {
                System.out.print("\nAté logo!");
                menu.close(); 
            }

            switch (opcao) {
            case 1:
                System.out.print("\nOpção Novo Cadastro Selecionado");
                break;

            case 2:
                System.out.print("\nOpção Clientes Selecionado\n");
                break;

            case 3:
                System.out.print("\nOpção Produtos Selecionado\n");
                break;

            default:
                System.out.print("\nOpção Inválida!");
                break;
        }
    }
 }

}
  • Type 4, you close the Scanner but it doesn’t come out of while, and in the next iteration nextInt() will try to read from Scanner closed and error: https://ideone.com/iPIikH

  • And just add a break;

  • Then edit the answer and do it. Responding with code that doesn’t work is not a good...

Browser other questions tagged

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