1

This code is working but leaves out the sort result. I am trying to get input from Scanner and then print out the result of making some function to input numbers like sort, min, max and average.

public class Arrayassignment {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter an intiger for array size.");

        int number = keyboard.nextInt();

        int array[] = new int[number];

        System.out.println("Array size " + number + " initiated.\n");

        System.out.println("Now enter the array intigers.");

        for (int index = 0; index < number; index++)

        {
            array[index] = keyboard.nextInt();
        }
        keyboard.close();

        System.out.println("Sorting ");

        int[] sortedArray = sort(array);
        // System.out.println(Arrays.toString(sortedArray));
        // printing the sorted array
        System.out.println("Sorted array not working " + sortedArray);
        System.out.println("The highest number in the array is " + max(array));
        System.out.println("The smallest number in the array is " + min(array));
        System.out.println("The average of the numbers in the array is " + avg(array));
    }

    public static int[] sort(int[] arg) {

        int arrange;
        for (int i = 0; i < arg.length - 1; i++)
            for (int j = i + 1; j < arg.length; j++) {

                if (arg[i] > arg[j]) {
                    arrange = arg[i];
                    arg[i] = arg[j];
                    arg[j] = arrange;

                }
            }
        return arg;
    }

    public static int max(int[] arg) {
        if (arg.length == 0) {
            System.out.println(" empty arguement list ");
            return 0;
        }
        int largest = arg[0];
        for (int i = 1; i < arg.length; i++) {
            if (arg[i] > largest)
                largest = arg[i];
        }
        return largest;
    }

    public static int min(int[] arg) {
        if (arg.length == 0) {
            System.out.println(" empty arguement list ");
            return 0;
        }
        int smallest = arg[0];
        for (int i = 1; i < arg.length; i++) {
            if (arg[i] < smallest)
                smallest = arg[i];
        }
        return smallest;
    }

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

    }
}
Ebraheem Alrabeea
  • 2,130
  • 3
  • 23
  • 42
sjames14
  • 33
  • 1
  • 4
  • 1
    **Please** ***stop*** asking [1) the](http://stackoverflow.com/questions/36561946/java-scanner-output-array-values-and-output-after-sort-methed) [2) same](http://stackoverflow.com/questions/36559195/need-a-sort-method-in-java-with-user-input-using-the-scanner-class) [3) question](http://stackoverflow.com/questions/36543057/need-java-array-help-using-scanner-class-to-output-an-average-and-sort-method). – Elliott Frisch Apr 12 '16 at 03:37
  • They all give different answers and they don't result in a clean compile. I would at least like a clean build but moreover, an example similar that runs. – sjames14 Apr 12 '16 at 03:47
  • I have added all input in comments if you want to review them. – sjames14 Apr 12 '16 at 03:48
  • Then edit the [**original** question](http://stackoverflow.com/questions/36543057/need-java-array-help-using-scanner-class-to-output-an-average-and-sort-method) and include all relevant details (including how the existing answers are lacking) (and **fix** your formatting). – Elliott Frisch Apr 12 '16 at 03:48
  • Now enter the array intigers. 1 2 3 4 Sorting Sorted array not working [I@33909752 The highest number in the array is 4 The smallest number in the array is 1 The average of the numbers in the array is 2.5 – sjames14 Apr 12 '16 at 03:50
  • 1
    Learn how to google. http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array – Elliott Frisch Apr 12 '16 at 03:50
  • When I am done with java 2 and move past 3 I will be able to learn all sorts of sorts. I just want a good understanding since I have spent the last 12 hours on trying to organize this to run. – sjames14 Apr 12 '16 at 03:53
  • If you can make it run properly then I will follow your resources. I am in the middle of http://www.sampleprogramz.com/java/sort.php, https://en.wikipedia.org/wiki/Selection_sort, http://visualgo.net/ and http://www.webassign.net/web/Student/Assignment-Responses/view_key?dep=12618986 – sjames14 Apr 12 '16 at 03:55
  • What is wrong with the formatting? – sjames14 Apr 12 '16 at 04:01
  • It is already running properly. I've given you links to detailed explanations, but arrays do not implement `toString` (so you get `Object.toString`). Use `+ Arrays.toString(sortedArray)` instead of `+ sortedArray`. As for your formatting, the indentation makes my eyes hurt. – Elliott Frisch Apr 12 '16 at 04:02
  • I see how I can make things better along paths in which to read. I have not been taught the indentation clarity as I have never had to review other programs yet. I am just looking for simple answers to complicated problems. I have used toString once before and that was in a different classname to test. I am amature in programming and I have spent too much time on math. – sjames14 Apr 12 '16 at 04:07
  • Those that you said are in my comments that don't run. – sjames14 Apr 12 '16 at 04:14
  • When you say `"Sorted array not working " + sortedArray`, the **compiler** turns that into `new StringBuilder("Sorted array not working ").append(String.valueOf(sortedArray))` ***and*** `String.valueOf(sortedArray)` invokes `sortedArray.toString()`; and since `sortedArray` is an `int[]` that invokes [`Object.toString()`](http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) which returns *`getClass().getName() + '@' + Integer.toHexString(hashCode())`*. – Elliott Frisch Apr 12 '16 at 04:15
  • So I am calling a toString unwillingly? – sjames14 Apr 12 '16 at 04:17
  • **YES**. Call [`Arrays.toString(int[])`](http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#toString-int:A-) instead; and **read** [What's the simplest way to print a Java array?](http://stackoverflow.com/q/409784/2970947). – Elliott Frisch Apr 12 '16 at 04:18
  • I saw my professor talking about getting that hexstring in toString. I am not trying to implement that at all. Is that the only way to call the sort method? – sjames14 Apr 12 '16 at 04:19
  • The hexstring you are getting is because of the way you are **printing** the array. – Elliott Frisch Apr 12 '16 at 04:20
  • I have had it out of comments and it doesn't allow it. – sjames14 Apr 12 '16 at 04:20
  • `import java.util.Arrays;` – Elliott Frisch Apr 12 '16 at 04:21
  • Am I mislead from some syntax I can't see? – sjames14 Apr 12 '16 at 04:22
  • The sort ran clean. Thanks. I have a few minor issues with the program. – sjames14 Apr 12 '16 at 04:27
  • Can you e-mail me or text me at sjames14@cougarmail.collin.edu or at 8177938028. I would appreciate your help for a decent wage. I am ending my second semester in programming (java) online and these few posts are all I have had for help since the start. I would like your 1-on-1 help with compensation. – sjames14 Apr 12 '16 at 04:50

0 Answers0