Search code examples
javaloopsexitautomata

Looping a program to start again after finish checking


I'm trying to make a program where the user would input a string over a and b, and the program would check whether it is accepted or not. Now i managed to make the program exit if the user input "exit", but i haven't been able to figure out how to loop the program and make it start again when it finished checking a string, asking the user to input again. How should i do it? Below is my code

import java.util.Scanner; 
public class Automata {
public static void main(String[] args) {
    Boolean keeprunning = true;
    Scanner inputScanner = new Scanner(System.in);
    System.out.println("Please enter your input");
    String input = inputScanner.next();
    char [] Inputted = input.toCharArray();

    if (input.equalsIgnoreCase("Exit")){
        keeprunning = false;
        System.out.println("Exit now");
    }

    String stateA = "A";
    String stateB = "B";
    String stateC = "C";
    String stateD = "D";
    String stateE = "E";
    String stateF = "F";
    String currentState = "A";

    while(keeprunning){
    for (int i = 0; i < Inputted.length; i++){
        if (Inputted[i]=='a' && currentState.equals(stateA)){
            currentState = "B";
            System.out.println(stateA + " -- " + Inputted[i] + " --> " + stateB);
        } else if (Inputted[i]=='b' && currentState.equals(stateA)){
            currentState = "F";
            System.out.println(stateA + " -- " + Inputted[i] + " --> " + stateF);
        } 

        else if (Inputted[i] == 'a' && currentState.equals(stateB)){
            currentState = "B";
            System.out.println(stateB + " -- " + Inputted[i] + " --> " + stateB);
        } else if (Inputted[i] == 'b' && currentState.equals(stateB)){
            currentState = "C";
            System.out.println(stateB + " -- " + Inputted[i] + " --> " + stateC);
        } 

        else if (Inputted[i]== 'b' && currentState.equals(stateF)){
            currentState = "F";
            System.out.println(stateF + " -- " + Inputted[i] + " --> " + stateF);
        } else if (Inputted[i] == 'a' && currentState.equals(stateF)){
            currentState = "B";
            System.out.println(stateF + " -- " + Inputted[i] + " --> " + stateB);
        } 

        else if (Inputted[i] == 'a' && currentState.equals(stateC)){
            currentState = "D";
            System.out.println(stateC + " -- " + Inputted[i] + " --> " + stateD);
        } else if (Inputted[i] == 'b' && currentState.equals(stateC)){
            currentState = "F";
            System.out.println(stateC + " -- " + Inputted[i] + " --> " + stateF);
        } 

        else if (Inputted[i] == 'a' && currentState.equals(stateD)){
            currentState = "B";
            System.out.println(stateD + " -- " + Inputted[i] + " --> " + stateB);
        } else if (Inputted[i] == 'b' && currentState.equals(stateD)){
            currentState = "E";
            System.out.println(stateD + " -- " + Inputted[i] + " --> " + stateE);
        } 

        else if (Inputted[i] == 'a' && currentState.equals(stateE)){
            currentState = "B";
            System.out.println(stateE + " -- " + Inputted[i] + " --> " + stateB);
        } else if (Inputted[i] == 'b' && currentState.equals(stateE)){
            currentState = "C";
            System.out.println(stateE + " -- " + Inputted[i] + " --> " + stateC);
        }   

    } if (currentState.equals(stateA) || currentState.equals(stateB) || currentState.equals(stateD) || currentState.equals(stateE)){
        System.out.println("Input accepted");
        keeprunning = false;
        break;
    } else if (currentState.equals(stateF) || currentState.equals(stateC)){
        System.out.println("Input not accepted");
        keeprunning = false;
        break;
    }

}
    }

Thanks for any help :D


Solution

  • Just move your while loop header to the top, just after keeprunning declaration:

    Boolean keeprunning = true;
    while(keeprunning){
    ...
    

    And remove below code from if-else statement:

    keeprunning = false;
    break;
    

    That should solve your stated problem


    import java.util.Scanner; 
    public class Automata {
    public static void main(String[] args) {
    
    Boolean keeprunning = true;
    while(keeprunning){
        Scanner inputScanner = new Scanner(System.in);
        System.out.println("Please enter your input");
        String input = inputScanner.next();
        char [] Inputted = input.toCharArray();
    
        if (input.equalsIgnoreCase("Exit")){
            keeprunning = false;
            System.out.println("Exit now");
        }
        else{
    
        String stateA = "A";
        String stateB = "B";
        String stateC = "C";
        String stateD = "D";
        String stateE = "E";
        String stateF = "F";
        String currentState = "A";
    
    
        for (int i = 0; i < Inputted.length; i++){
            if (Inputted[i]=='a' && currentState.equals(stateA)){
                currentState = "B";
                System.out.println(stateA + " -- " + Inputted[i] + " --> " + stateB);
            } else if (Inputted[i]=='b' && currentState.equals(stateA)){
                currentState = "F";
                System.out.println(stateA + " -- " + Inputted[i] + " --> " + stateF);
            } 
    
            else if (Inputted[i] == 'a' && currentState.equals(stateB)){
                currentState = "B";
                System.out.println(stateB + " -- " + Inputted[i] + " --> " + stateB);
            } else if (Inputted[i] == 'b' && currentState.equals(stateB)){
                currentState = "C";
                System.out.println(stateB + " -- " + Inputted[i] + " --> " + stateC);
            } 
    
            else if (Inputted[i]== 'b' && currentState.equals(stateF)){
                currentState = "F";
                System.out.println(stateF + " -- " + Inputted[i] + " --> " + stateF);
            } else if (Inputted[i] == 'a' && currentState.equals(stateF)){
                currentState = "B";
                System.out.println(stateF + " -- " + Inputted[i] + " --> " + stateB);
            } 
    
            else if (Inputted[i] == 'a' && currentState.equals(stateC)){
                currentState = "D";
                System.out.println(stateC + " -- " + Inputted[i] + " --> " + stateD);
            } else if (Inputted[i] == 'b' && currentState.equals(stateC)){
                currentState = "F";
                System.out.println(stateC + " -- " + Inputted[i] + " --> " + stateF);
            } 
    
            else if (Inputted[i] == 'a' && currentState.equals(stateD)){
                currentState = "B";
                System.out.println(stateD + " -- " + Inputted[i] + " --> " + stateB);
            } else if (Inputted[i] == 'b' && currentState.equals(stateD)){
                currentState = "E";
                System.out.println(stateD + " -- " + Inputted[i] + " --> " + stateE);
            } 
    
            else if (Inputted[i] == 'a' && currentState.equals(stateE)){
                currentState = "B";
                System.out.println(stateE + " -- " + Inputted[i] + " --> " + stateB);
            } else if (Inputted[i] == 'b' && currentState.equals(stateE)){
                currentState = "C";
                System.out.println(stateE + " -- " + Inputted[i] + " --> " + stateC);
            }   
    
        } if (currentState.equals(stateA) || currentState.equals(stateB) || currentState.equals(stateD) || currentState.equals(stateE)){
            System.out.println("Input accepted");
    
        } else if (currentState.equals(stateF) || currentState.equals(stateC)){
            System.out.println("Input not accepted");
        }
    }   
    
    }
    }
    }