Ascending Integers

Like many I'm new at Java. I need to do a program that asks for five integers and then arranges the integers in ascending order.

All I can think of is this:

int v, w, x, y, z;

System.out.println("Enter an integer");

System.out.println();

v = reader.readInt();

System.out.println("Enter an integer");

System.out.println();

w = reader.readInt();

System.out.println("Enter an integer");

System.out.println();

x = reader.readInt();

System.out.println("Enter an integer");

System.out.println();

y = reader.readInt();

System.out.println("Enter an integer");

System.out.println();

z = reader.readInt();

if(v < w)

System.out.println(v);

/*

if(w < v || w < x || w < y || w < z);

System.out.println(w);

if(x < v || x < w || x < y || x < z);

System.out.println(x);

if(y < v || y < w || y < x || y < z);

Can someone help me set it up:

[1048 byte] By [mike_jonesa] at [2007-11-26 20:42:04]
# 1

Don't put a semicolon at the end of the if line:

if (x <0) ;

y = 1;

That code tests if x is negative. If it is, it does nothing (;)

then in every case it sets y to 1 -- the indentation is a red herring.

To your assignment:: your instructor is a jerk. You need to sort the 5 ints,

and doing that without a data structure like an array is an exercise

in frustration. And sorting is a non-trivial task for a beginner to even consider.

Are you sure this is your assignment?

DrLaszloJamfa at 2007-7-10 2:01:13 > top of Java-index,Java Essentials,Java Programming...
# 2
http://leepoint.net/notes-java/data/arrays/70sorting.htmlUse an array and then the sort method?
jazza_guya at 2007-7-10 2:01:13 > top of Java-index,Java Essentials,Java Programming...
# 3
> Use an array and then the sort method?Do you think that is what the instructor wants? Mmmmaybe.The OP needs to talk to the instructor and find out.Ask: can I use an array and sorting methods in the API.
DrLaszloJamfa at 2007-7-10 2:01:13 > top of Java-index,Java Essentials,Java Programming...
# 4
I havent learned how to use an array yet.
mike_jonesa at 2007-7-10 2:01:13 > top of Java-index,Java Essentials,Java Programming...
# 5

yeah normally assignments (well at my school) will want you to adapt methods learnt in the weeks prior to the assignment.

Although, i know one of the first things i learnt was how to use the API... which was also part of my first assignment so maybe you could.

Should ask the teacher though incase i guess

jazza_guya at 2007-7-10 2:01:13 > top of Java-index,Java Essentials,Java Programming...
# 6
I dont know how to set this up:int[] numbers = {v,w,x,y,z}; Arrays.sort(numbers); System.out.println(Arrays.toString(numbers));This is for arranging those integers in order and then printing them in order.
mike_jonesa at 2007-7-10 2:01:13 > top of Java-index,Java Essentials,Java Programming...
# 7
I get an error when I try this:double[] lengths = {120.0, 0.5, 0.0, 999.0, 77.3};Arrays.sort(lengths);System.out.println(Arrays.toString(lengths));something about string object cannot be applied to double
mike_jonesa at 2007-7-10 2:01:13 > top of Java-index,Java Essentials,Java Programming...
# 8
Try posting your actual code and actual error message becuase that code should be fine.
floundera at 2007-7-10 2:01:13 > top of Java-index,Java Essentials,Java Programming...
# 9
I have this exactly: double[] lengths = {120.0, 0.5, 0.0, 999.0, 77.3};Arrays.sort(lengths);System.out.println(Arrays.toString(lengths));And I get this error:toString() in java.lang.Object cannot be applied to (double[])
mike_jonesa at 2007-7-10 2:01:13 > top of Java-index,Java Essentials,Java Programming...
# 10
Thats just 3 lines. Post all of your code and when you do, highlight it and click the code button above the message box.
floundera at 2007-7-10 2:01:13 > top of Java-index,Java Essentials,Java Programming...
# 11

import TerminalIO.KeyboardReader;

import java.util.Arrays;

class program3 {

public static void main(String args[]){

KeyboardReader reader = new KeyboardReader();//creating an object called reader to hold info from keyboard

DecimalFormat twoDecimal = new DecimalFormat("0.00");

double v, w, x, y, z;

System.out.println("Enter an integer");

System.out.println();

v = reader.readInt();

System.out.println("Enter an integer");

System.out.println();

w = reader.readInt();

System.out.println("Enter an integer");

System.out.println();

x = reader.readInt();

System.out.println("Enter an integer");

System.out.println();

y = reader.readInt();

System.out.println("Enter an integer");

System.out.println();

z = reader.readInt();

double[] lengths = {v, w, x, y, z};

Arrays.sort(lengths);

System.out.println(Arrays.toString(lengths));

}

}

mike_jonesa at 2007-7-10 2:01:13 > top of Java-index,Java Essentials,Java Programming...
# 12

Well I can see nothing wrong with the code. The only thing I can think of is that there is something hinky with the KeyboardReader class> I assume this is a custom class provided to you by the teacher. Try replacing it with the Scanner class and your code will work.

P.S. As I requested, to make your code readable you must wrap it in code tags. eg:

for(int index = 0; index < MAX; index++) {

System.out.println(index);

}

floundera at 2007-7-10 2:01:13 > top of Java-index,Java Essentials,Java Programming...
# 13
Note: toString method takes no arguement.To call it you use lengths.toString()
Icycoola at 2007-7-10 2:01:13 > top of Java-index,Java Essentials,Java Programming...
# 14
The static toString methods of the Arrays class do take parameters though.
floundera at 2007-7-10 2:01:13 > top of Java-index,Java Essentials,Java Programming...
# 15
ok I just checked the API. Seems like toString with arguements only appear in 1.5.0 but not 1.4.2 Think that's the reason why OP got the error.
Icycoola at 2007-7-21 18:01:22 > top of Java-index,Java Essentials,Java Programming...
# 16

Hello friends !

I also tried that code. It is not giving any errors for me, and it is executing perfectly. I donno why it is giving error in the toString() method of Arrays.

But i am sure that toString() method of Arrays class takes parameter.

If you are permit to use Arrays.toString(), it is the etter way. If we use that method to get the the total array becomes sorted. To get the biggest element from that you should add the following statement.

System.out.println("Biggest : "+lengths[lengths.length-1]);

Where lengths is an array of integers, or double, etc.....

In case if you are not permitted to use Arrays.toString(), you should write as you posted.

mahesh.komuravellia at 2007-7-21 18:01:22 > top of Java-index,Java Essentials,Java Programming...
# 17

Probably you were trying to get the numbers and print them in the ascending order.

The code is fine except the below line:

<code>

System.out.println(Arrays.toString(lengths));

</code>

You are trying to convert an object reference to string, which is not double.

Try to see the value:

<code>

System.out.println(lengths);

</code>

lengths is an array.

Now to print the number use the code:

<code>

for(int i=0;i<lengths.length;i++)

System.out.println("i is "+i+"lengths is :"+lengths);

></code>

hope this helps.

Rgds

Message was edited by:

ayusman_dikshit

ayusman_dikshita at 2007-7-21 18:01:22 > top of Java-index,Java Essentials,Java Programming...
# 18

Hello Try this.. code.. actually this is Selection Sort Technique...

class AscendingOrder

{

public static void main(String[] args){

int a[]={4,3,2,1,5};

int min,temp;

for(int i=0;i<a.length-1;i++){

min=i;

for(int j=i+1;j<a.length;j++)

if(a[j]><a[min])

{

min=j;

}

temp=a;

a=a[min];

a[min]=temp;

}//enf of for

for(int i=0;i<a.length;i++)

System.out.println(a);

}//end of main()

};//end of class

Thanks & Regards

Aditya SVS.>

svs.adityaa at 2007-7-21 18:01:22 > top of Java-index,Java Essentials,Java Programming...
# 19
> Hello Try this.. code.. actually this is Selection Sort Technique...If he is using Arrays.sort, why would he back down and code his own sort?
DrLaszloJamfa at 2007-7-21 18:01:22 > top of Java-index,Java Essentials,Java Programming...
# 20

As others have answered, and I will try to clarify, the Arrays.toString(double[]) method was not available until Java 5.0 (1.5). If you are using 1.4.2 (most likely, considering the error you are getting) you should either upgrade to the lastest version (6.0), or you will need a loop to print out the results.

Arrays.sort(lengths);

for(int i = 0; i < lengths.length; i++)

{

System.out.println(lengths[i]);

}

~Tim

SomeoneElsea at 2007-7-21 18:01:22 > top of Java-index,Java Essentials,Java Programming...