8 Replies - 374 Views - Last Post: 06 October 2012 - 09:07 PM Rate Topic: -----

#1 diagnonsense  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 11-February 12

Sorting a list of Integers

Posted 06 October 2012 - 07:27 PM

Hey, I'm getting a confusing error that I'm not sure how to fix, can anyone help me out?

import java.util.Scanner;
public class Numbers
{
	public static void main (String[] args)
	{
		int size;
		int[] intList;
	
		Scanner scan = new Scanner(System.in);
		System.out.print ("\nHow many integers do you want to sort? ");
		size = scan.nextInt();
		
		System.out.println ("\nEnter the numbers...");
		for (int i = 0; i < size; i++)
			intList[i] = scan.nextInt(); //THIS IS WHERE THE ERROR OCCURS
		
		IntegerList intList2 = new IntegerList(size);
		System.out.println(intList2);
		Integer[] intCopy = intList2.makeIntegers();
		Sorting.selectionSort(intCopy);
			
		System.out.println ("\nYour numbers in sorted order...");
		for (int i = 0; i < intCopy.length; i++)
		System.out.print(intCopy + "  ");
		System.out.println ();
	}
}


public class IntegerList implements Comparable
{
    int[] list; //values in the list

    public IntegerList(int size)
    {
	list = new int[size];
    }
    
    public int[] getInts(){
    	return list;
    }
    
    /*Converting int into Integer*/
    public Integer[] makeIntegers(){
    	Integer[] newlist = new Integer[list.length];
    	for (int i = 0;i < list.length; i++)
    		newlist[i] = new Integer(list[i]);
    	return newlist;
    }

    public void randomize()
    {
	for (int i=0; i<list.length; i++)
	    list[i] = (int)(Math.random() * 1000000) + 1;
    }

    public String toString()
    {
	String result = "";
	for (int i=0; i<list.length; i++)
	    result += list[i] + "\t";
	result += "\n\n";
	return result;
    }

    public double average(){
	double sum = 0;
	for (int i=0; i<list.length; i++)
	  sum += list[i];
	return sum / list.length;
    }

    public int compareTo(Object obj){
	double myavg = average();
	double otheravg = ((IntegerList)obj).average();
	if (myavg == otheravg) return 0;
	else if (myavg < otheravg) return -1;
	else return 1;
    }
}




In the Numbers.java code I get the error " The local variable intList may not have been initialized" but I thought I initialized it when I wrote "int[] intList"
Is this wrong?

This post has been edited by diagnonsense: 06 October 2012 - 07:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Sorting a list of Integers

#2 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Sorting a list of Integers

Posted 06 October 2012 - 07:38 PM

Arrays are not dynamic. They cannot grow or shrink. You did not set the amount of numbers you are holding for the array, so Java has no clue how many numbers the array is going to hold. So right before the for loop make initialize the array.

intList = new int[size];


Was This Post Helpful? 2
  • +
  • -

#3 diagnonsense  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 11-February 12

Re: Sorting a list of Integers

Posted 06 October 2012 - 07:43 PM

View PostKinaces, on 06 October 2012 - 07:38 PM, said:

Arrays are not dynamic. They cannot grow or shrink. You did not set the amount of numbers you are holding for the array, so Java has no clue how many numbers the array is going to hold. So right before the for loop make initialize the array.

intList = new int[size];



Ahh makes sense, but now I'm getting the "intList cannot be resolved to a variable" error.
I've been looking at this program too long now, it's all looking like a foreign language at this point.


import java.util.Scanner;
public class Numbers
{
	//---------------------------------------------
	//  Reads in an array of integers, sorts them,
	//  then prints them in sorted order.
	//---------------------------------------------
	public static void main (String[] args)
	{
		int size;
		
		Scanner scan = new Scanner(System.in);
		System.out.print ("\nHow many integers do you want to sort? ");
		size = scan.nextInt();
		
		System.out.println ("\nEnter the numbers...");
		intList = new int[size];
		for (int i = 0; i < size; i++)
			intList[i] = scan.nextInt();
		
		IntegerList intList2 = new IntegerList(size);
		System.out.println(intList2);
		Integer[] intCopy = intList2.makeIntegers();
		Sorting.selectionSort(intCopy);
			
		System.out.println ("\nYour numbers in sorted order...");
		for (int i = 0; i < intCopy.length; i++)
			System.out.print(intCopy + "\t");
		System.out.println ();
	}
}

Was This Post Helpful? 0
  • +
  • -

#4 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Sorting a list of Integers

Posted 06 October 2012 - 07:50 PM

Now you took out

int[] initList; //setting the type and name.



Remember there are two pars of making a variable.
The type and name, and initializing the variable.

You cannot initialize a variable without setting a name and type, and you cannot use a variable without initializing it.

So you need both statements.

