0

My program compiles, but I am getting run time exceptions, not sure if I need to handle them? methods commented out and for extended class, they just display text. I also don't know if I am using labels correctly? I want my program to loop and handle exceptions for erroneous characters, which it does, but it should only terminate if the user enters "5".

import java.util.*;
import java.util.InputMismatchException;
import java.util.Scanner;


public class Mainmenu3 {

// extends premierLeagueClubs



public static void main(String args[]){

    boolean shouldExit = false;

    int option = 0;
loop:   while (!shouldExit) {
try{
    Scanner in = new Scanner(System.in);     

    menu();
    System.out.println("\n");
    option = in.nextInt();
} // end try

catch(InputMismatchException e) {
String option2 = Integer.toString(option);

    } // end catch

    switch (option) {

    case 1:
    chooseTeam();
    break;

    case 2: 
    createProfile();
    break;

    case 3:
    loadSave();
    break;

    case 4:
    credits();  
    break;

    case 5:
    break loop;

    default:
    System.out.println("Invalid choice");
}

    } // end switch




   } // end main method

public static void chooseTeam(){
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
System.out.println("Select Team : ");
System.out.println("1. Arsenal");
System.out.println("2. Aston Villa");
System.out.println("3. Bournemouth");
System.out.println("4. Chelsea");
System.out.println("5. Crystal Palace");
System.out.println("6. Everton");
System.out.println("7. Leicester City");
System.out.println("8. Liverpool");
System.out.println("9. Manchester United");
System.out.println("10. Manchester City");
System.out.println("11. Newcastle United");
System.out.println("12. Norwich City");
System.out.println("13. Southampton");
System.out.println("14. Stoke City");
System.out.println("15. Sunderland");
System.out.println("16. Swansea City");
System.out.println("17. Tottenham Hotspur");
System.out.println("18. Watford");
System.out.println("19. West Brom");
System.out.println("20. West Ham United");

int option = 0;

    Scanner in = new Scanner(System.in);
    //menu();

    System.out.println("\n");
    option = in.nextInt();
    System.out.println("You entered : " + option);

} // end chooseTeam



public static void createProfile(){
} // end createProfile
public static void loadSave(){
} // end loadSave
public static void credits(){
} // end credits


public static void menu(){



System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    System.out.println("Created by Darren Estcourt");   
    System.out.println("\n");
    System.out.println("Please choose an option : ");
    System.out.println("\n");
    System.out.println("1. Choose team");
    System.out.println("\n");
    System.out.println("2. Create profile");
    System.out.println("\n");
    System.out.println("3. Load/Save game");
    System.out.println("\n");
    System.out.println("4. Credits");   
    System.out.println("\n");
    System.out.println("5. Quit");

    System.out.println("\n");

    String option="0";

    Scanner in = new Scanner(System.in);

    System.out.println("\n");
    option = in.nextLine();

    switch (option) {

    case "1":
    chooseTeam();
    break;

    case "2":   
    createProfile();
    break;

    case "3":
    loadSave();
    break;

    case "4":
    credits();  
    break;

    case "5":
    System.out.println("Goodbye!"); 

    default:
    System.out.println("Please select an option between 1 and 4");
//menu();
    } // end switch



} // end menu




} // end class
melli-182
  • 1,216
  • 3
  • 16
  • 29
  • 3
    please, provide a stacktrace and delete commented code if they are irrelevant. – T.G Jul 08 '15 at 12:26

1 Answers1

0

There are multiple issues with your program:

  1. you define shouldExit but nowhere do you update it according to user input
  2. you scan and process user input both in main() and in menu(). generally, your code isn't structured in a coherent readable manner and there is little reuse.
  3. println() already prints a new line (as opposed to print())
  4. for the love of programming goddess - do not use System.exit() to terminate a Java program

here is an example of a working class with the above guidelines implemented to a pasable degree :

import java.util.*;

public class Mainmenu3
{
    static Scanner in;

    public static void main(String args[])
    {
        in = new Scanner(System.in);
        boolean shouldExit = false;
        while (!shouldExit) {
            displayMenu();
            shouldExit = processUserInput();
        }
    }

    public static void displayMenu()
    {
        System.out.println();
        System.out.println("Created by Darren Estcourt");
        System.out.println("Please choose an option : ");
        System.out.println("1. Choose team");
        System.out.println("2. Create profile");
        System.out.println("3. Load/Save game");
        System.out.println("4. Credits");
        System.out.println("5. Quit");
    }

    public static int getUserInput()
    {
        int option = -1;
        try {
            option = in.nextInt();
        } catch (InputMismatchException e) {
            // When a scanner throws an InputMismatchException, 
            // the scanner will not pass the token that caused the exception,
            // so that it may be retrieved or skipped via some other method.
            in.next();
        } catch (NoSuchElementException e) {
            // input is exhausted
            option = 5;
        } catch (IllegalStateException e) {}
            // scanner is closed
            option = 5;
        }
        return option;
    }

    public static boolean processUserInput()
    {
        int option = getUserInput();
        switch (option) {
        case 1:
            chooseTeam();
            break;
        case 2:
            createProfile();
            break;
        case 3:
            loadSave();
            break;
        case 4:
            credits();
            break;
        case 5:
            System.out.println("Goodbye!");
            return true;
        default:
            System.out.println("Invalid choice");
        }
        return false;
    }

    public static void chooseTeam()
    {
        System.out.println();
        System.out.println("Select Team : ");
        System.out.println("1. Arsenal");
        System.out.println("2. Aston Villa");
        System.out.println("3. Bournemouth");
        System.out.println("4. Chelsea");
        System.out.println("5. Crystal Palace");
        System.out.println("6. Everton");
        System.out.println("7. Leicester City");
        System.out.println("8. Liverpool");
        System.out.println("9. Manchester United");
        System.out.println("10. Manchester City");
        System.out.println("11. Newcastle United");
        System.out.println("12. Norwich City");
        System.out.println("13. Southampton");
        System.out.println("14. Stoke City");
        System.out.println("15. Sunderland");
        System.out.println("16. Swansea City");
        System.out.println("17. Tottenham Hotspur");
        System.out.println("18. Watford");
        System.out.println("19. West Brom");
        System.out.println("20. West Ham United");

        int option = getUserInput();

        System.out.println("You entered : " + option);
    } // end chooseTeam

    public static void createProfile()
    {
        System.out.println("createProfile");
    }

    public static void loadSave()
    {
        System.out.println("loadSave");
    }

    public static void credits()
    {
        System.out.println("credits");
    }
}
Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
  • I want to handle InputMismatchException. If the user inputs a char, this program (sharonbn solution) goes into a permanent loop! Does anyone else have a solution? – Darren Estcourt Jul 10 '15 at 09:28
  • all that was needed is just a little more research to get to [this thread](http://stackoverflow.com/questions/12702076/java-try-catch-with-inputmismatchexception-creates-infinite-loop) which explains what's missing. I fixed the `getUserInput()` method in the answer (now you see what it is good to reuse code :)) – Sharon Ben Asher Jul 12 '15 at 07:00