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: Sorting an ArrayList

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sorting an ArrayList

    Hello all,

    I am working on a program for school and I am having trouble with sorting my list. The numbers I receive in my output are correct, but not in ascending order as required.

    Here is my current code

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package gtt1_task2b;
     
    import java.util.Scanner;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.FileReader;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Iterator;
    /**
     *
     */
    public class GTT1_Task2B{
     
          public static void main(String[] args) throws FileNotFoundException, IOException
        {
         // Prompt for the input and output file names
            Scanner properties = new Scanner(System.in);
            System.out.print("Input file: ");
            String inputFile = properties.next();
         // Create variables
            String id = null ;
            BufferedWriter pwfo = null;
            try{
            pwfo = new BufferedWriter(new FileWriter("C:\\Users\\zubeda.a.hemani\\Documents\\NetBeansProjects\\GTT1_Task2\\src\\gtt1_task2\\overview.txt"));
            } catch (IOException e){    
               }
               PrintWriter pwo = new PrintWriter(pwfo);
            File input = new File(inputFile);
            BufferedReader in = new BufferedReader(new FileReader(input));
            int count = 0;
            double sum = 0;
            //int i = 0;
            //ArrayList<Double> plist = new ArrayList<Double>();
     
      while ((id = in.readLine())!= null)     
                {
                count++;
                String[] proplist = id.split("[\\s}]");
                Double pvalue = Double.parseDouble(proplist[2]);
     
                for (int i=0; i < proplist.length; i++)
               {
                    sum+= pvalue;
                   }        
                }
                System.out.println("Total properties listed:  " + count);
                System.out.println("Total value of properties listed: " + sum + "\n");
                in.close();
     
                try {
            ArrayList<String> propids = new ArrayList<String>();       
            Scanner idlist = new Scanner((input));
              //print property ids
     
               while (idlist.hasNextLine())
               {               
                 id = idlist.nextLine();
                 String[] fields =id.split("[\\s}]");
                 String pids = (fields [0]);   
                       {     
                        Collections.sort(propids);
                        Iterator j =propids.iterator();
                        System.out.println(pids);
                        for (int i =0; i<6;i++)
                        in.close();
                       }
                    }
              //close Print Writer
                pwo.flush();
                pwo.close();
              }catch(Exception e){
                       }
                }
            }

    I am not sure where I am going wrong. Any assitance is greatly appreciated.

    Thank you.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Sorting an ArrayList

    You have to debug the program to figure out where its execution differs from what you expect. Recommended reading: http://www.javaprogrammingforums.com...t-println.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sorting an ArrayList

    Thank you for the advice, I will try that.

Similar Threads

  1. ArrayList Sorting Problem
    By coke32 in forum Object Oriented Programming
    Replies: 3
    Last Post: April 29th, 2012, 08:53 AM
  2. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  3. Sorting ArrayList
    By leao in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 31st, 2011, 03:33 PM
  4. Sorting a Arraylist
    By waterjames in forum Java Theory & Questions
    Replies: 1
    Last Post: November 26th, 2010, 02:47 PM
  5. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM