Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to rearrange ArrayList from text file in Java

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to rearrange ArrayList from text file in Java

    Actually I'm working on a class to read a text file that contains the following elements:

    Ringer-ID-0: USB\Device_Name_Here
    Ringer-ID-0: {2344343r4efdwdd2w2sdwq}
    Ringer-ID-0: volume =100;
    Ringer-ID-0: speaker = off;
    Ringer-ID-1: HDAUDIO\Device_Name_Here
    Ringer-ID-1: {4r3444ewdwdaedw3re3d34d3}
    Ringer-ID-1: volume = 93;
    Ringer-ID-1: speaker = off;
    Ringer-ID-2: HDAUDIO\Device_Name_Here
    Ringer-ID-2: {23edecrrvt5y66h6hyyhy66}
    Ringer-ID-2: volume = 93;
    Ringer-ID-2: speaker = off;
    Ringer-ID-3: HDAUDIO\Device_Name_Here
    Ringer-ID-3: {3444444t56756y7h7h6ef}
    Ringer-ID-3: volume = 93;
    Ringer-ID-3: speaker = off;
    Ringer-ID-4: USB\Device_Name_Here
    Ringer-ID-4: {r4rfrf54g65g65g6h7uj7}
    Ringer-ID-4: volume = 50;
    Ringer-ID-4: speaker = on;

    what I need to do with these elements is to rearrange them in order to have all HDAUDIO devices with its values in the top of my list and USB devices in the bottom like this:

    Ringer-ID-0: HDAUDIO\Device_Name_Here
    Ringer-ID-0: {4r3444ewdwdaedw3re3d34d3}
    Ringer-ID-0: volume = 93;
    Ringer-ID-0: speaker = off;
    Ringer-ID-1: HDAUDIO\Device_Name_Here
    Ringer-ID-1: {23edecrrvt5y66h6hyyhy66}
    Ringer-ID-1: volume = 93;
    Ringer-ID-1: speaker = off;
    Ringer-ID-2: HDAUDIO\Device_Name_Here
    Ringer-ID-2: {3444444t56756y7h7h6ef}
    Ringer-ID-2: volume = 93;
    Ringer-ID-2: speaker = off;
    Ringer-ID-3: USB\Device_Name_Here
    Ringer-ID-3: {2344343r4efdwdd2w2sdwq}
    Ringer-ID-3: volume =100;
    Ringer-ID-3: speaker = off;
    Ringer-ID-4: USB\Device_Name_Here
    Ringer-ID-4: {r4rfrf54g65g65g6h7uj7}
    Ringer-ID-4: volume = 50;
    Ringer-ID-4: speaker = on;

    I thought first store them in an Array List but I don't know how would be the better way to achieve this, please give a hand, thanks in advance

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.util.ArrayList;
     
     
     
    public class RearrangeItems {
        public static void main(String[] args) {
            try {
                BufferedReader reader = new BufferedReader(new FileReader(new File("C:\\Users\\John\\Desktop\\items.txt")));
                ArrayList<String> list = new ArrayList<String>();
                String line = reader.readLine();
                while (line != null) {
                    list.add(line);
                    line = reader.readLine();
                }
                reader.close();
                String[] records = (String[]) list.toArray(new String[] { "" });
                for (int i = 0; i < records.length; i++) {
                    System.out.println(records[i]);
                }
     
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
    }
    Last edited by jcaviles85; May 1st, 2014 at 12:45 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,083
    My Mood
    Mellow
    Thanks
    244
    Thanked 752 Times in 738 Posts

    Default Re: How to rearrange ArrayList from text file in Java

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post code correctly.

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    849
    Thanks
    0
    Thanked 158 Times in 141 Posts

    Default Re: How to rearrange ArrayList from text file in Java

    You can sort a list with the method "sort(List<T>, Comparator<? super T>)" from the Collections class.
    Doc: Collections (Java Platform SE 6)

    All you have to do is define a comparator for your data.
    I would advice you not to store each line as a string but to parse it into an apropriate object which you can compare.

Similar Threads

  1. Replies: 1
    Last Post: May 19th, 2013, 12:28 AM
  2. [SOLVED] Help - reading from a text file into an ArrayList
    By deeevo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 19th, 2013, 08:47 AM
  3. Reading into an ArrayList from a text file
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 18th, 2013, 05:32 PM
  4. Reading from a text file into an ArrayList
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 17th, 2013, 02:24 AM
  5. Reading a text file into an arraylist of fueldispensers.
    By vbhatti in forum Object Oriented Programming
    Replies: 3
    Last Post: November 20th, 2011, 01:17 PM