Note that there are some explanatory texts on larger screens.

plurals
  1. POJava try-catch not working
    primarykey
    data
    text
    <p>wondering if someone can tell me why my try-catch doesn't work. I enter a character when I should be entering an integer and the program just exits with the exception. It's identical to a try-catch I wrote in another program. Not sure why it doesn't work. :(</p> <pre><code>import java.io.*; import java.util.*; public class Lab8FileIO { public static void main(String[] args) throws IOException { int menuChoice = 0; String filename, userInput, choice; boolean playAgain = true; char response; Scanner keyboard = new Scanner(System.in); while (playAgain) { // Display menu and get user selection displayMenu(); do { try { menuChoice = keyboard.nextInt(); if ((menuChoice &lt; 1) || (menuChoice &gt; 4)) // Make sure user enters a number within the specified range System.out.print("You must enter a number from 1 to 4!"); } catch (InputMismatchException e) { System.out.print("You must enter a number from 1 to 4!"); keyboard.nextInt(); } } while (menuChoice &lt; 1 &amp;&amp; menuChoice &gt; 4); // Get input/output filename from user System.out.print("\nEnter filename in the format filename.txt: "); filename = keyboard.next(); switch (menuChoice) { // Write to file case 1: PrintWriter outputFile = new PrintWriter(filename); userInput = getUserInput(); System.out.println("I am writing your input to filename: " + filename + "\n"); outputFile.println(userInput); outputFile.close(); break; // Read from file case 2: File file = new File(filename); Scanner inputFile = new Scanner(file); System.out.println("I am reading from filename: " + filename + "\n"); while (inputFile.hasNextLine()) { String line = inputFile.nextLine(); System.out.println(line); } inputFile.close(); break; // Append to file case 3: FileWriter appendFile = new FileWriter(filename, true); userInput = getUserInput(); System.out.println("I am appending your input to filename: " + filename + "\n"); appendFile.write(userInput); appendFile.close(); break; // Exit case 4: System.out.println("Goodbye!"); System.exit(0); break; } // Ask user to play again do { System.out.print("\nWould you like to continue (Y/N)? "); choice = keyboard.next(); response = Character.toUpperCase(choice.charAt(0)); } while (response != 'Y' &amp;&amp; response != 'N'); // Keep asking until user enters correct response if (response == 'Y') // Play again { playAgain = true; System.out.println(); } else if (response == 'N') // Quit { playAgain = false; System.out.println("\nThanks for playing!"); } } </code></pre> <p>} </p> <pre><code>public static void displayMenu() { System.out.println("File IO Options:"); System.out.println("1) Write"); System.out.println("2) Read"); System.out.println("3) Append"); System.out.println("4) Quit"); System.out.print("What would you like to do: "); } public static String getUserInput() { String str; Scanner keyboard = new Scanner(System.in); System.out.println("Start typing. Hit enter when you're done:"); str = keyboard.nextLine(); return str; } } </code></pre> <p>This is the error I'm getting:</p> <p>File IO Options:<br> 1) Write<br> 2) Read<br> 3) Append<br> 4) Quit<br> What would you like to do: a<br> You must enter a number from 1 to 4!Exception in thread "main"<br> java.util.InputMismatchException<br> at java.util.Scanner.throwFor(Scanner.java:840)<br> at java.util.Scanner.next(Scanner.java:1461)<br> at java.util.Scanner.nextInt(Scanner.java:2091)<br> at java.util.Scanner.nextInt(Scanner.java:2050)<br> at Lab8FileIO.main(Lab8FileIO.java:41)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload