I want to make a sort of simple menu in this program, but I can't seem to make the string "command" change. I couldn't understand the buffer string concept, and char only applies to single letters, numbers, or symbols. Is there some way to keep entering words into "command" whether it is a char or a string? This is was easier in C++ because strings were not immutable. Could you please suggest a solution?
import java.util.Scanner;
import java.lang.StringBuffer;
public class Driver {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Coins CoinsdHsu = new Coins();
String command = "";
System.out.println("Enter commands, enter Menu to view options");
while(true){
System.out.println("Command: ");
command = in.nextString();
if(command == "Menu"){
CoinsdHsu.menu();
}else if(command == "Enter number of cents"){
CoinsdHsu.howMany();
}else if(command == "Calculate coin distribution"){
CoinsdHsu.calculate();
}else if(command == "Stop"){
break;
}else{
System.out.println("Enter a valid command");
}
}
}
}
class Coins {
private int numCents;
public Coins(){};
public void calculate() {
int numQuarter = numCents / 25;
int quarterRemain = numCents % 25;
int numDime = quarterRemain / 10;
int dimeRemain = quarterRemain % 10;
int numNickel = dimeRemain / 5;
int numPenny = dimeRemain % 5;
System.out.println(numCents + " cents =>");
System.out.println("Quarter(s)\t" + numQuarter);
System.out.println("Dime(s)\t\t" + numDime);
System.out.println("Nickel(s)\t" + numNickel);
System.out.println("Penny(s)\t" + numPenny);
System.out.println("");
}
public void howMany(){
int cents;
Scanner in = new Scanner(System.in);
System.out.print("Number of cents: ");
cents = in.nextInt();
numCents = cents;
}
public void menu(){
System.out.println("Enter number of cents\nCalculate coin distribution\nStop\n");
}
}