-2

I have 2 questions. 1. I have problem to find a way going one step back in 'do, while loop' if user do not enter correct command into console. 2. After doing that, I want to use Users input in switch loop, so the chosen command can be processed, but I am not sure what to put into brackets as a variable to test, because it does not recognise Users input.

Here is what I have done so far. I've tried a lot of things, but non of them worked out.

  `package assignment6;
   public class Enumeracija {

   public enum Commands {
   PROPERTIES, NEW_FOLDER, RENAME, COPY, CUT, DELETE, UNSPECIFIED 
   }}  



   package assignment6;
   import assignment6.Enumeracija.Commands;
   import java.io.*;
   import java.util.Scanner;
   public class Assignment6 {
   public static boolean main(String[] args) throws FileNotFoundException, 
   IOException {

    //Showing commands to user
    for (int i = 0; i<Commands.values().length-1; i++){
        System.out.println(Commands.values()[i]);
    }
    //Reading users input

        Scanner scan = new Scanner(System.in); 
        System.out.println("Enter one of the given commands: ");
        String userInput;
    /* WHILE DO LOOP so user can make a mistake and program will not stop 
    running, until he writes it correctly. But Im getting main class not 
    found problem, when I want to run it.*/ 
    do {
        userInput = scan.nextLine().trim().toUpperCase();

        if (userInput.equals(Commands.values())) {
        return true;} 
        else {
        System.out.println("Input error. Keep trying and make sure you write command correctly.");    
        return false;}
    } while (false);

    switch(userInput) {
    /*If I use userInput, it does not recognise commands 
    that user enters. Every case is being unrecognized.*/ 
        case PROPERTIES:
        //;
        break;
        case NEW_FOLDER:
        //;
        break;
        case RENAME:
        //;
        break;
        case COPY:
        //;
        break;
        case CUT:
        //;
        break;
        case DELETE:
        //;
        break;
        case UNSPECIFIED: 
        //;
        break;*/
Lidija
  • 1

4 Answers4

0

First of all, the return type of main method in Java should always be void.

public static void main(String[] args) throws FileNotFoundException, IOException

Second, if the user enters the correct command, the program will end as you have used the return statement. It's better if you use a boolean variable as a flag, set its value to true and just break out of the loop.

boolean flag = false;

do {
    userInput = scan.nextLine().trim().toUpperCase();

    if (userInput.equals(Commands.values())) {
    flag = true;
    break;} 
    else {
    System.out.println("Input error. Keep trying and make sure you write command correctly.");    
    }
} while (flag);

This will keep the user in the loop till the right command is entered.

Now, coming to the switch statement, since the commands are in the form of enums, you need to reference them properly.

switch(userInput) {
    case Commands.PROPERTIES:
    //;
    break;
    case Commands.NEW_FOLDER:
    //;
    break;
    case Commands.RENAME:
    //;
    break;
    case Commands.COPY:
    //;
    break;
    case Commands.CUT:
    //;
    break;
    case Commands.DELETE:
    //;
    break;
    case Commands.UNSPECIFIED: 
    //;
    break;

Let me know if this works.

Mayank Aggarwal
  • 169
  • 1
  • 15
  • First part was mostly fine that way, but I needed to change the code, because of the .equals method: ` do{ String input = userInput.nextLine(); for(Commands c : Commands.values()){ if (input.toUpperCase().trim().equals(c.toString())) { System.out.println("You chose " + input.trim().toUpperCase() + " command."); correctCommand = true; } else { correctCommand = false; System.out.println("Input error. Keep trying..."); } }} while (correctCommand);` And still the same problem – Lidija Apr 10 '18 at 03:23
  • Switch part worked, when I converted String to Enum. – Lidija Apr 10 '18 at 03:23
0

You are stopping the flow of program by returning true/false. Instead maintain a variable to keep track

 do {
        boolean validInput = false; // this will keep track
        userInput = scan.nextLine().trim().toUpperCase();
        if (userInput.equals(Commands.values())) {
           validInput = true; // make flag as true on valid input
        } 
        else {
           System.out.println("Input error. Keep trying and make sure you write command correctly.");    
           validInput = false; // make flag as false on invalid input
       }
    } while (!validInput); //repeat the loop if invalid input

The problem with your switch statement is that you are passing String to switch whereas the cases are Enums. Convert your String to an Enum object

Commands value = Commands.valueOf(userInput);

switch(value){
   //processing
}
Sandesh Gupta
  • 1,175
  • 2
  • 11
  • 19
  • Thank you, i realised my mistake in second part, but first part still confuses me.` do{ String input = userInput.nextLine(); for(Commands c : Commands.values()){ if (input.toUpperCase().trim().equals(c.toString())) { System.out.println("You chose " + input.trim().toUpperCase() + " command."); correctCommand = true; } else { correctCommand = false; System.out.println("Input error. Keep trying..."); } }} while (correctCommand);` – Lidija Apr 10 '18 at 03:10
  • I needed to modify that part, cause using .equals, obviosly is possible only that way in FOR loop (im comparing input (String) and Enum). And now i'm stucked. While loop is not doing what I need, which is not stopping program until user do not write command properly. – Lidija Apr 10 '18 at 03:15
0
do {
    userInput = scan.nextLine().trim().toUpperCase();
    // telling you what to do here will take the entire fun out of the problem
    // HINT: check what Command.values() actually does and also what userInput.equals() actually does.
    // HINT: once you have figured out how to interpret the user input, you need to store it in a variable to be used in the switch statement
    // HINT: the variable that you store it in should be of the Command type (this should be more than sufficient a hint)
    if (userInput.equals(Commands.values())) {
        // do not return here. Return will cause the execution of the main method to stop
        // what you actually want over here is break;
        // this will allow you to break out of the infinite loop once your conditions are met.
    } 
    else {
        System.out.println("Input error. Keep trying and make sure you write command correctly.");   
        // remove the return condition here. This will stop execution of the main method
    }
} while (true); // change condition to true so that loop is infinite
  • OK, so the 2. question went easily. I realised that i can not put String into switch, without previous converting it to Enum and vice versa, depending on what I want to 'examine'. But the 1. question is still opened. I got that using .equals also can not be that simple, because I want to compare String with an enum. Because of that I used FOR loop, but after that, all that part with while loop can not be done and I try to figure out how to put that For loop inside the while loop, so the program won't stop running until the command isn't correctly written. – Lidija Apr 10 '18 at 01:48
  • ` do{ String input = userInput.nextLine(); for(Commands c : Commands.values()){ if (input.toUpperCase().trim().equals(c.toString())) { System.out.println("You chose " + input.trim().toUpperCase() + " command."); correctCommand = true; } else { correctCommand = false; System.out.println("Input error. Keep trying..."); } }} while (correctCommand);` – Lidija Apr 10 '18 at 02:50
0
  1. You can use a while loop as opposed to a do while loop. This makes the code more readable and hence easier to debug.

    boolean flag = true;
    while(flag){
     userInput = scan.nextLine().trim().toUpperCase();
     if (userInput.equals(Commands.values())) 
         flag = false;
     else 
         System.out.println("Input error..."); 
    }  
    
  2. You are using userInput which is a String object in the switch statement. Click here for an explanation on that.

Also the signature of the main class is:

public static void (String[] whateverHere)
enea
  • 41
  • 4