Andrew Andrew - 1 month ago 7
Java Question

Java - prompting user to input commands and throw error if unrecognizable

I am trying to write a code that will prompt the user to enter some instructions. If the user inputs the command "echo" + word, it will be displaying the word itself on the next line. Then the prompt will appear again waiting for another input.

The program should display an error if the command that was entered is unknown. I'm having issues here as the program is not displaying the error message.

In addition, if the user does not type anything and just press enter, it should just simply display again the prompt on the next line, however it's not doing it.

Hopefully, you can help me..

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

public class Prompter {

public static void main(String[] args) {

Scanner sc = new Scanner (System.in);

String sInstruct, sTerm;


System.out.print("Enter:> ");
sInstruct = sc.next();
sTerm = sc.nextLine();

try {
if (sInstruct.equals("")){
while(sInstruct.equals(""))
{
System.out.print("Enter:> ");
sInstruct = sc.next();
}

} else if (sInstruct.equals("echo")){
while (sInstruct.equals("echo"))
{
sayWord(sInstruct, sTerm);
System.out.print("Enter:> ");

sInstruct = sc.next();
sTerm = sc.nextLine();
}

}
}
catch(Exception error){
System.out.print("Invalid command " + sInstruct);
}

sc.close();
}
public static void sayWord (String sInstruct, String sTerm){

System.out.println(sTerm);
}
}


Output should be:

Enter:> echo hello brown fox
hello brown fox
Enter:>
Enter:>
Enter:> eccoh hello
Invalid command eccoh
Enter:>

Answer

I see some problems in your code:

  • Using sInstruct and sTerm at the same time is an overkill, you should use only sTerm since it will contain the complete instruction
  • The while loop must be outside the if conditions and should check for a sc.hasNextLine()
  • To check if the entered string is empty, you should do it using sTerm.isEmpty()
  • To check if the entered string starts with echo, you should do it using sTerm.startsWith("echo")
  • The check for an invalid instruction, must be set inside the while loop.
  • The try-catch clause is not needed.

See the proposed solution:

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

public class Prompter {

    public static void main(String[] args) {

        Scanner sc = new Scanner (System.in);
        String sTerm;

        System.out.print("Enter:> ");

        while(sc.hasNextLine()) {
            sTerm = sc.nextLine();
            if(sTerm.isEmpty()) {
            } else if (sTerm.startsWith("echo")) {
                sayWord(sTerm.substring(5));
            } else {
                System.out.println("Invalid command " + sTerm.split(" ")[0]);
            }
            System.out.print("Enter:> ");
        }

        sc.close();
    }

    public static void sayWord (String sTerm){

        System.out.println(sTerm);
    }
}

Or, if you prefer, the if-else clauses could be even more compacted:

if (sTerm.startsWith("echo")) {
    sayWord(sTerm.substring(5));
} else if(!sTerm.isEmpty()) {
    System.out.println("Invalid command " + sTerm.split(" ")[0]);
}
Comments