0

So yeah here is my problem. I get an inputmismatchexception at the line of int count. How do i Fix this issue ? But one rule I must not touch the code line 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!");
            }


    }

 }

Please guide me the correct way to achieve my objective.

Shulz
  • 508
  • 3
  • 12
  • 27
  • 1
    possible duplicate of [Question about InputMismatchException while using Scanner](http://stackoverflow.com/questions/2708186/question-about-inputmismatchexception-while-using-scanner) – user207421 Jun 22 '14 at 10:11
  • Your question has nothing to do with file writing, and not much to with file reading either. Amended your title and tags accordingly. – user207421 Jun 22 '14 at 10:17
  • Sorry about that. But Why does it read the newline ? – Shulz Jun 22 '14 at 10:26

0 Answers0