Results 1 to 8 of 8
  1. #1
    YoungJavaBoy is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default 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

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    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.

  3. #3
    YoungJavaBoy is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    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

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

  5. #5
    YoungJavaBoy is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    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

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Then why do you sort them?

  7. #7
    YoungJavaBoy is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    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

  8. #8
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    here's one way you can split your lines and get the integers
    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 );
                }
            }
    other ways include going over the each string , checking for a digit using Character.isDigit() for example.

Similar Threads

  1. Split a String with split()--Help
    By danilson in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 04:08 PM
  2. Split string help
    By Ben1 in forum New To Java
    Replies: 1
    Last Post: 11-08-2010, 04:28 PM
  3. String Split
    By sarovarc in forum New To Java
    Replies: 6
    Last Post: 04-19-2010, 05:06 AM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •