Reputation: 47
I have a text file, that I'm supposed to read data from. The data should then be used to set a level to pass:
The .txt
file looks like this
X;20
Y;12
The problem is, that when I read from the file, and add it to an ArrayList
, it only reads the columns. In other words, the result is that index 0
contains X;Y
, and index 1
contains 20;12
. What I need is for index 0
to contain [X 20]
and index 1
to contain [Y 12]
.
My code is:
BufferedReader assignment = new BufferedReader(new FileReader(assignmentFile));
ArrayList<String> assignmentArrayList = new ArrayList<String>();
String item;
while ((item = assignment.readLine()) != null) {
assignmentArrayList.add(item);
String[] itemSplit = item.split(";");
String passWithDistinction = itemSplit[0];
String pass = itemSplit[1];
System.out.println(passWithDistinction + pass);
}
Upvotes: 0
Views: 656
Reputation: 27946
If I understand your question correctly then you want to read the text file and end up with the two numbers that define the distinction and pass mark. I'm not exactly sure what the X and Y are for and why you want to store them - I'd be happy to help with that if you want.
If you know the text file has two (and only two) lines then you don't really need a while loop. Simpler would be to just read and parse this lines in turn:
String[] distinctionLine = assignment.readLine().split(";");
int distinction = Integer.parseInt(distinctionLine[1]);
String[] passLine = assignment.readLine().split(";");
int pass = Integer.parseInt(passLine[1]);
There are a number of exceptions and error conditions you should be checking for but hopefully you get the idea.
Upvotes: 0
Reputation: 1103
You can retrieve the file's content to a String
array like this:
File assignmentFile = new File("C:/Dir1/Dir2/file.txt");
ReadFile assignment = new ReadFile(assignmentFile.getAbsolutePath());
String[] lines = assignment.OpenFile();
If you want to set the strings so they won't have the ';' symbol, add these lines:
for(int i=0; i<lines.length(); i++)
lines[i].replace(";", " ");
Upvotes: 1