Comp Sci Assignment Help (Java)

vayanui8

Well-Known Member
OP
Member
Joined
Nov 11, 2013
Messages
1,086
Trophies
0
XP
908
Country
United States
I'm currently having some serious issues with my comp sci assignment. I don't really understand how to read and write to a file and need help. Here are the instructions to the assignment.


Create a program that prompts for a student's name and student ID#.
Program will
- add name and id to a file
- continually prompt and add until user chooses to end program


Create a class, Student, which has-A String for the name (Last, First)
and a String for the id

Create a class called IdCompleter,
which has-A (HLY2)List/(SL)Array of Students

load the file into a (HLY2) List or (SL) Array
create a completeID method, which adds zeroes to the front
of the ID IF there are less than 6 digits in the ID

modify the file to include the updated IDs.

******************************
**************************************
Use BufferedReader(FileReader) class to read from a file
and PrintWriter(FileWriter) class to write to a file
(LOOK THEM UP)
********************************************************************
Extension to the plan:

Create a menu with the following options:

1) display current roster (loads file and prints the current list of students and IDs)
- display will be sorted (you should change your code from earlier to
sort the list and THEN write to the file)
2) add students
3) find students (account for ID and for name (display name and ID when found)
- for name, if they pass in, for example, last name only, display
ALL students that contain that last name)
4) quit

Idiot proof your code (re-prompt if enter a string instead of an int for id,
choose menu item option that doesn't exist, enter an id
with too many numbers, etc)

Can somebody help me?
 

tj_cool

Site dev
Supervisor
Joined
Jan 7, 2009
Messages
10,064
Trophies
2
Location
This planet
XP
3,102
Country
Belgium
This might be better suited for a thread, Ask GBAtemp is quite limited for questions like these.

Anyway, reading and writing is not all that difficult. There are multiple ways to do it, but they have already provided you the names of the classes to use.

Read:

Code:
BufferedReader r = new BufferedReader(new FileReader(new File(filename)));
String line;
while( (line = r.readLine()) != null ) {
    // Process line (eg. push into list/array)
}
r.close();

Write:

Code:
PrintWriter w = new PrintWriter(new FileWriter(new File(filename)));
for(Student s : students) { // Assuming students is the list of students
    w.println(String.format("%s %s", s.getId(), s.getName())); // Adapt the format to the one you need
}
w.close();


In both cases you'll need to wrap them in try catch blocks for exceptions.
 

vayanui8

Well-Known Member
OP
Member
Joined
Nov 11, 2013
Messages
1,086
Trophies
0
XP
908
Country
United States
Thanks for the help. One more thing I don't understand us how to add the line read from the file to the list. Would I just be using the add method, and if so what would be the format on that?
 

tj_cool

Site dev
Supervisor
Joined
Jan 7, 2009
Messages
10,064
Trophies
2
Location
This planet
XP
3,102
Country
Belgium
Depends, what kind of list do you use.

If it's just a standard ArrayList then you can indeed just do
Code:
List<Student> students = new ArrayList<>();
// ...
students.add(new Student(id, name));
With the ID and Name extracted from the line.
 

vayanui8

Well-Known Member
OP
Member
Joined
Nov 11, 2013
Messages
1,086
Trophies
0
XP
908
Country
United States
Yah, its a standard arraylist. I'm still a bit confused though, when extracting the I'd or name from the file what would be the exact format? Would it be Id=readline for example?
 

tj_cool

Site dev
Supervisor
Joined
Jan 7, 2009
Messages
10,064
Trophies
2
Location
This planet
XP
3,102
Country
Belgium
Again, depends on your file.

Say your file looks like:
Code:
000001 Doe, John
000002 Smith, Will
...

Then I would just use the String split() method
Code:
line.split(" ", 2)
Then you should have a String array with the ID and Name
The 2 specifies you want max. 2 elements in the array, so it'll only split on the first space.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    OctoAori20 @ OctoAori20: Ello