Convert simply text file into array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bjork
    New Member
    • May 2015
    • 1

    Convert simply text file into array

    Hello.. i would like to ask you. Its long time i did something in java.. and now I am lost. :)
    I have text file with constant elements in line but variable number of lines... text file looks like
    asdf 12 54 12
    brjk 45 2 14
    etc..
    I want to make array from this file. Can you help me with my code? This not working..

    Code:
    String[] part = new String[5];
            BufferedReader f = null;
            try{
                f = new BufferedReader(new FileReader(filename));
                String something=null;
                while((something=f.readLine())!=null){
                    part = something.split(" ");
                System.out.print(part[0]);
                }
             
            } catch (IOException ex) {
                Logger.getLogger(Hlavni.class.getName()).log(Level.SEVERE, null, ex);
    }

    all comments are appreciated..
    Last edited by Rabbit; May 30 '15, 05:26 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    What error message do you get? Please show us the exact error message, so we can help.
    Maybe you should test in your code if the splitted string array contains at least 1 element before accessing the first element.

    Comment

    Working...