title: allow user to edit student info from a binary file in Java
body: <p>I am working on a school project. It basically allows the user to create a Student using scanner, assigns and id to the student and then writes the data into a binary file called studentsinfo.dat. Once the student has been created the user can either display the info from the binary file or edit student. I got the displaying part working. However, I am having trouble to allow them to edit, the program asks the user to enter the student id to edit, the program should check if the user input exists in the file but I am having problem been able to move the file pointer so it looks for the id and compare it with the user input, I have tried some stuff but not working. Any ideas would be appreciated, here is my code:</p>
  <pre><code>import java.util.*;
  import java.util.concurrent.atomic.AtomicInteger;
  import java.io.*;
  public class MidTermProject {
      
      static AtomicInteger idGenerator = new AtomicInteger(0001);
      static int id;
      
      public static int getId() {
          return id;
      }
      ///Create Student method
      public static void CreateStudent() throws IOException {
          String FullName;
          String address;
          String city;
          String state;
          String newStudentID;
          
          Scanner keyboard = new Scanner(System.in);
          
          FileOutputStream fstream =
                  new FileOutputStream(&quot;StudentInfo.dat&quot;, true);
          DataOutputStream outputFile =
                  new DataOutputStream(fstream);
          
          System.out.print(&quot;\nPlease enter your information bellow.\n&quot; + &quot;\nFull Name: &quot;);
          FullName = keyboard.nextLine();
          outputFile.writeUTF(FullName);
          
          System.out.print(&quot;Address: &quot;);
          address = keyboard.nextLine();
          outputFile.writeUTF(address);
          
          System.out.print(&quot;City: &quot;);
          city = keyboard.nextLine();
          outputFile.writeUTF(city);
          
          System.out.print(&quot;State: &quot;);
          state = keyboard.nextLine();
          outputFile.writeUTF(state);
          
          id = idGenerator.getAndIncrement();
          String student = Integer.toString(getId());
          outputFile.writeUTF(student);
      
          outputFile.close();
          
          System.out.print(&quot;\nDone\n&quot;);
          
      }
      ///Edit Student method
      public static void EditStudent() throws IOException {
          String editName;
          String editaddress;
          String editCity;
          String editState;
          int editID;
          
          Scanner keyboard = new Scanner(System.in);
          
          System.out.print(&quot;Enter the ID of the student you would like to edit: &quot;);
          editID = keyboard.nextInt();
          
          RandomAccessFile file = 
              new RandomAccessFile(&quot;StudentInfo.dat&quot;, &quot;rw&quot;);
          file.seek(10);
              
          char ch = file.readChar();
              
          if(editID == ch) {
              System.out.print(&quot;\nPlease enter NEW information bellow.\n&quot; + &quot;\nFull Name: &quot;);
              editName = keyboard.nextLine();
              file.writeUTF(editName);
                  
              System.out.print(&quot;Address: &quot;);
              editaddress = keyboard.nextLine();
              file.writeUTF(editaddress);
                  
              System.out.print(&quot;City: &quot;);
              editCity = keyboard.nextLine();
              file.writeUTF(editCity);
                  
              System.out.print(&quot;State: &quot;);
              editState = keyboard.nextLine();
              file.writeUTF(editState);
                  
              file.close();
                  
              System.out.print(&quot;Successfully Edited&quot;);
          } else {
              System.out.print(&quot;error&quot;);
          }
      }
      ///Display Student method
      public static void DisplayStudent() throws IOException {
          FileInputStream fstream = new FileInputStream(&quot;StudentInfo.dat&quot;);
          DataInputStream inputFile = new DataInputStream(fstream);
          
          String student;
          boolean endOfFile = false;
          
          while(!endOfFile)
          {
              try
              {
                  student = inputFile.readUTF();
                  System.out.print(student + &quot;\n&quot;);
              }
              catch (EOFException e)
              {
                  endOfFile = true;
              }
          }
          System.out.println(&quot;\nDone&quot;);
          
          inputFile.close();
      }
  </code></pre>