0

So I'm trying to read some numbers from a file and put them into an array. I've been reading about people having problems with whitespace, so using trim, I did it like this:

String[] tokens = new String[length];
 for(int i = 0; i<length;i++){
    String line = fileReader.nextLine();
    line = line.trim();
    tokens = line.split("");
    }

But the first element this array (token[0]) becomes empty. Am I using the split function wrong?

4
  • You are converting line to character array. You want to split the given line with space? Commented Apr 19, 2013 at 10:40
  • MAybe change split function parameter Commented Apr 19, 2013 at 10:41
  • 1
    Yes, the argument to split(...) is the (regex) delimiter. So if your input is eg. 1, 2, 4, 8 you could do split( "," ).' Commented Apr 19, 2013 at 10:42
  • Ok, thank you very much, I will work with the delimiter now. Commented Apr 19, 2013 at 10:43

2 Answers 2

1

You need to tell the split method what character it should split on. Try this:

tokens = line.split(" "); //split on a space character
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah! Thank you! Sometimes it's just right there in front of me!
1
tokens = line.split(" ");

You forgot whitespace.

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.