InputMismatchException是什么意思
代码:
import java.util.ArrayList; import java.util.Scanner; public class Notes { Scanner in = new Scanner(System.in); ArrayList<String> notes_list = new ArrayList<String>(); int input=1; public void turnOn() { while (input!=0) { run(); } } public void run() { System.out.println("===Wanma.note.com==="); System.out.println("1. add notes"); System.out.println("2. look notes"); System.out.println("3. delete notes"); System.out.println("4. revise notes"); System.out.println("0. exit"); System.out.println("plaase input your choice"); input = in.nextInt(); switch (input) { case 1: add(); break; case 2: look(); break; case 3: delete(); break; case 4: revise(); break; } } public void add() { System.out.println("===add notes==="); System.out.println("please input the note:"); String note1 = in.next(); notes_list.add(note1); } public void look() { System.out.println("===look notes==="); System.out.println("here are your notes:"); for (int i=0; i<notes_list.size(); i++) { System.out.println((i+1)+". "+notes_list.get(i)); } } public void delete() { System.out.println("===delete notes==="); look(); System.out.println("please input the note you want to delete:"); int del = in.nextInt(); notes_list.remove(del-1); System.out.println("===the note has been removed==="); } public void revise() { System.out.println("===revise notes==="); look(); System.out.println("please input the note you want to change:"); int change = in.nextInt(); System.out.println("please input your new note:"); String recent = in.next(); notes_list.remove(change-1); notes_list.add(change-1,recent); System.out.println("your note has been changed"); } public static void main(String[] args) { Notes notes = new Notes(); notes.turnOn(); } }
尝试在48行之后添加以下代码:
具体原因可能是scanner.next()方法没有将回车等字符读入,而遗留在缓冲区,导致之后执行scanner.nextInt()时候读入这个回车,发生格式异常报错。