Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. public class ReadWrite {
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.         try {
  8.             RandomAccessFile rand = new RandomAccessFile("C:/Users/Caiden/Desktop/Stu.dat", "rw");
  9.             for (int i=0;i<5;i++) {
  10.                 System.out.print("Student ID:");
  11.                 rand.writeInt(scan.nextInt());
  12.                 System.out.print("GPA:");
  13.                 rand.writeDouble(scan.nextDouble());
  14.             }
  15.             boolean terminate = false;
  16.             while (!terminate) {
  17.                 System.out.println("Type ID to see GPA. Type 0 to exit.");
  18.                 int id = scan.nextInt();
  19.                 if (id == 0) {
  20.                     terminate = true;
  21.                     break;
  22.                 }
  23.                 rand.seek(0);
  24.                 int curr = 0;
  25.                 while (curr != -1 && curr != id) {
  26.                     if (rand.getFilePointer() != rand.length())
  27.                         curr = rand.readInt();
  28.                     else break;
  29.                 }
  30.                 if (curr == id) System.out.println("GPA: " + rand.readDouble());
  31.                 else System.out.println("Sorry! ID not found!");
  32.             }
  33.         } catch (IOException e) {
  34.             System.out.println("IO error!");
  35.             e.printStackTrace();
  36.         } finally {
  37.             System.out.println("Shutting down...");
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement