0

I'm importing a text file with multiple lines like this

 0     2     23
 1     3     34
 2     4     45
12     5     56

I'm using this to read the file and split the values

while (txtFile.hasNext()) {

    String str = txtFile.nextLine();
    String[] parts = str.split("\\s+");

Based on this regex, the 1st three lines will have parts[1] [2] and [3], when it comes to the 4th line, it becomes parts[0] [1] and [2]

My question is which regex should I use to overcome this problem so it can read part[0] [1] and [2] for all the lines?

1 Answer 1

4

Trim the leading whitespace from the input String

String str = txtFile.nextLine().trim();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, so simple but couldnt think of trim()

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.