0
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?

arshajii
  • 127,459
  • 24
  • 238
  • 287

2 Answers2

1

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.

arshajii
  • 127,459
  • 24
  • 238
  • 287
0

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.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • 2
    Java could very well have been smart enough to know what you mean. Arrays *should* have provided a `toString()`, it was a mistake in the language design that they did not. – arshajii May 14 '14 at 15:11
  • That's just like, your opinion, man. When I say "Java isn't smart enough", I don't mean it as a rebuke of the language, I just use it as a way to demonstrate a lack of functionality that many novices assume is there. – Kevin Workman May 14 '14 at 15:14
  • It's pretty well accepted that this was a mistake (for e.g. see [this](http://stackoverflow.com/questions/7060016/why-does-the-tostring-method-in-java-not-seem-to-work)). I just think your second statement is somewhat misleading. You could have said the same thing about `ArrayList`, but that prints just fine. – arshajii May 14 '14 at 15:16
  • Again, that's just an opinion. The page you posted also contains an argument **for** the way it currently works. I believe that the entire point of the OP's homework was to work all of this out on his own, which is why I didn't provide code. – Kevin Workman May 14 '14 at 15:26
  • thanks for your tip Kevin. i just wondering are you pointing to this loop: public static void display(char[] a) { for (int i = 0; i < a.length; i++) { System.out.print(a[i]); } System.out.println(); } – user3528825 May 14 '14 at 15:27
  • Did that loop do what you want it to? If so, then yes! If not, then post your updated code. – Kevin Workman May 14 '14 at 15:30