0

I'm pretty new to java, and I'm trying to create a simple method that sorts inputted numbers, either ascending or descending. However, there's a problem that I can't put in repeated values. Is there a way to get the key of a certain item of an array??

My code:

import java.io.Console;

public class TestSort {
    public static void main(String args[]) {
        Console c = System.console();
        if (c == null) {
            System.err.println("No console.");
            System.exit(1);
        }

        System.out.println("TESTSORT.java");
        System.out.println("-------------");
        System.out.println("Type in a set of numbers here:");
        String in = c.readLine();
        System.out.println("(A)scending or (D)escending");
        String ad = c.readLine();

        boolean d = false;

        if(ad.equals("a")) d = false;
        else if(ad.equals("d")) d = true;
        else {
            System.out.println("Invalid Input.");
            System.exit(1);
        }

        String[] in2 = in.split(" ");
        int[] x = new int[in2.length];

        int count1 = 0;
        for(String val : in2)
            x[count1++] = Integer.parseInt(val);

        int[] a = new int[x.length];
        int count = 0;

        for(int y : x) {
            for(int z : x) {
                // if index of y equals index of z continue
                if(z < y) count++;
            }
            a[count] = y;
            count = 0;
        }

        if(d) {
            int[] arr3 = new int[a.length];
            int length = a.length;
            for(int b : a) arr3[--length] = b;
            for(int b : arr3) System.out.println(b);
        } else
            for(int b : a)
                System.out.println(b);
    }
}

This program just counts up the number of other numbers smaller than itself, but not including itself. However, it doesn't differentiate itself from other numbers with the same value.

Help would be appreciated.

Thanks.

7
  • 2
    What's your code currently doing? Is it working/not working? What errors/output are you getting? Commented Aug 18, 2013 at 21:16
  • To access a certain item in the array, one must have the index. I don't understand what you're asking. Commented Aug 18, 2013 at 21:16
  • sorry, I meant how to access an array element's key. Commented Aug 18, 2013 at 21:19
  • What do you mean by "it's key"? Like it's index? Commented Aug 18, 2013 at 21:20
  • @Jon: A key is an index. What is your code currently doing? In your code you do key++, but where is that key coming from? You just use it out of the blue. Where do you create it? Commented Aug 18, 2013 at 21:21

3 Answers 3

3

To get the index of a certain value for an array you will have to loop through the array. However if there is multiple entries with the same value this approach wouldn't work (without modification)

 int indexVal = -1;
 int inputValue;  // This is your input vlaue you are trying to find
 for(int i = 0; i < array.length ; i++)
 {
     if (array[i] == inputValue)
     {
         indexVal = i;
         break;
     }
 }

You may also want to look at Array.sort for built in array sorrting

Sign up to request clarification or add additional context in comments.

Comments

1

If you want an index you should not be using for each loops. You will have to use a regular for loop to get at an index in the array.

Comments

0

A SortedSet is perfect for this. As a set, it does not allow duplicate values, and it is sorted automatically for you!

Just add your elements to the set, e.g:

SortedSet<Integer> set = new SortedSet<Integer>();
for(String value : in2.split(" ")){
    set.add(Integer.parseInt(value));
}

To reverse the order of the set do something like this:

SortedSet<Integer> descending = set.descendingSet();

You can iterate through sets just like arrays too:

for(Integer i : set){
    //Do something
}

Good luck!

Comments

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.