import java.io.*;

public class SortDoubleArray 
{
	public static void main(String[] args) throws IOException
	{
		double[] doubleValue = new double[10];
		
		Input(doubleValue);
		
		ascendingSort(doubleValue, doubleValue.length);
		
		descendingSort(doubleValue, doubleValue.length);
		
	}
	public static void Input(double[] array) throws IOException
	{
		BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
		String[] numbers = {"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"};
		
		for (int n = 0, i = 0; n < numbers.length && i < array.length; ++n, ++i)
		{
			System.out.println("Enter the " + numbers[n] + " number");
			array[i] = dataIn.readLine();
		}
	}
	public static void ascendingSort(double[] array, double length)
	{
		double temp;
		double highSubscript = length - 1;
		for (int a = 0; a < highSubscript; ++a)
		{
			for (int b = 0; b < highSubscript; ++b)
				if (array[b] > array[b + 1])
				{
					temp = array[b];
					array[b] = array[b + 1];
					array[b + 1] = temp;
				}
		}
		System.out.println("\nAscending sort");
		for (int x = 0; x < array.length; ++x)
			System.out.print(array[x] + " ");
		System.out.println();
	}
	public static void descendingSort(double[] array, double length)
	{
		double temp;
		double highSubscript = length - 1;
		for (int a = 0; a < highSubscript; ++a)
		{
			for (int b = 0; b < highSubscript; ++b)
				if (array[b] < array[b + 1])
				{
					temp = array[b];
					array[b] = array[b + 1];
					array[b + 1] = temp;
				}
		}
		System.out.println("\nDecending sort");
		for (int x = 0; x < array.length; ++x)
			System.out.print(array[x] + " ");
		System.out.println();
	}

}

As you can see, in this part

for (int n = 0, i = 0; n < numbers.length && i < array.length; ++n, ++i)
		{
			System.out.println("Enter the " + numbers[n] + " number");
			array[i] = dataIn.readLine();
		}

and this line is what i'm having trouble over

array[i] = dataIn.readLine();

All I'm trying to do is to get user input with numbers, floating numbers to be exact, double numbers, and I don't know how to tell it that I want the user input to convert to double and not make it think I'm typing characters but NUMBERS and output the array sort with what you enter, not what those characters equal like ASCII saying if I input the number 56.09 - it thinks I'm saying 53.0 - which is the character equivalent and i don't want that.
I don't know how else to explain it if it doesn't already make sense. I've tried doing a
Double.parseDouble() thing going and it just gives me errors (using Eclipse).
I know putting dataIn(bufferedreader)
is a String thing, that's why I'm having trouble.
Someone help!!!

Could you try:

array[i] = Double.valueOf(dataIn.readLine());

I'm not sure what the difference between that and parsing is, but this works more often than parsing for me :)

L

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.