Evening guys, I'm doing a project for uni and I find myself in kind of a fuss. I'm having trouble with my command interpreter. I can get the code to run, but unfortunately it's not reading my input right. Could someone point me in the right direction towards approaching the error ? The code follows below, ignore the comments and the instances of other supporting classes. Any help would be appreciated. Thanks in advance !
import java.util.Scanner;
public class Main {
//Constantes que definem os comandos
public static final String REGISTER = "register";
public static final String LISTUSERS = "listusers";
public static final String UPLOAD = "upload";
public static final String READ = "read";
public static final String WRITE = "write";
public static final String GRANT = "grant";
public static final String REVOKE = "revoke";
public static final String USERDOCS = "userdocs";
public static final String TOPLEAKED = "topleaked";
public static final String TOPGRANTERS = "topgranters";
public static final String HELP = "help";
public static final String EXIT = "exit";
//Constantes que definem as mensagens para o utilizador
public static final String EXIT_COMM = "Bye!";
public static final String REGISTER_COMM = " was registered.";
public static final String ALREADY_REG_ERROR = " is already assigned to another user.";
public static final String NO_LIST_USERS = "There are no registered users.";
public static final String DOC_UPLOADED = " was uploaded.";
public static final String OFFICIAL_DOC = "Contact upda1ted.";
public static final String TOPSECRET_DOC = "Contact book empty.";
public static final String CONFIDENTIAL_DOC = "Phone number does not exist.";
public static final String CONTACT_SHARE_PHONE = "There are contacts that share phone numbers.";
public static final String HELP_COMM = "register - registers a new user /n";
public static void main(String[] args) {
System.out.println("Welcome!");
//WeKeepSecrets app = new WeKeepSecretsClass();
Scanner in = new Scanner(System.in);
String comm = getCommand(in);
while (!comm.equals(EXIT)){
switch (comm) {
case HELP:
System.out.println(HELP);
default:
System.out.println("ERRO");
break;
}
comm = getCommand(in);
}
System.out.println(EXIT_COMM);
in.close();
}
private static String getCommand(Scanner in) {
String input;
input = in.nextLine().toUpperCase();
return input;
}
}
break
is missing in case
for HELP
. Note that you do not need break
in the last case
which is default
.toUpperCase()
from returned string.do...while
to avoid using comm = getCommand(in);
twice.Given below is the corrected code:
import java.util.Scanner;
public class Main {
// Constantes que definem os comandos
public static final String REGISTER = "register";
public static final String LISTUSERS = "listusers";
public static final String UPLOAD = "upload";
public static final String READ = "read";
public static final String WRITE = "write";
public static final String GRANT = "grant";
public static final String REVOKE = "revoke";
public static final String USERDOCS = "userdocs";
public static final String TOPLEAKED = "topleaked";
public static final String TOPGRANTERS = "topgranters";
public static final String HELP = "help";
public static final String EXIT = "exit";
// Constantes que definem as mensagens para o utilizador
public static final String EXIT_COMM = "Bye!";
public static final String REGISTER_COMM = " was registered.";
public static final String ALREADY_REG_ERROR = " is already assigned to another user.";
public static final String NO_LIST_USERS = "There are no registered users.";
public static final String DOC_UPLOADED = " was uploaded.";
public static final String OFFICIAL_DOC = "Contact upda1ted.";
public static final String TOPSECRET_DOC = "Contact book empty.";
public static final String CONFIDENTIAL_DOC = "Phone number does not exist.";
public static final String CONTACT_SHARE_PHONE = "There are contacts that share phone numbers.";
public static final String HELP_COMM = "register - registers a new user /n";
public static void main(String[] args) {
System.out.println("Welcome!");
// WeKeepSecrets app = new WeKeepSecretsClass();
Scanner in = new Scanner(System.in);
String comm;
do {
System.out.print("Enter command: ");
comm = getCommand(in);
switch (comm) {
case HELP:
System.out.println(HELP);
break;
default:
if (!comm.equals(EXIT)) {
System.out.println("ERRO");
}
}
} while (!comm.equals(EXIT));
System.out.println(EXIT_COMM);
}
private static String getCommand(Scanner in) {
String input;
input = in.nextLine();
return input;
}
}
A sample run:
Welcome!
Enter command: help
help
Enter command: abc
ERRO
Enter command: exit
Bye!