0
    public static void main(String[] args)
{
    CollegeTester ct = new CollegeTester();
    ct.getCommand();//goes to command
}   

//Ask user for a command
public void getCommand()
{
    String command = "";
    System.out.println("Enter a command: ");
    command = input.nextLine();
        if(command.equals("add"))
            addCommand();//If command is add go to addCommand
        if(command.equals("course"))
            courseCommand();//If command is course go to courseCommand
        if(command.equals("find"))
            findCommand();
        if(command.equals("remove"))
            removeCommand();
        if(command.equals("highest"))
            highestCommand();
        if(command.equals("login"))
            loginCommand();
        if(command.equals("quit"))
            System.exit(1);
}

I want to repeat the getCommand() method until the user types in quit but it always fails

  1. i tried to put getCommand() at the end of each other method & if statements it won't repeat

  2. i tried to do a while loop for everything but if someone accidently types mistypes quit the program would never end

How would i effectively go back to this method until the user wishes to quit?

user3249265
  • 113
  • 1
  • 5
  • 16

2 Answers2

2

I would do it one of two ways.

public static void main(String[] args)
{
    CollegeTester ct = new CollegeTester();
    ct.getCommand();//goes to command

}  

//Ask user for a command
public void getCommand()
{
    boolean exit = false;
    while(!exit){
        String command = "";
        System.out.println("Enter a command: ");
        command = input.nextLine();
            if(command.equals("add")){
                addCommand();//If command is add go to addCommand
            }else if(command.equals("course")){
                courseCommand();//If command is course go to courseCommand
            }else if(command.equals("find")){
                findCommand();
            }else if(command.equals("remove")){
                removeCommand();
            }else if(command.equals("highest")){
                highestCommand();
            }else if(command.equals("login")){
                loginCommand();
            }else if(command.equals("quit")){
                exit = true;
            }else {
                System.out.println("Not valid command, try again.");
            }
    }

}



//Ask user for a command
public void getCommand()
{
    while(true){
        String command = "";
        System.out.println("Enter a command: ");
        command = input.nextLine();
            if(command.equals("add"))
                addCommand();//If command is add go to addCommand
            if(command.equals("course"))
                courseCommand();//If command is course go to courseCommand
            if(command.equals("find"))
                findCommand();
            if(command.equals("remove"))
                removeCommand();
            if(command.equals("highest"))
                highestCommand();
            if(command.equals("login"))
                loginCommand();
            if(command.equals("quit"))
                System.exit(0);  // or could use 'break' here (thanks Mikkel)
    }

}

Edit: Corrected System.exit(1). Thanks Mikkel.

AnxGotta
  • 1,006
  • 7
  • 28
  • Alternatively you can just `break` out of the `while(true)` loop. Also `System.exit()` should be called with 0, to indicate a clean exit. – Mikkel Løkke Jan 29 '14 at 14:40
1

Or you can place it inside a while loop:

while(true) {
    getCommand();
}

Also, check out this question on System.exit(int), you are effectively saying that the application should close because of an error. When it's intended to close, you ought to pass 0, not 1.

In fact, if you use a while loop like above, I might just escape the loop through a return false; or something and let the program end naturally.

Community
  • 1
  • 1
Reinstate Monica
  • 2,767
  • 3
  • 31
  • 40