Kofi
Kofi

Reputation: 11

How to extract specific data from a textfile and then display it (Java)

I have a set of data that is displayed in this format:

total question,Hint count,Right question Count

These are integers which are separated by a ':'.

"01:02:03"

How can I read the text-file so that it picks out the '02' from the data and displays it?

I have written a basic program that only reads the text-file and displays it in a jTextField but I am finding it difficult to get information to help me do this.

In the end if I had these set of data,

number1"10:07:03"
number2"10:03:08"
number3"10:06:05"
number4"10:02:10"

I will extract 07, 03, 06, 02 from the data respectively.

Any input will be good and if anyone has on-line resources and tutorials, I will also be happy to have a look for myself.

Thanks in advance

Upvotes: 1

Views: 2218

Answers (3)

Archer
Archer

Reputation: 5147

The example of using readLine and split:

BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("yourFile.txt")));

String text;
while((text = reader.readLine()) != null) {
    String [] parts = text.split(":");
    // now `parts` array will contain your data
}

Upvotes: 0

Alex DiCarlo
Alex DiCarlo

Reputation: 4891

You will probably want to use the string manipulation methods in String. In particular check out String.split using : as a delimiter.

You could also look into using more advanced regex's, see Pattern for more information. However, in this case, split should work fine.

Upvotes: 1

Viç
Viç

Reputation: 356

You could use the readLine method and then on the String representing your line use split method with the ":" delimiter. Then it should be trivial for you to work out...

(If you never heard of bufferedreader please read the class description in the first link I provided.)

Upvotes: 1

Related Questions