0

Very simple. It is the same as you print out on the console. Whatever format you want is fine. In stead of printing out to the screen, you just make it to String. Oh and do not forget to use @Override to let JAVA knows that the method is overriding the default method from Object class (which return junk for you).

@Override
public String toString() {
  String output="";
  // iterate through your ArrayList here
  for(int element : numbers) {
    // in your display, you would use...
    // System.out.print(element+" ");
    // in converting to string, you could use...
    // out += element+" ";
    // So it is your decision to make whatever string to be like.
  }
  return output;
}

Edited by Taywin: n/a

0

Ok that solved that problem but the entire program is not getting the correct answers...is it an error in the main method?

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class RandomIntArrayList
{
   
    public RandomIntArrayList(int size)
    {      

	    numbers = new ArrayList<Integer>();

        Random generator = new Random();
       
        for (int i = 0; i < numbers.size(); i++)
        {
            int n = 100 + generator.nextInt(100);
            n = numbers.get(i);
        }
        for (int i = 0; i < numbers.size(); i++)
        {
        System.out.print(numbers.get(i)+ " ");
        }
    }
    public int sum()
    {
        int total = 0;
        for(int element : numbers)
        {
            total = total + element;
        }
        return total;
    }
   
    public double average()
    {
        double average = (double) sum() / numbers.size();
        return average;
    }
   
    public int max()
    {
        int max = 0;
        for(int element : numbers)
        {
            if(element > max)
            {
                max = element;
            }
        }


        return max;
    }
   
    public int min()
    {
        int min = Integer.MAX_VALUE;
        for(int element : numbers)
        {
            if(element > 0 && element < min)
            {
                min = element;
            }
        }


        return min;
       
    }
   
    public int getSize()
    {
        int size;
        size = numbers.size();
        return size;
    }
    public boolean search(int ix)
    {

        boolean b = false; 
        for (int j=0; j < numbers.size(); j++) 
        { 
            if (numbers.get(j) == ix) 
            { 
                b = true; 
            } 
        } 
        return b; 

    }
   
   
   
    public String toString()
    {
    	String output="";
    for(int element : numbers) 
    {
    	output = output + element++;
    
    }
    return output;
    }
    private ArrayList<Integer> numbers;
}
import java.util.Scanner;
import javax.swing.JOptionPane;

public class RandomIntArrayListDriver
{
	
 public static void main(String args[])
    {
        String input = JOptionPane.showInputDialog("Enter the size of the array: ");
        int size = Integer.parseInt(input);
       
          RandomIntArrayList numArray = new RandomIntArrayList (size);
          
          JOptionPane.showMessageDialog(null, "\n" + "Sum: " + numArray.sum());
          JOptionPane.showMessageDialog(null, "Average: " + numArray.average());
          JOptionPane.showMessageDialog(null, "Max: " + numArray.max());
          JOptionPane.showMessageDialog(null, "Min: " + numArray.min());
          JOptionPane.showMessageDialog(null, "Size: " + numArray.getSize());
          JOptionPane.showMessageDialog(null, "Numbers: " + numArray.toString());
         
    }
}
0

Nope, it is the way you create your random integer... I thought you were using primitive int array... You are using ArrayList which is different.

public RandomIntArrayList(int size) {      
  numbers = new ArrayList<Integer>();  // OK, but you create an empty array
  Random generator = new Random();  // OK

  // problem! You will never get in here because your array is empty!
  // You need the max of "i" with the incoming "size" variable
  for (int i = 0; i < numbers.size(); i++) {  // change it to i<size
    int n = 100 + generator.nextInt(100);  // OK
    n = numbers.get(i);  // Not OK! You need to add the "n" to numbers, not the other way around
    // for ArrayList, you do
    //    numbers.add(new Integer(n));
    // The reason is that your ArrayList is Integer class, not primitive int,
    // so you need to create an Integer object.
  }

  // for your debugging?
  for (int i = 0; i < numbers.size(); i++) {
    System.out.print(numbers.get(i).intValue()+ " ");
  }
}

Because you are using ArrayList of Integer type, you need to convert any place you are calling the value as "int" by using intValue() of the object (i.e. int v=number.get(i).intValue()).

Also line 102, why element++???

I need to go home now... Won't be on line until late at night...

Edited by Taywin: n/a

0

I'm sorry I may not have been clear...in the first program I was using an array then I had to convert the program to arrayList which is the program above. Honestly I have no idea why I used element on line 102...the whole toString with ArrayList confuses me.

Thank you so much for your help today...Unfortunantly I have to put this aside for now and go to a rehearsal dinner for a wedding but hopefully I can implement these final changes when I get home and put this program to rest.

Once again thank you so much for your patience I am new at programming but really want to learn and understand everything.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.