2

I have this code here.

public class ArrayChallenge {

private static Scanner kb = new Scanner(System.in);

public static void main(String[] args) {
    System.out.print("Number of integers: ");
    int num = kb.nextInt();

    int [] returnedArray = readIntegers(num);
    System.out.println("Ascending Order: " + printAsc(returnedArray));
}

public static int [] readIntegers(int count){
    int [] values = new int[count];
    for(int i=0; i<count; i++){
        System.out.print("Enter number: ");
        values[i] = kb.nextInt();
    }
    return values;
}

public static int [] printAsc(int [] theArray){
    Arrays.sort(theArray);
    return  theArray;
}

The code above works perfectly fine. I have only one class but several static methods and during calling the method printAsc(returnedArray), the program will return a set of arrays in a format just like using .toString

I took this code to a next level using two classes and making the static methods to instance methods. However when i print the arrays. It prints these values [I@643b1d11

Here is my code below:

Main Class

public class Main {

public static Scanner kb = new Scanner(System.in);

public static void main(String[] args) {
    System.out.print("Enter the number of elements: ");
    int num = kb.nextInt();

    int numArrays []  = new int[num];

    Minimum minimum = new Minimum();
    int [] returnedArrays = minimum.readIntegers(numArrays);
    int returnedMin = minimum.findMin(returnedArrays);

    System.out.println("Minimum Value: " + returnedMin);
    System.out.println("Ascending Arrays: " + minimum.arrayAsc(returnedArrays));
}

}

Minimum Class

public class Minimum {

public int [] readIntegers(int [] numArrays){
    System.out.println("Enter " + numArrays.length + " integers:");
    for(int i=0; i< numArrays.length; i++){
        numArrays[i]=Main.kb.nextInt();
    }
    return numArrays;
}

public int findMin(int [] numArrays){
    int max = Integer.MAX_VALUE;
    for(int i=0;i<numArrays.length;i++){
        if(max>numArrays[i]){
            max = numArrays[i];
        }
    }
    return max;
}

public int [] arrayAsc(int [] numArrays){
    Arrays.sort(numArrays);
    return numArrays;
}

}

How did the result of my arrays became like that? Can someone explain the process to me? Much appreciate. Also, i used the debug method because im using Intellij, It didnt really show anything.

2
  • 5
    The code you wrote that "works perfectly fine" can't possibly work fine. printAsc returns an array. An array cannot be printed directly - you have to use Arrays.toString or a loop. Perhaps you were running an old version of it where printAsc did something else. Commented Mar 23, 2020 at 15:29
  • @RealSkeptic thank you for pointing that out. It was indeed because of the old version. Commented Mar 23, 2020 at 15:45

2 Answers 2

3

The way you are using printAsc, you need to change its definition to return a String.

public static String printAsc(int [] theArray){
    Arrays.sort(theArray);
    return  Arrays.toString(theArray);
}

If you do not want to change the definition of printAsc, you need to call it as follows:

System.out.println("Ascending Order: " + Arrays.toString(printAsc(returnedArray)));
Sign up to request clarification or add additional context in comments.

2 Comments

yeah i did that however i want to use an array int as the return type and i cant seem to figure it out.
i see you've edited it. Thank you for that answer. I didn't know its capable of doing that.
0

You can't just print an Array and expect it to read out all the values. When printing, Java casts the value into a String. By default, when dealing with Objects, Java will simply print the name of the Object's class and its Hash value. This is what you see. You can read more about it here.

If you want to parse the Array into a String that hold all the members of the Array, you might want to take a look at the Arrays.toString method (and how to use it).

1 Comment

yes I can convert it from Arrays.toString. However I want to return a int array in the instance method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.