This post has been edited by Kinaces: 06 October 2012 - 07:50 PM

Was This Post Helpful? 0
  • +
  • -

#5 diagnonsense  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 11-February 12

Re: Sorting a list of Integers

Posted 06 October 2012 - 08:06 PM

View PostKinaces, on 06 October 2012 - 07:50 PM, said:

Now you took out

int[] initList; //setting the type and name.



Remember there are two pars of making a variable.
The type and name, and initializing the variable.

You cannot initialize a variable without setting a name and type, and you cannot use a variable without initializing it.

So you need both statements.


Ah whoops, that's my bad.
Okay that is resolved.

Now I have to take the integers from intList and copy them to a new array.
I did that and made intCopy, but it will not let me convert intCopy to an array of Integers?

import java.util.Scanner;
public class Numbers
{
	//---------------------------------------------
	//  Reads in an array of integers, sorts them,
	//  then prints them in sorted order.
	//---------------------------------------------
	public static void main (String[] args)
	{
		int size;
		int[] intList;
		
		Scanner scan = new Scanner(System.in);
		System.out.print ("\nHow many integers do you want to sort? ");
		size = scan.nextInt();
		
		System.out.println ("\nEnter the numbers...");
		intList = new int[size];
		for (int i = 0; i < size; i++)
			intList[i] = scan.nextInt();
		
		Integer[] intCopy = new Integer[size];
		for (int j = 0; j < intList.length; j++){
			intCopy[j] = new Integer(intList[j]);
			System.out.println(intCopy[j]);
		}

		IntegerList intCopy2 = intCopy.makeIntegers(); // IT WANTS ME TO MAKE THIS intCopy.Length
		Sorting.selectionSort(intCopy2); // THIS SAYS THAT THERE IS NO COMPARABLE FOR THE VARIABLE INT, I KNOW THIS HENSE WHY I PUT THE LINE BEFORE BUT IT WON'T WORK?
	
		/*System.out.println ("\nYour numbers in sorted order...");
		for (int i = 0; i < intCopy.length; i++)
			System.out.print(intCopy + "\t");
		System.out.println ();*/
	}
}


Was This Post Helpful? 0
  • +
  • -

#6 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Sorting a list of Integers

Posted 06 October 2012 - 08:19 PM

I do not understand why you are trying to make the array into a IntegerList.

Why not add add everything in your array into a Collection (i.e. ArrayList)
then do

Collection.sort((list name))
Was This Post Helpful? 0
  • +
  • -

#7 diagnonsense  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 11-February 12

Re: Sorting a list of Integers

Posted 06 October 2012 - 08:28 PM

View PostKinaces, on 06 October 2012 - 08:19 PM, said:

I do not understand why you are trying to make the array into a IntegerList.

Why not add add everything in your array into a Collection (i.e. ArrayList)
then do

Collection.sort((list name))


The only reason I wouldn't do that is because we haven't covered that in my course so I didn't want to put things like that in.
Was This Post Helpful? 0
  • +
  • -

#8 diagnonsense  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 11-February 12

Re: Sorting a list of Integers

Posted 06 October 2012 - 08:48 PM

I've narrowed down the error to the first for loop

import java.util.Scanner;
public class Numbers
{
	//---------------------------------------------
	//  Reads in an array of integers, sorts them,
	//  then prints them in sorted order.
	//---------------------------------------------
	public static void main (String[] args)
	{
		int size;
		//int[] intList;
		//int[] intCopy;
		
		Scanner scan = new Scanner(System.in);
		System.out.print ("\nHow many integers do you want to sort? ");
		size = scan.nextInt();
		
		System.out.println ("\nEnter the numbers...");
		IntegerList intList = new IntegerList(size);
		for (int i = 0; i < size; i++)
			intList[i] = scan.nextInt(); // ERROR
		
		Integer[] intCopy = intList.makeIntegers();
		for (int i = 0; i < intCopy.length; i++)
			intCopy[i] = new Integer(intCopy[i]);
	
		Sorting.selectionSort(intCopy);
			
		System.out.println ("\nYour numbers in sorted order...");
		for (int i = 0; i < intCopy.length; i++)
			System.out.print(intCopy[i] + "\t");
		System.out.println ();
	}
}


It outputs the error "The type of the expression must be an array type but it resolved to IntegerList"
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10565
  • View blog
  • Posts: 39,103
  • Joined: 27-December 08

Re: Sorting a list of Integers

Posted 06 October 2012 - 09:07 PM

This is a question about an is-a vs. has-a relationships. IntegerList has-an array, but it isn't an array. Therefore, you cannot use the intList[i] array syntax with IntegerList. Looking at IntegerList, you cannot add elements. Instead, you have to populate an array to pass to it.

Also, Comparable is a generic interface, so you should implement Comparable<IntegerList> to adhere to Java standards.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1