Home Answers Viewqa Java-Beginners Sorting an ArrayList

 
 


Zubeda
Sorting an ArrayList
0 Answer(s)      a year and a month ago
Posted in : Java Beginners

print("code sample");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

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\\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]);   
         for (int i =0; i<propids.size();i++)
             propids.add(fields[0]);
               {     
                Collections.sort(propids);
                Iterator i =propids.iterator();

                System.out.println(pids);


                in.close();
               }
            }
      //close Print Writer
        pwo.flush();
        pwo.close();
      }catch(Exception e){
               }
        }
    }

I am really new at Java...This is only the second program I am writing for school and this site was very helpful in pointing out a key error I had in my first program, so I am hopeful you will be able to help me now.

Thank you in advance for any assistance!!!!

View Answers









Related Pages:
Sorting an ArrayList
Sorting an ArrayList  print("code sample");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
Sorting arraylist without using Collection.sort()
Sorting arraylist without using Collection.sort()  Hi, How can I sort an arraylist without using Collection.sort() nad also I am not allowed to use any other data structure in the program? Is there any algorithm by using which I
ADT ,data structure (ArrayList), sorting
ADT ,data structure (ArrayList), sorting  Write a program to calculate a bonus for 10 employees of WAFA Supermarket. The program consists... in different arrayList depending on his/her position (cashier or supervisor
ArrayList
Java Notes: ArrayList java.util.ArrayList allows for expandable arrays, and is the Collections replacement for the older Vector class. An ArrayList has the following advantages over an array: An ArrayList automatically expands
ArrayList
Java Notes: ArrayList java.util.ArrayList allows for expandable arrays, and is the Collections replacement for the older Vector class. An ArrayList has the following advantages over an array: An ArrayList automatically expands
SORTING - Java Interview Questions
SORTING  HOW to sort the intger elemnts only in arraylist?  ...{ public static void main(String args[]) { ArrayList list= new ArrayList...("Items after sorting are ="+list); } } Thanks
sorting
sorting   write a program to arrange sorting in rows,column and diagonal
Alphabetically sorting order
Alphabetically sorting order  Write a java program that takes a list of words from the command line and prints out the arguments in an alphabetically...(st); } System.out.println("ArrayList Elements in Sorted order
Sorting
Sorting  can any help me know which sorting algorithm java uses for sorting collection and arrays
sorting
sorting  write to a program to arrange the sorting in rows, column and digonal
sorting
sorting  how to do sorting without using bubble sort,selection sort
ArrayList<E>
. Works efficiently with both ArrayList and LinkedList. Sorting If the data in your ArrayList has a natural sorting order (ie, implements Comparable... class. An ArrayList has these characteristics: An ArrayList automatically
arraylist
arraylist   Using arraylist class , what type of exception are throws, how can write a java programe   An ArrayList can throw... ArraylistException { void buildAlphabet() { ArrayList list = new ArrayList
Java arraylist sort
ArrayList doesn't have sort() method. We can use the static sort ... of Java Arraylist Sort import java.util.ArrayList; import... main(String[] args) {      Integer ar[]={444,222,333,111};      ArrayList list
ArrayList (non-generic)
))); } Sorting If the data in your ArrayList has a natural sorting order (ie... Java Notes: ArrayList (non-generic) java.util.ArrayList allows.... An ArrayList has the following advantages over an array: An ArrayList automatically
Sorting in Java
Sorting in Java  Sorting in Java
arraylist
arraylist  Hi How can we eliminet duplicate element from arraylist? How can we find highest salary from arraylist ? How can we highest key value pair from map? Thanks Kalins Naik   Remove duplicates from Arraylist
arraylist
arraylist  Hi how can we eliminet duplicate element from arraylist in java? how can we achieve data abstrcation and encapulation in java? how many type of modifier are there in java? Thanks kalins anik   Remove
arraylist
arraylist  Hi i have class A , i have added employee name and id in arraylist, then how can i find out all infomation of class A using emplyee... data into an arraylist and display the data of the particular employee according
arraylist
% *(noOfYearService/2). Store each employee information in different arrayList depending on his
How to sort ArrayList in java
and sorting by sort method of collection, lastly displaying the sorted arraylist...How to sort ArrayList in java In this section you will learn how to sort ArrayList in java. ArrayList support dynamic array that can grow as needed. Array
arraylist of an arraylist
arraylist of an arraylist  Can anyone suggest me how to use arraylist of an arraylist?? how to put data into it and get data from it???? becoz i want to make rows and column dynamic which can grow as per requirement?????/ plz
group by sorting
group by sorting  group by sorting of data in SQL
Sorting data in a jsp using a servlet while implementing comparable - JSP-Servlet
Sorting data in a jsp using a servlet while implementing comparable  ... BookManagementForm { private ArrayList books; private Book currentBook; private ArrayList errorMsg; public ArrayList getBooks() { return books; } public void setBooks
Sorting Arrays
computer science topic it sorting, putting a collection of data in some order..., ArrayList), and there are data structures such as TreeMap that keep elements sorted based on a key. Sorting Arrays with Arrays.sort
linear sorting
linear sorting  what is linear sorting? can any send me an example   Hi Friend, Try this: import java.util.*; class ArraySort{ public...("After Sorting: "); for(int i=0;i<arr.length;i++){ System.out.println
Sorting Program
Sorting Program  To sort 10items in alphabetical order   Arrays.sort(name of ur array)   import java.util.*; class ArrayExample{ public static void main(String[] args) { String array[]=new
sorting numbers
sorting numbers  How to sort the numbers in ascending order   import java.util.*; class SortNumbers{ public static void main(String[] args) { Scanner input=new Scanner(System.in
Java sorting
sorting mechanism. say about Collections.Sort() & Arrays.Sort() that uses
sorting and storing data
sorting and storing data   sorting and storing data in UITableView
Sorting and Searching
Sorting and Searching  Hi i would really be thankful if someone could help me with this A program is required to ask users to rate the Java programming language using a number in the range 0 - 10. The data input is ended
Sorting the array
Sorting the array  Implement a program to process votes for 5 candidates in a talent contest. The program should use a String array to hold the names of the 5 candidates and an integer array to record the number of votes for each
arraylist and vector
arraylist and vector  what is the diff b/w arraylist and vector
Java ArrayList
Java ArrayList   How to declare unique ArrayList
ArrayList and Vector
ArrayList and Vector  hello, Why ArrayList is faster than Vector?   hii, ArrayList is faster than Vector, because ArrayList is not synchronized. Synchronization will reduce the performance of Vector
arraylist in java
arraylist in java  arraylist in java   Array List Example in java
sorting Line in c
sorting Line in c  Hi I need help with sorting line in programming c asking user to enter 1-10 characters and then sort them in lower case, alphabetical and ascending order. Any help will be appreciated! please help! void
Imp-HTML Table Sorting
Imp-HTML Table Sorting   Hi, I am working on a project and as per requirement from client i have to sort a table based on the ID Column. I want... of HTML table sorting where sorting happenes whenever user clicks on column header
ArrayList object
ArrayList object  i have am ArrayList object it containg data base records in it, now i want to display this ArrayList object data in my jsp programe, plz help me
Sorting along life span
Sorting along life span  how to find total age in date of birth and date of death.and also sort along life span.using object class and collection API
ArrayList elements
ArrayList elements  how to print ArrayList elements in reverse order... ArrayListReverse { public static void main(String[] args) { ArrayList list=new ArrayList(); list.add("Innova"); list.add("Alto
Java arraylist of arraylist
Java arrayList can make the use of other arrayList.  In one arrayList other arrayList can be added as the object  It works as two dimension array. Example of Java Arraylist of Arraylist import
Array Sorting - Development process
Array Sorting  Hello sir, Now i m doing various Array sorting(Bubble,insertion,Quick,merge etc ) I want to display Array Sort in steps by step like... is the code to display array sorting with moving graphics(color)in steps