Results 1 to 6 of 6
  1. #1
    tuts73 is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Read in from file and sort in array

    Hi all, im new to Java and just wondering if you could help out.....

    I have a list of names in a standard text file that need to be sorted. Im guessing the best way to do this is to read the contents of the file and store in an array, then use a sorting algorithm to sort the contents.

    So far i have been able to read in the file - but now struggling to apply some code to store and sort......would appriciate any help. Thanks

    The code i have so far.....

    public class MSL{


    public static void main(String[] args)throws IOException{
    String contents;
    File f = new File("MyList.txt");
    FileReader fr = new FileReader(f);
    BufferedReader br = new BufferedReader(fr);
    while (br.ready()){
    contents = br.readLine();
    System.out.println(contents);
    }
    fr.close();

    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,437
    Blog Entries
    7
    Rep Power
    20

    Default Re: Read in from file and sort in array

    First complete your code by storing each line in an array and then read the API documentation for the Arrays class; it has the method you need.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    tuts73 is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Read in from file and sort in array

    Thanks Jos for replying... what code can i use to store the API data into the array - any suggest

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,437
    Blog Entries
    7
    Rep Power
    20

    Default Re: Read in from file and sort in array

    Quote Originally Posted by tuts73 View Post
    Thanks Jos for replying... what code can i use to store the API data into the array - any suggest
    I don't know what you mean by 'API data'. You're reading lines (Strings) and you have to store Strings in your array. afaik there is no pre-built method that that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    tuts73 is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Read in from file and sort in array

    Sorry i didnt know what API means....i know how to create an array but not how to store it directly from file

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,437
    Blog Entries
    7
    Rep Power
    20

    Default Re: Read in from file and sort in array

    Here is the API documentation for Java version 6 and this is for Java version 7. Bookmark at least one of them because you'll need it. It documents all classes/interfaces and methods available to you; you can't do without it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 11
    Last Post: 01-27-2012, 10:32 PM
  2. Replies: 1
    Last Post: 08-05-2011, 09:21 PM
  3. Read text file and sort in ArrayList
    By Tabula Rasa in forum New To Java
    Replies: 7
    Last Post: 04-17-2011, 04:08 AM
  4. Read File into 2d array
    By almjodla in forum New To Java
    Replies: 8
    Last Post: 03-23-2010, 02:55 PM
  5. Replies: 2
    Last Post: 01-26-2009, 04:28 PM

Tags for this Thread

Posting Permissions

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