Mr E
Mr E

Reputation: 33

Getting errors and cannot see the mistake creating a console menu

This is the code producing 2 errors (I apologise as this is my first time using this site and not completely sure how to do everything but the errors have been flagged at the m of menu in the first error and the s of scanchoice of the second error)

java:22: error: cannot find symbol choiceentry = menu();

java:52: error: cannot find symbol choiceentry = scanchoice.nextInt();

import java.util.*;
import java.io.*;

public class Student
{

    public static void main(String[] args)
    {

        int choiceentry;
        Scanner input = new Scanner(System.in);

        choiceentry = menu();
            while (choiceentry != 6)
            {
                if(choiceentry == 1) 
                {
                // ..do something
                }
                else if(choiceentry == 2)
                {
                //..something else
                }
                else if(choiceentry == 3)
                {
                //...something else
                }
                else if(choiceentry == 4)
                {
                // ..something else
                }
                else if(choiceentry == 5)
                {
                //..something else
                }
                else if(choiceentry == 6)
                {
                System.exit(0);
                }
                else
            {
            System.out.println("Enter \"1\", \"2\", \"3\", \"4\", \"5\" or \"6\"");
            choiceentry = scanchoice.nextInt();
            }
        }


    }
}

This is the code I have used to setup the menu and it is building fine

import java.util.*;
import java.io.*;

public class Enroll
{

//Creation of Console Menu
    public static int menu()
        {
        int selection;
        Scanner input = new Scanner(System.in);

        /***************************************************/

        System.out.println("Please Select an Option:");
        System.out.println("-------------------------");
        System.out.println("0 - Input Course Details");
        System.out.println("1 - Search");
        System.out.println("2 - Add Student");
        System.out.println("3 - Delete Student");
        System.out.println("4 - Report (Percentage of M & F Students)");
        System.out.println("5 - Save");
        System.out.println("6 - Quit");

        selection = input.nextInt();
        return selection;    
        }
//End Menu
}

Upvotes: 0

Views: 101

Answers (1)

Daniel Nugent
Daniel Nugent

Reputation: 43322

Edit: Good catch @Tom, I just added a modification to the answer that should take care of the != 6 case.

You need to access the menu() function through the Enroll class like this:

choiceentry = Enroll.menu();

Note that your only method menu() in the Enroll class is static, so you don't need to create an instance of the Enroll class.

This line:

choiceentry = scanchoice.nextInt();

Should be this in order to use the Scanner reference:

choiceentry = input.nextInt();

One more thing, it looks like you want the user to be able to enter multiple commands until they choose to quit. Take a look at the use of the hasAnswered flag in the code below.

Code with changes:

import java.util.*;
import java.io.*;

public class Student
{

    public static void main(String[] args)
    {

        int choiceentry;
        Scanner input = new Scanner(System.in);

        choiceentry = Enroll.menu(); //Access through the Enroll class
        if(choiceentry == 6)
        {
             //Exit if user entered 6
             System.exit(0);
        }

          while (true) //I hate to put while(true) in code, but it seems appropriate here
          {
                boolean hasAnswered = false; //use a flag to determine if the user entered a valid command
                if(choiceentry == 1) 
                {
                  hasAnswered = true;
                // ..do something
                }
                else if(choiceentry == 2)
                { 
                  hasAnswered = true;
                //..something else
                }
                else if(choiceentry == 3)
                {
                  hasAnswered = true;
                //...something else
                }
                else if(choiceentry == 4)
                {
                  hasAnswered = true;
                // ..something else
                }
                else if(choiceentry == 5)
                {
                  hasAnswered = true;
                //..something else
                }
                else
                {
                  System.out.println("Enter \"1\", \"2\", \"3\", \"4\", \"5\" or \"6\"");
                  choiceentry = input.nextInt(); //use the Scanner

                  if(choiceentry == 6)
                  {
                     //Exit if user entered 6
                     System.exit(0);
                  }
                }

                if (hasAnswered == true){
                    hasAnswered == false;
                    //user had issued a valid command, prompt for the next command
                    choiceentry = Enroll.menu(); //Access through the Enroll class
                    if(choiceentry == 6)
                    {
                       //Exit if user entered 6
                       System.exit(0);
                     }
                }
        }


    }
}

Upvotes: 1

Related Questions