0

I'm planning to output an error statement when a character was entered in the obj.nextInt();. But whenever i entered an invalid character type, it's looks rather than go to the catch.

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

import Methods.FileSys;

class Main {
    public static void main(String args[]) throws FileNotFoundException, IOException {
        Scanner obj = new Scanner(System.in);
        FileSys fileSys = new FileSys();

    while (true){
        System.out.println("\nMain Menu");
        System.out.println("[1] Create");
        System.out.println("[2] Read");
        System.out.println("[3] Update");
        System.out.println("[4] Delete");
        System.out.println("[5] Search");
        System.out.println("[6] Exit");
    try {  
        System.out.print("\nWhat do you want to do? ");
        int decision = obj.nextInt();
        obj.nextLine(); 

        switch(decision){
            case 1:
                fileSys.createNote();
            break;

            case 2:
                fileSys.displayNote();
            break;

            case 3:
                fileSys.updateNote();
            break;

            case 4:
                fileSys.deleteNote();
            break;

            case 5:
                fileSys.searchNote();
            break;

            case 6:
                System.exit(0);
            break;

            default:
                System.out.print("Invalid input!\n");
            break;
        }
    } 
    catch (InputMismatchException e) {
    }
}
}
}

It should output an error message then go back to the main menu rather rather looping non stop

enter image description here

dwextit
  • 11

0 Answers0