Question

How many elements do you want in the array?
3
Enter an integer to store in the array
66
enter an integer to store in the array
33
enter an integer to store in the array
99
Here are the sorted values:
[33,66,99]

This should be the result.

This is what I made:

import java.util.Scanner;


public class PracTe3Te {

    /**
     * @param args
     */
    public static void main(String[] args) {
        int [] a;
        int size;
        int value;


        Scanner sc;
        sc = new Scanner(System.in);
        System.out.println("How many elements do you want in the array? ");
        size = sc.nextInt();

        a = new int [size];

        System.out.println("the size of array is " + a.length);



            for (int i = 0; i < a.length; i++) 
            {
                System.out.println("Enter an integer to store in the array");
                value = sc.nextInt();
                a[i] = value;


                 int newNum = value;

                 if (value < a.length - 1) { //otherwise array is already full
                     a[value] = value;
                     newNum = newNum + 1;


                 }

            }
             System.out.print(a);

        }

    }

and get this result:

How many elements do you want in the array? 
3
the size of array is 3
Enter an integer to store in the array
4
Enter an integer to store in the array
3
Enter an integer to store in the array
5
[I@60072ffb

How can I print the array list of entered items not like [I@60072ffb?

Was it helpful?

Solution

Use Arrays.toString() (don't forget to import java.util.Arrays):

System.out.print(Arrays.toString(a));

Arrays don't override toString() (the method that gets called when you try to print an object), so the implementation defaults to that of Object which consists of the class name followed by the hash code.

OTHER TIPS

You're trying to print an array:

System.out.print(a);

But Java isn't smart enough to know what you mean by that. What if it's an array of Objects? What if it's an array of JButtons? What should it print out?

There are functions in the API that print an array in a smarter way, or you can define your own algorithm for printing the array, presumably by looping through and printing individual elements of the array one at a time. I assume this is the point of the homework.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top