The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Splitting a txt file with multiple spaces OR tabs

Now i believe this is an easy one, Although i have tried quiet a few methods of doing this.. The textfile data that my clients other program generates seperates the collumns all with a different ammount of spacing.

Here is the code i have written to try to split the data into a java 2D array:

although i may have just relized that it could be split through tabs instead of spaces.. will try it and post back
EDIT: Nope it is still giving me a null error, Below is the error messages spitting out at me:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at newFile.doMaths(newFile.java:30)
at newFile.<init>(newFile.java:13)
at calculate.<init>(calculate.java:17)
at updateData.<init>(updateData.java:16)
at correctData.<init>(correctData.java:25)
at correctData.main(correctData.java:211)

Last edited by macko; November 1st, 2011 at 09:31 AM.
Reason: posting update

Re: splitting file using /t

I'm clueless lol xD I'm probably just to stupid to see past the fact, Although if the temp[0] is posting out correctly and the temp[1] should have the correct amount of data in it as it has been stopping at data 34.. It for some reason cannot read further into the array which i have no idea why as they rest of the array also includes the tab in between each column.

What i'm expecting to haphen: the data splits into the correct arrays so that i may continue with calculations.

Whats haphening? : Giving me a index out of range error saying that temp[1] is out of bounds when it does read up to line 34.

This is temp[1] .. the temp[0] reads until line 886

Cnj i is equal to: 0
Sxt i is equal to: 1
Sqr i is equal to: 2
Tri i is equal to: 3
Sxt i is equal to: 4
Opp i is equal to: 5
Sqr i is equal to: 6
Sqr i is equal to: 7
Opp i is equal to: 8
Sxt i is equal to: 9
Sqr i is equal to: 10
Sqr i is equal to: 11
Opp i is equal to: 12
Sqr i is equal to: 13
Sxt i is equal to: 14
Sxt i is equal to: 15
SSq i is equal to: 16
Sxt i is equal to: 17
SSq i is equal to: 18
SSq i is equal to: 19
Cnj i is equal to: 20
Sxt i is equal to: 21
Cnj i is equal to: 22
Sqr i is equal to: 23
Tri i is equal to: 24
Cnj i is equal to: 25
Sqr i is equal to: 26
Sxt i is equal to: 27
Tri i is equal to: 28
Sqr i is equal to: 29
Sxt i is equal to: 30
Tri i is equal to: 31
Sxt i is equal to: 32
Sxt i is equal to: 33
Sxt i is equal to: 34
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at newFile.doMaths(newFile.java:30)
at newFile.<init>(newFile.java:13)
at calculate.<init>(calculate.java:17)
at updateData.<init>(updateData.java:16)
at correctData.<init>(correctData.java:25)
at correctData.main(correctData.java:211)

Re: Splitting a txt file with multiple spaces

What does line equal before you split it? You can further simplify the problem by narrowing it down to that and getting rid of all the rest.

And since the split() function is looking for a regular expression, I suggest you do some reading up on the Pattern class API:

Backslashes within string literals in Java source code are interpreted as required by the Java Language Specification as either Unicode escapes or other character escapes. It is therefore necessary to double backslashes in string literals that represent regular expressions to protect them from interpretation by the Java bytecode compiler. The string literal "\b", for example, matches a single backspace character when interpreted as a regular expression, while "\\b" matches a word boundary. The string literal "\(hello\)" is illegal and leads to a compile-time error; in order to match the string (hello) the string literal "\\(hello\\)" must be used.

Re: Splitting a txt file with multiple spaces

Just print out the value of line to make sure it is what you want.

When you're sure of that, write a simple (< 10 lines) program that does the split on that line (hardcoded). Does that work how you expect? If not, read over what I posted from the API, or better yet, consult the API itself.