10 Replies - 3748 Views - Last Post: 23 June 2009 - 09:39 AM Rate Topic: -----

#1 usamor  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 03-March 09

reverse order in java

Post icon  Posted 22 June 2009 - 01:36 PM

can some help me with the following.
the code read .txt file and store its elements in an arraylist.
import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.List;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Random;
import java.util.Collections;


public class reverseOrder
 {
 	
	public static ArrayList loadFile(String fileName)
	{
		if ((fileName.length() == 0))
		{
			throw new IllegalArgumentException();
		}
		
		ArrayList<Double> file = new ArrayList<Double>();
			   Collections.reverse(file);
	
		try
		{
			
			BufferedReader reader = new BufferedReader(new FileReader("c://data.txt"));
			
			//Line to hold the line read from file
			String line = ""; 
			
			
			while ((line = reader.readLine()) != null)
			{
			
				StringTokenizer st = new StringTokenizer(line, ",");
				while (st.hasMoreTokens())
				{
					
					double tokenAsDouble = Double.parseDouble(st.nextToken().trim());
					file.add(tokenAsDouble);
					
					System.out.println(""+ tokenAsDouble);
					
				}
			}
		}catch (Exception e)
		{
			System.out.println("Exception while reading the file: " + e);
		}
		return file;
	}
	
	
	public static void main(String[] args)
	{
		ArrayList<Double> Close = loadFile("c://stocksstocks.txt");
	}
  }



how can i reverse the order of the elements of the arraylist.
i am using collections.reverse(file);
i am not getting any errors but also the arraylist is not reversed.
the data.txt file looks like:
90.43
78.22
10.55
60.76


i want it like this :
60.76
10.55
78.22
90.43

thanks

sorry this is the code
import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.List;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Random;
import java.util.Collections;


public class reverseOrder
 {
 	
	public static ArrayList loadFile(String fileName)
	{
		if ((fileName.length() == 0))
		{
			throw new IllegalArgumentException();
		}
		
		ArrayList<Double> file = new ArrayList<Double>();
		Collections.reverse(file);
	
		try
		{
			
			BufferedReader reader = new BufferedReader(new FileReader("c://data.txt"));
			
			//Line to hold the line read from file
			String line = ""; 
			
			
			while ((line = reader.readLine()) != null)
			{
			
				StringTokenizer st = new StringTokenizer(line, ",");
				while (st.hasMoreTokens())
				{
					
					double tokenAsDouble = Double.parseDouble(st.nextToken().trim());
					file.add(tokenAsDouble);
					
					System.out.println(""+ tokenAsDouble);
					
				}
			}
		}catch (Exception e)
		{
			System.out.println("Exception while reading the file: " + e);
		}
		return file;
	}
	
	
	public static void main(String[] args)
	{
		ArrayList<Double> Close = loadFile("c://stocksstocks.txt");
	}
  }



*Edited to balance the [ code] tags

This post has been edited by pbl: 22 June 2009 - 08:18 PM


Is This A Good Question/Topic? 0
  • +

Replies To: reverse order in java

#2 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: reverse order in java

Posted 22 June 2009 - 01:41 PM

what's the problem? if you are trying to output the arrays backwards. you would do it with a for loop as normal, but instead of making i=0 you would make it equal the amount of items in the array then i-- until i<=0. hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#3 usamor  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 03-March 09

Re: reverse order in java

Posted 22 June 2009 - 01:45 PM

if i use the for loop it will work
i want to use something like Collections.reverse(arraylist) is this possible without using the for loop
Was This Post Helpful? 0
  • +
  • -

#4 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: reverse order in java

Posted 22 June 2009 - 01:47 PM

i don't see why you would use that, but either way you would have to implement a decreasing for loop. be it inside that method or you manually create one.
Was This Post Helpful? 0
  • +
  • -

#5 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: reverse order in java

Posted 22 June 2009 - 01:58 PM

You dont have to implement a decreasing for loop to get the order reversed. The reason it looks like its not working for you is because your implement the reverse method before you even add any data to the ArrayList. So in essence, your reversing nothing. You need to call this method after you add data to your ArrayList.
Was This Post Helpful? 0
  • +
  • -

#6 usamor  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 03-March 09

Re: reverse order in java

Posted 22 June 2009 - 06:13 PM

i called the method after adding data
import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.List;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Random;
import java.util.Collections;


public class reverseOrder
 {
 	
	public static ArrayList loadFile(String fileName)
	{
		if ((fileName.length() == 0))
		{
			throw new IllegalArgumentException();
		}
		
		ArrayList<Double> file = new ArrayList<Double>();
	
	
		try
		{
			
			BufferedReader reader = new BufferedReader(new FileReader("c://khadija.txt"));
			
			//Line to hold the line read from file
			String line = ""; 
			
			
			while ((line = reader.readLine()) != null)
			{
			
				StringTokenizer st = new StringTokenizer(line, ",");
				while (st.hasMoreTokens())
				{
					
					double tokenAsDouble = Double.parseDouble(st.nextToken().trim());
					file.add(tokenAsDouble);
					Collections.reverse(tokenAsDouble);
					
					System.out.println(""+ tokenAsDouble);
					
				}
			}
		}catch (Exception e)
		{
			System.out.println("Exception while reading the file: " + e);
		}
		return file;
	}
	
	
	public static void main(String[] args)
	{
		ArrayList<Double> Close = loadFile("c://khadija.txt");
	}
  }


but i have an error:
reverse(java.util.List<?>)in java.util.Collections cannot be applied to (double)
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: reverse order in java

Posted 22 June 2009 - 06:29 PM

Guess it only applies to Objects and double is a basic datatype... not an Object as Double is
Was This Post Helpful? 0
  • +
  • -

#8 usamor  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 03-March 09

Re: reverse order in java

Posted 22 June 2009 - 07:18 PM

how can i solve this problem
what do i need to modify in my code to make it work and reverse the order of the arraylist.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: reverse order in java

Posted 22 June 2009 - 08:17 PM

The simplest I can make it

import java.util.*;

public class Reverse {

	public static void main(String[] arg) {
		ArrayList<Double> al = new ArrayList<Double>();
		al.add(new Double(1.0));
		al.add(new Double(2.0));
		al.add(new Double(3.0));
		Collections.reverse(al);
		for(Double dd : al) {
			System.out.println(dd);
		}		
	}
}


produces
3.0
2.0
1.0
:^: :D
Was This Post Helpful? 0
  • +
  • -

#10 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2071
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: reverse order in java

Posted 22 June 2009 - 11:34 PM

                    double tokenAsDouble = Double.parseDouble(st.nextToken().trim());
                    file.add(tokenAsDouble);
                    Collections.reverse(tokenAsDouble);



Here you're trying to reverse tokenAsDouble when you really want to reverse your list. ;)
Was This Post Helpful? 0
  • +
  • -

#11 usamor  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 03-March 09

Re: reverse order in java

Posted 23 June 2009 - 09:39 AM

thank you the program is working now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1