Results 1 to 4 of 4
Thread: split method question
- 11-19-2009, 10:34 AM #1
Member
- Join Date
- Jul 2009
- Posts
- 56
- Rep Power
- 0
split method question
I have a file that contains
1 2 3
4 5 6
7 8 9
I have a scanner that reads it, puts it into a String, and then attempts to use the split method to tokenize it. The problem is that it isn't counting \n as a delimiter.
That is the relevant code. I was under the impression \\s would cover all whitespace which includes \n. Can anyone help me?Java Code:try { Scanner scan = new Scanner(theFile); while(scan.hasNextLine()) { n++; stringForm += scan.nextLine(); } //end while //make array square = new int[n][n]; //put ints into array String[] tokens = stringForm.split("\\s"); for(int i=0;i<tokens.length;i++) { //loop through array for(int rowN=0;rowN<n;rowN++) { for(int colN=0;colN<n;colN++) { System.out.println(tokens[i]); square[rowN][colN] = Integer.parseInt(tokens[i]); //initialize array
- 11-19-2009, 10:47 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
The \n are no longer there anyway, try
to see what happens.Java Code:stringForm += scan.nextLine()+" ";
- 11-19-2009, 06:19 PM #3
Senior Member
- Join Date
- Sep 2009
- Location
- Sweden/Borås
- Posts
- 107
- Rep Power
- 0
I split my whitespace with
Java Code:String[] tokens = stringForm.split(" ");
But mabey thats wrong aint that old at java.
- 11-19-2009, 07:19 PM #4
Member
- Join Date
- Jul 2009
- Posts
- 56
- Rep Power
- 0
Similar Threads
-
method question
By xplayerr in forum New To JavaReplies: 7Last Post: 11-11-2009, 04:13 PM -
How to split a String using split function
By Java Tip in forum java.langReplies: 4Last Post: 04-17-2009, 08:27 PM -
problem with split method
By abhiN in forum New To JavaReplies: 7Last Post: 02-10-2009, 01:54 PM -
Simple Method Question
By Froz3n777 in forum New To JavaReplies: 2Last Post: 02-13-2008, 02:39 AM -
How to split a String using split function
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks