Hi! I'm having hard time where to put the system.out.print code in my assignment.
please check my codes, I'm still new and learning in java sorry If I have mistakes :)

here:

import java.util.Scanner;

public class SelectionSort
{

    public static void main(String[] args)
    {

        Scanner input = new Scanner(System.in);

        System.out.println("Enter the size of the array: ");
        int n = input.nextInt();
        int[] x = new int[n];

        System.out.println("Enter "+ n +" numbers: ");
        for(int i=0; i<n; i++)
        {
            x[i] = input.nextInt();
        }

 for(int i=0; i<n; i++)
     {
        for(int j=i+1; j<n; j++)
        {
            if(i < j )
            {
                int temp = j;
                j = i;
                i = temp;
            }
        }




    }

}
}

Not sure what you mean by that... Do you mean you want to display the output? If so, you would need to add another for-loop right at the end. Iterate through that loop and display each element of your x array element...

My question would be, 1)do you know what System.out.println() does? And 2)do you know what for-loop is?

Edited 2 Years Ago by Taywin

sorry I mean that I want to display the output. :)
yes I know System.out.println() and the for loop.

If you want to display the output (the contents of the array at the end), and you know loops and print, then what exactly is your question?

yes, i want to display the output from the contents of the array at the end.
because I dont know exactly where to add the print code to show the output.

The code on lines 21-36 sorts the data (or it will, when you've fixed the bug), so it has to be after that. The main method ends on line 38, so it has to be before that.

Yup, that's what you get when you print an array - it's the toString() method that arrays inherit from Object - the bracket says its an array, the I says its an array of ints, the rest is the array's hash (normally its address in memory).
To print the contents of an array you use a loop to print all the elements one at a a time. (There's also a method in the Arrays class that does that for you, but it's best if you figure it out yourself for now)

Yup, that's what you get when you print an array - it's the toString() method that arrays inherit from Object - the bracket says its an array, the I says its an array of ints, the rest is the array's hash (normally its address in memory).
To print the contents of an array you use a loop to print all the elements one at a a time. (There's also a method in the Arrays class that does that for you, but it's best if you figure it out yourself for now)

Edited 2 Years Ago by JamesCherrill

i cant solve my problem, please check my attachement. 1480c7113a0d863160b921be7e2e7990

and here's my code:

import java.util.Scanner;

public class SelectionSort
{

    public static void main(String[] args)
    {

        Scanner input = new Scanner(System.in);

        System.out.println("Enter the size of the array: ");
        int n = input.nextInt();
        int[] x = new int[n];

        System.out.println("Enter "+ n +" numbers: ");
        for(int i=0; i<n; i++)
        {
            x[i] = input.nextInt();
        }



            int least;
        for(int i=0; i<n-1; i++){

                least = i;

        for(int j=i+1; j<n; j++){

                if(j < least )

                least = j;


                int tmp = least;
                if(i!=least){
                least = i;
                i = tmp;



            }
        }




 }
        for(int w=0;w<n;w++){

        System.out.println(x[w]+"");

        }
}
}

Well, your sorting is wrong. You are comparing the loop index value instead of the value inside your array... For example, least=i; should at least be least=x[i];, so that you get the the value from the array to be compared, not the loop index itself.

can you give me the correct code? I tried to edit but the output is not sorted.

can you give me the correct code?

No.

This is not a "we do your homework" service, and if you submit some of our fixed code as your homework then you will deserve an "F" for cheating.

Taywin told you where the problem is, and how to start fixing it. Read and understand what he said. Correct your code accordingly. If it still doesn't work then put a load of print statements into your code, printing the values of the key variables at each stage so you can see where it's going wrong. If you're still stuck after that post your latest code and a description of what you have tried, and someone will give you some more advice.

I apologize, I didn't mean you give the whole code,just the lopping line. I just confuse which of Taywin is telling me because there are two "least=i;" on my code. but I'll try to correct this.

There are a few places inside your loops where you use i instead of x[i] or j instead of x[j]
In other words, you should be comparing and swapping the elements in the array, not the indexes.

thank you very much! should I post my code?
I have solved my problem and I understand it a bit now. :) I will study this and the algo so I can explain it to my prof.

This question has already been answered. Start a new discussion instead.