Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java - In this problem you are meant to ask a user to input users and then write

ID: 3682530 • Letter: J

Question

Java - In this problem you are meant to ask a user to input users and then write them to a txt file. In a separate program, you have to ask the user for an id number and if that id number exists, return the full user information based on that user. I am having trouble searching for that id number in the program.

Write File:

import java.io.*;
import java.util.*;

class Record implements Serializable
{
int IDnumber, DOB;
String name;
double iq;

public Record(int id, String name, int dob, double iq)
{
this.name=name;
IDnumber=id;
DOB=dob;
this.iq=iq;
}
public String toString()
{
return "ID: "+IDnumber+" Name: "+name+" Birth Year: "+DOB+" IQ: "+iq;
}

}

public class P3
{
protected int idNumber;
public static void main(String a[])
{
try
{
ArrayList<Record> records;
File file=new File("iq.txt");
// Read already saved records from file (If available)
if(file.exists())
{
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
records = (ArrayList<Record>) ois.readObject();
ois.close();
}
else
records=new ArrayList();
  
int c=1;
Scanner sc=new Scanner(System.in);
int id,dob;
double iq;
String name;
Record r;
//ArrayList records=new ArrayList();
  
while(c!=0)
{
System.out.println("Enter 1 to add record and 0 to exit");
c=sc.nextInt();
if(c==1)
{
System.out.println("Enter Id");
id=sc.nextInt();
System.out.println("Enter Name");
name=sc.next();
System.out.println("Enter Birth Year");
dob=sc.nextInt();
System.out.println("Enter IQ");
iq=sc.nextDouble();
  
r=new Record(id,name,dob,iq);
records.add(r);
  
  
}
}
  
// Sort the list based on ID
Collections.sort(records, new Comparator<Record>() {
@Override public int compare(Record p1, Record p2) {
return p1.IDnumber - p2.IDnumber; // Ascending
}
});
  
System.out.println("Saving Records..... Done");
// Write the updated list to file
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(records);
oos.close();

}
catch(Exception e)
{
System.out.println(e);
}
}
}

Read File:

import java.io.*;
import java.util.*;

public class P3Read
{
public static void main(String a[])
{
int[] arr = null;
int num = 120;
try
{
ArrayList<Record> records;
File file=new File("iq.txt");


if(file.exists())
{
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
records = (ArrayList<Record>) ois.readObject();
ois.close();

Scanner scan = new Scanner(fis);
while(scan.hasNextLine()){
String lineFromFile = scan.nextLine();
if(lineFromFile.contains("120")){
System.out.println("Found");
break;
}
else;
System.out.println("Not found.");
}
for(int i=0; i<records.size();i++)
{
System.out.println("Record#"+i+" "+records.get(i));
if(records.contains(num)){
System.out.print("Yes");
}
}
}
else
{
System.out.println("Records not found");
}

  

}
catch(Exception e)
{
System.out.println(e);
}

}

}

Explanation / Answer

Hi, Please find the updated P3Read class as per your requirement.

P3Read.java

import java.io.*;
import java.util.*;
public class P3Read
{
public static void main(String a[])
{
int[] arr = null;
int num = 120;
try
{

File file=new File("iq.txt");

if(file.exists())
{
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList<Record> records = (ArrayList<Record>) ois.readObject();

Scanner scan = new Scanner(System.in);
System.out.println("Please enter the id number: ");
int id = scan.nextInt();
boolean found = false;
Record rec = null;
int count = 0;
for(int i=0; i<records.size();i++){
   rec = records.get(i);
   if(rec.IDnumber == id){
       found = true;
       break;
   }
   count++;
}
if(found == true){
System.out.println("Record Found: "+records.get(count));
}
else{
System.out.println("Record Not Found");  
}
ois.close();
}

}
catch(Exception e)
{
System.out.println(e);
}
}
}

Output1:

Please enter the id number:
111
Record Found: ID: 111 Name: Suresh Birth Year: 1988 IQ: 1111.0

Output2:

Please enter the id number:
222
Record Found: ID: 222 Name: Sekhar Birth Year: 1989 IQ: 12345.0

Output3:

Please enter the id number:
333
Record Not Found

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote