4 Replies - 379 Views - Last Post: 15 March 2013 - 01:23 PM Rate Topic: ***-- 1 Votes

#1 BTJ1971  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-March 13

using a comparator with arrays to sort temperatures

Posted 07 March 2013 - 10:26 PM

I have been trying to get these comparators to work, and I seem to keep getting errors (red squiggly underline and X)I am trying to read from a list I made from a data file chosen by the user. I get the file and make it into a list, but I can't seem to get my comparator to work. I have a class called WeatherData, and one that is called Weather Almanac. I am trying to get the high temp and the low temp from the list. I have looked on the internet and looked for help and I have read chapters of how to write code in Java, for beginners, java 7 a tutorial, etc. I am still confused. My code looks like this:
	// Create an Array to hold the weather data.
        //   There are exactly 365 WeatherData objects.
        //   Name the variable 'list'.
		WeatherData [] list = new WeatherData [365];
        // Open the data file.
        Scanner in = null;
		try
		{
			in = new Scanner (inputFile);
		} catch (FileNotFoundException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
        // Read in all the data, create the weather objects, and store them in the array.
        for (int i = 0 ;i<365; i++)
        {
        	String date = in.next();
        	int high = in.nextInt();
        	int low = in.nextInt();
        	list[i] = new WeatherData(date, high, low);		
		}
        {
       /*
        *Sort the list of WeatherData objects to find the high temp of the year and the low temp of the year.
        *used to find the 10 hottest and coldest days in the year, to find how many coldfronts occurred in the year
        *and to find the number of 'perfect' days in the year.  Perfect days are those whose low > 50 and a High < 80.
        *
         * 
         */
        Arrays.sort(list, new WeatherHighComparator());
        }
        {
        	// This comparator will compare the Low temperatures.
           // Arrays.sort(list, new WeatherLowComparator());
        }
    }	
	// This is for my comparator classes.  They will implement the Comparator<weatherData> interface.
	private static class WeatherHighComparator implements Comparator<WeatherData>
    {
				
    	public int compare (WeatherData a, WeatherData B)/>/>/>
    	{
    		// Put 'if' statements here that compare a and b for order
            //   Use your accessor methods to get high Temps and Low Temps from a and b
            //   Return -1 if a comes before b
            //   Return 1 if a comes after b
            //   Return 0 if a and b are equal.
    		
    		if(a.getHigh() > b.getHigh())
    			return -1;
    		if(a.getHigh() < b.getHigh())
    			return 1;
    		
    		return 0;
    	}
     	
    private static class WeatherLowComparator implements Comparator<WeatherData>
    {
    	public int compare (WeatherData a, WeatherData B)/>/>/>
    	{
    		if(a.getLow() > b.getLow())
    			return 1;
    		if(a.getLow() < b.getLow())
    			return -1;
    		
    		return 0;
    	}
    }
    System.out.println(high);
    System.out.println(low);
}]
and my Class with all the other methods look like this:[
public class WeatherData
{
	Scanner in;
	
	private String date;
	private int high;
	private int low;
	
	public WeatherData(String date, int high, int low )
	{
		this.setDate(date);
		this.setHigh(high);
		this.setLow(low);
	}

	public String getDate()
	{
		return date;
	}

	public void setDate(String date)
	{
		this.date = date;
	}

	public int getHigh()
	{
		return high;
	}

	public void setHigh(int high)
	{
		this.high = high;
	}

	public int getLow()
	{
		return low;
	}

	public void setLow(int low)
	{
		this.low = low;
	}
	
}


Is This A Good Question/Topic? 0
  • +

Replies To: using a comparator with arrays to sort temperatures

#2 pbl  Icon User is offline

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

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: using a comparator with arrays to sort temperatures

Posted 08 March 2013 - 04:53 AM

Note that your compare() methods can simply be written as

return a.getHigh() - b.getHigh();

and

return a.getLow() - b.getLow();

At the end you println() a high and a low where are they coming from ?
Was This Post Helpful? 1
  • +
  • -

#3 BTJ1971  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-March 13

Re: using a comparator with arrays to sort temperatures

Posted 15 March 2013 - 01:10 PM

I have the information being pulled from a data file chosen by the user. I used a file chooser. Here is the code I used
public class WeatherAlmanac
{
	
	public static void main(String[] args) 
	{
		
		//Create a file chooser	
		//Create the scanner, Get the file selected and place it into an array to compare the data.
		//Get user to select a file using a GUI interface (jfilechooser/Scanner)
		
		JFileChooser chooser = new JFileChooser();
		//Declare the variables for the chooser
		int result;
		result = chooser.showOpenDialog(chooser);
		//ask for the file name using the file chooser
		chooser.setDialogTitle("Choose the weather file would you like to analyze?");

		File inputFile = null;  
		if (result == JFileChooser.CANCEL_OPTION)
		{
			// User cancelled the chooser.
			return;
		}
		inputFile = chooser.getSelectedFile();
		
		// Create an Array to hold the weather data.
        //   There are exactly 365 WeatherData objects.
        //   Name the variable 'list'.
		WeatherData [] list = new WeatherData [365];
        // Open the data file.
        Scanner in = null;
		try
		{
			in = new Scanner (inputFile);
		} catch (FileNotFoundException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
        // Read in all the data, create the weather objects, and store them in the array.
        for (int i = 0 ;i<365; i++)
        {
        	String date = in.next();
        	int high = in.nextInt();
        	int low = in.nextInt();
        	list[i] = new WeatherData(date, high, low);		
		}
        {
       /*
        *Sort the list of WeatherData objects to find the high temp of the year and the low temp of the year.
        *used to find the 10 hottest and coldest days in the year, to find how many coldfronts occurred in the year
        *and to find the number of 'perfect' days in the year.  Perfect days are those whose low > 50 and a High < 80.
        *
         * 
         */
        Arrays.sort(list, new WeatherHighComparator());
      

and My constructor looks like this:
import java.util.Scanner;

public class WeatherData
{
	Scanner in;
	
	private String date;
	private int high;
	private int low;
	
	public WeatherData(String date, int high, int low )
	{
		this.setDate(date);
		this.setHigh(high);
		this.setLow(low);
		System.out.println("The hottest day of the year was: " + high);
	}
	

	public String getDate()
	{
		return date;
	}

	public void setDate(String date)
	{
		this.date = date;
	}

	public int getHigh()
	{
		return high;
		
	}

	public void setHigh(int high)
	{
		this.high = high;
	}

	public int getLow()
	{
		return low;
	}

	public void setLow(int low)
	{
		this.low = low;
	}
	
}


I just don'
t understand what else I do, I have been having a difficult time learning this part of java.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,479
  • Joined: 27-December 08

Re: using a comparator with arrays to sort temperatures

Posted 15 March 2013 - 01:21 PM

Look here System.out.println("The hottest day of the year was: " + high);. The WeatherData class models a single day. The WeatherData object doesn't know if it was the hottest day of the year or not. Think about this in terms of an int[]. The ints themselves don't know if they are the largest. You have to loop through the array and find the largest. The same applies here with the WeatherData objects. As a note as well, the WeatherData objects shouldn't have Scanners. A separate class should be reading in the data, not the WeatherData objects themselves.
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,217
  • Joined: 20-September 08

Re: using a comparator with arrays to sort temperatures

Posted 15 March 2013 - 01:23 PM

There's absolutely nothing there that implements Comparable. Look at the api docs for that class
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1