0

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:

  • > 20 → very good
  • between 12-20 → OK
  • < 12 → failed

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);
}
5
  • 3
    Show your code please. Commented Feb 9, 2015 at 11:28
  • Why do you use ArrayList instead of a simple String array? Commented Feb 9, 2015 at 11:41
  • Your code works. I can't understand what you're looking for. Commented Feb 9, 2015 at 11:47
  • What I'm trying to do is add the number into a specific index in the arraylist so if i .get(1) I get "20", so that i can set the level for pass with that number. Commented Feb 9, 2015 at 11:53
  • I'm not sure if you are still looking for suggestions on this. I'd be happy to help but I really don't understand your question. You say that you are reading the file to obtain the two numbers that define the distinction and pass marks. But then what is the X and Y for? And why do you say that you want your ArrayList to have [X 20] and [Y 12] as members? Commented Feb 9, 2015 at 12:26

2 Answers 2

1

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(";", " ");
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.