-2

The program is a expense tracker and needs to be error free. To accomplish that goal I need to restart everything from line 11 and further.

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int Size;
    int order;
    System.out.println("Put in the amount of expenses you have");
    Size = sc.nextInt();
    System.out.println("put in all your expenses");
    int userInput[] = new int[Size];
    for (int i = 0; i < userInput.length; i++)
        userInput[i] = sc.nextInt();
    System.out
            .println("do you want it ascending or descending order. If you want it in ascending press 1 or if you want descending press 2");
    order = sc.nextInt();
    System.out.print("expenses not sorted : ");
    printExpenses(userInput);
    if (order == 1) {
        expensesAscending(userInput);
    } else if (order == 2) {
        expensedescending(userInput);
    }else if (order>2){
        //How do i make it so that if they press three or above the program restarts
    }
}
j.doe
  • 1
  • 2

2 Answers2

0

You can use an eternal loop until the user enters a valid entry:

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    int Size;
    int order;
    int userInput[];
    do { // loop starts here
        System.out.println("Put in the amount of expenses you have");
        Size = sc.nextInt();
        System.out.println("put in all your expenses");
        userInput = new int[Size];
        for (int i = 0; i < userInput.length; i++)
            userInput[i] = sc.nextInt();
        System.out.println(
                "do you want it ascending or descending order. If you want it in ascending press 1 or if you want descending press 2");
        order = sc.nextInt();
    } while (order < 1 || order > 2); // if the input is not 1 or 2, it goes back
    System.out.print("expenses not sorted : ");
    printExpenses(userInput);
    if (order == 1) {
        expensesAscending(userInput);
    } else {
        expensedescending(userInput);
    }
}

However, I don't recommend you to restart the whole program if the user makes a mistake. It is preferable to ask him the input again or if he wants to restart the whole program.

Carlos
  • 302
  • 5
  • 12
0

Without feeding you the code, what you need is called a while loop. While loops will continue to 'do the thing' (in this case try to get the user to enter a correct input) until a condition is met (in this case the value of order is 1 or 2).

For example:

int order = null; 
while (order != 1 && order != 2){
    System.out.println("do you want it ascending or descending order."
        + "If you want it in ascending press 1 or if you want descending press 2");
    order = sc.nextInt(); 
}

However, I also think you may want to reconsider your interface design. Error and incorrect input handling is important, but so it making it easy for users to do things right on the first try. For example, it might be easier for users to remember how to get the result you want by entering 'a' for ascending and 'd' for descending.

Meg
  • 659
  • 7
  • 18