0

I am reading text from a file and I have been having trouble trying to read List 1 and List 2 into 2 different String . The * indicates where the first list ends. I have tried using arrays but the array only stores the last * symbol.

List 1
Name: Greg
Hobby 1: Swimming
Hobby 2: Football
*
List 2
Name: Bob
Hobby 1: Skydiving
*

Here's what I tried so far:

String s = "";
try{
Scanner scanner = new Scanner(new File("file.txt"));
while(scanner.hasnextLine()){
s = scanner.nextLine();
}
}catch(Exception e){
e.printStackTrace}
String [] array = s.split("*");
String x = array[0];
String y = array[1];
m1759
  • 25
  • 3

1 Answers1

0

Your code has multiple issues like @Henry said that your string contains only the last line of the file and also you misunderstood the split() because it takes a RegularExpression as a parameter.

I would recommend you to use the following example because it works and is a lot faster than your approach.


Kick-Off example:

// create a buffered reader that reads from the file
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("test.txt")));

// create a new array to save the lists
ArrayList<String> lists = new ArrayList<>();

String list = ""; // initialize new empty list
String line; // initialize line variable

// read all lines until one becomes null (the end of the file)
while ((line = reader.readLine()) != null) {
    // checks if the line only contains one *
    if (line.matches("\\s*\\*\\s*")) {
        // add the list to the array of lists
        lists.add(list);
    } else {
        // add the current line to the list
        list += line + "\r\n"; // add the line to the list plus a new line
    }
}

Explanation

I'm going to explain special lines that are hard to understand again.


Looking at the first line:

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

This line creates a BufferedReader that is nearly the same like a Scanner but it's a lot faster and hasn't as much methods as a Scanner. For this usage the BufferedReader is more than enough.

Then it takes an InputStreamReader as a parameter in the constructor. This is only to convert the following FileInputStream to a Reader.

Why should one do that? That's because an InputStreamReader. An InputStream returns the raw values and a Reader converts it to human readable characters. See the difference between InputStream and Reader.


Looking at the next line:

ArrayList<String> lists = new ArrayList<>();

Creates a variable array that has methods like add() and get(index). See the difference of arrays and lists.


And the last one:

list += line + "\r\n";

This line adds the line to the current list and adds a new line to it.

"\r\n" Are special characters. \r ends the current line and \n creates a new line.

You could also only use \n but adding \r in front of it is better because this supports more Os's like Linux can have problems with it when \r misses.


Related

Using BufferedReader to read Text File

fuggerjaki61
  • 822
  • 1
  • 11
  • 24
  • I see thank you for that. I will need to look into Regular Expressions a bit more before I can implement it into my own code. Would it be possible to stop the buffered reader from reading further from the *(asterisk) and assign the values before it to a String, and the values after it, into a different string? – m1759 Jan 03 '21 at 14:14
  • @m1759 As far as I understand your question, this is already done. The `ArrayList` contains two values when running the example: one before the asterisk, one after the asterik and if there are more lists, each list will be in an own string. You can access them by `lists.get(0)` and `lists.get(1)` (this is the same as `array[0]` and `array[1]`). If you've never worked with Lists before, I recommend you to read [this](https://www.w3schools.com/java/java_arraylist.asp) article about the `ArrayList`. – fuggerjaki61 Jan 03 '21 at 15:08