-3

Ok this is quite a long one but I've looked everywhere and I'm still unsure on how to do it. This is a list of students information in the classroom layout. The program is used to let a child choose a seat but once they have chose it then it should have a status update so nobody else can take it.

Columns explained - (1)Student in number order (2)Male/Female (3)Window Seat/Aisle Seat (4)With/Without table (5)Forward Seat/Backward Seat (6) Ease of Access Seat

.txt file;

01 2 true false true false

02 2 false false true false

03 1 true false true true

04 2 false false true true

05 1 true true true false

I understand they don't totally make sense but it's just an example.

How do I get the program to read through each one of these rows using an array to store all this information? for child 1,2,3's seat etc. The .txt file represents exactly what kind of seat it is as explained above. Once the array has read through I want it to be able to save each row.

gnizzle
  • 7
  • 4

3 Answers3

0

If you just want to read a file and store each line seperately in an array you can do the following. Note that it's not possible to create the array beforehand as you do not know how many lines you will get.

String[] result;
ArrayList<String> lines = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader("path"))) {
    while (reader.ready()) {
        lines.add(reader.readLine());
    }
} catch (IOException e) {
    // proper error handling
}
result = lines.toArray(new String[lines.size()]);

Or if you want to be able to access the columns directly:

String[][] result;
ArrayList<String[]> lines = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader("path"))) {
    while (reader.ready()) {
        lines.add(reader.readLine().split(" ");
    }
} catch (IOException e) {
    // proper error handling
}
result = lines.toArray(new String[lines.size()][]);

for(String[] lineTokens) {
  String studendNumber = lineTokens[0];
  boolean gender = Boolean.parseBoolean(lineTokens[1]);
  ...
}
MartinS
  • 2,759
  • 1
  • 15
  • 25
-1

let's say your file name is students.txt all u need to do is read the data and store it into an array of strings to deeal with it later so her's the stuff :

BufferedReader in  = new BufferedReader(new FileReader("students.txt"));
String[][] data  = new String[][6];
int i = 0;
while(in.ready()){
data[i] = bf.readLine().split(" ");//use whatever is separating the data
i++;
}
Méhdi Màick
  • 157
  • 3
  • 7
  • That does not even compile yet alone the fact that you have to specify the array size that you do not know beforehand. – MartinS Feb 24 '16 at 16:39
-1

If you just want to read a text file in java, have a look at this: Reading a plain text file in Java

A saving of each row won't be possibe, it's a file, not a database. So load the file, change the data as you like, save it.

You should also think about the format... may be use XML, JSON or CSV format to store the data. There are libs which do most of the job for you...

If you are planning parallel access to your program data (more than one program instance and users, only one datafile), a simple text file is the wrong solution for your needs.

Community
  • 1
  • 1
user3227576
  • 554
  • 8
  • 22
  • What's wrong about storing a file in JSON or XML format and not inventing your own format? It depends what he wants to do... – user3227576 Feb 24 '16 at 16:48
  • Nothing wrong with those but this is most likely a homework question for a Programming 101 course, as such I am assuming the asker might not be allowed to use any 3rd party libraries or use XML or JSON encoding. – hyde Feb 24 '16 at 16:52
  • Ok, i'm sry, I didn't get it and just wanted to help... so just for the file access i believed the link would be the best solution... and i just wanted to get more information about what he wants to do. May be you are right... the link would have been enough. – user3227576 Feb 24 '16 at 16:55