Results 1 to 8 of 8
Thread: String split help
- 01-18-2011, 08:24 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
String split help
Hey all
I need help using the split method on a file that is input through the buffered reader.
I have tried using the Tokenizer class but it has just made things more complicated, and I was recommended the split method.
So I've got the input file stuff figured out, now I'm just struggling to use the split method to refer to the file.
''
FileReader FILE = new FileReader("filename.filetype");
BufferedReader BUFILE = new BufferedReader (file);
''
any help will be greatly appreciated
- 01-18-2011, 08:30 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
How do you want to split the file contents? You first have to read the file, then parse the file contents while (or after) reading using either a tokenizer or String.split(). For example, you can read each line using the BufferedReader.readLine method, then parse the line based upon a given regular expression/string using the split method. How you do so is dependent upon how you wish to parse the file.
Last edited by doWhile; 01-18-2011 at 08:34 PM.
- 01-18-2011, 08:38 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
thanks for the quick reply.
Well the file will contain with numbers so I was planning on reading each line like you said then splitting the numbers on each line. The numbers will all have spaces between them so that's what I'll be using.
I'm having trouble getting the split method to split the file that has been input because it's not a string so I'm looking for a way around this problem.
cheers
- 01-18-2011, 09:22 PM #4
How you do this is still dependant upon what you want to do, which you still haven't fully explained.
You could use a BufferedReader or Scanner to read the file line by line and use the split method on a space. I'm sure you can find a good example if you Google.
Or you can use a Scanner to read each number one at a time. Once again I'm sure a Google search will give you a good example of how Scanner works.
Try one or both approaches, write some code and if/when you get stuck come back with your code, exact error messages (if you get them) and ask a specific question.
- 01-18-2011, 11:01 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
Thanks for the tip on using scanner, that seems to be ideal for what I need.
Basically, I need to read integers from a file and store them in an array for later use.
''''code
Sorry if I'm not making sense, been working at this for a while now and have started from scratch a few times.
Scanner sounds the like the best fit but the examples i've seen use buffered/filereader.
The thing is, this code gives me the integers BUT it changes the order of them. It's important that the order doesn't change.
thanks for your help so far.Last edited by YoungJavaBoy; 01-18-2011 at 11:33 PM. Reason: removing code
- 01-18-2011, 11:12 PM #6
Then why do you sort them?
- 01-18-2011, 11:15 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
That's a good question.
Told you I was tired. I guess that's what happens when you try and use examples, but I'm so stuck it's what I'll have to do.
Anyways, thanks for all your help everyone
- 01-19-2011, 01:39 AM #8
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
here's one way you can split your lines and get the integers
other ways include going over the each string , checking for a digit using Character.isDigit() for example.Java Code:Scanner sc = new Scanner( new File("file") ); String line ; while( sc.hasNext() ){ line = sc.nextLine(); String[] tokens = line.split("\\s+"); for(String s : tokens){ if ( s.matches("^[0-9]+$") ) System.out.println( s ); } }
Similar Threads
-
Split a String with split()--Help
By danilson in forum New To JavaReplies: 7Last Post: 11-19-2010, 04:08 PM -
Split string help
By Ben1 in forum New To JavaReplies: 1Last Post: 11-08-2010, 04:28 PM -
String Split
By sarovarc in forum New To JavaReplies: 6Last Post: 04-19-2010, 05:06 AM -
How to split a String using split function
By Java Tip in forum java.langReplies: 4Last Post: 04-17-2009, 08:27 PM -
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