InputMismatchException文件读取文件写入

时间:2014-06-22 10:02:32

标签: inputmismatchexception

所以,这是我的问题。我在int count行获得了 inputmismatchexception 。我该如何解决这个问题?但有一条规则我不能触及代码行32 - 100+

package main;

 import java.util.*;

    import java.io.*;

    public class Main {

        public static void main(String[] args) {
                Scanner in = new Scanner(System.in);         
                int pick =0;
                //Menu            
                System.out.println("Pick an option");
                System.out.println("1. Add");
                System.out.println("2. Modify");
                System.out.println("3. Delete");
                System.out.println("4. View");
                System.out.print("Enter #: ");
                    pick = in.nextInt();
                System.getProperty("line.separator");
                    if(pick == 1)
                    {
                        String data = "database.txt";
                        try {

                            FileWriter fileWriter = new FileWriter(data);

                            // Always wrap FileWriter in BufferedWriter.
                            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

                            // Note that write() does not automatically
                            // append a newline character.
                            String fname = in.next();
                            String lname = in.next();
                            String city = in.next();
                            String id = in.next();


                            bufferedWriter.write(fname);
                            bufferedWriter.newLine();
                            bufferedWriter.write(lname);
                            bufferedWriter.newLine();
                            bufferedWriter.write(city);
                            bufferedWriter.newLine();
                            bufferedWriter.write(id);


                            // Always close files.
                            bufferedWriter.close();
                        } catch (IOException ex) {
                            System.out.println("Cannot Write !");

                        }

                    }


            try {
                Scanner input = new Scanner(new FileReader("database.txt"));

                // read how many records are there

                int count = input.nextInt();

                // remove the excess newline
                input.nextLine();

                for(int i = 0; i < count; i++) {
                    String fname = input.nextLine();
                    String lname = input.nextLine();
                    String city = input.nextLine();
                    String id = input.nextLine();

                    System.out.printf("Student #%d\n", i+1);
                    System.out.println(fname);
                    System.out.println(lname);
                    System.out.println(city);
                    System.out.println(id);
                    System.out.println();
                }

                input.close();
            }
            catch (FileNotFoundException e) {
                System.out.println("File not found!");
            }


    }

 }

请指导我实现目标的正确方法。

0 个答案:

没有答案