473,385 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Using an Array, what is lacking to my code to have an output of ascending numbers?

19
If the user input three numbers, say (1,3,2), how it will become 1,2,3 or in ascending order?? there is something wrong/lacking with my code, help plzzzz!!

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  public class sign {
  3.  public static void main (String args[])throws Exception
  4.  {
  5.  
  6.  String number;
  7.  int numberInteger;
  8.  int[] temp; 
  9.  temp = new int[3];
  10.  int i;
  11.  
  12. for (i=0;i<=2;i++) 
  13.     {
  14.       System.out.print("Input three numbers:");
  15.       InputStreamReader no= new InputStreamReader(System.in);
  16.       BufferedReader num=new BufferedReader(no);
  17.       number=num.readLine();
  18.       numberInteger=Integer.parseInt(number);
  19.       temp[i]=numberInteger;
  20.     }
  21.            for (i=0;i<=2;i++)
  22.            {
  23.             System.out.print("\nAcsending Order:"+temp[i]);
  24.            }
  25.       }
  26. }
Feb 13 '07 #1
3 1570
r035198x
13,262 8TB
If the user input three numbers, say (1,3,2), how it will become 1,2,3 or in ascending order?? there is something wrong/lacking with my code, help plzzzz!!

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. public class sign {
  3. public static void main (String args[])throws Exception
  4. {
  5.  
  6. String number;
  7. int numberInteger;
  8. int[] temp; 
  9. temp = new int[3];
  10. int i;
  11.  
  12. for (i=0;i<=2;i++) 
  13. {
  14. System.out.print("Input three numbers:");
  15.      InputStreamReader no= new InputStreamReader(System.in);
  16. BufferedReader num=new BufferedReader(no);
  17. number=num.readLine();
  18. numberInteger=Integer.parseInt(number);
  19. temp[i]=numberInteger;
  20. }
  21. for (i=0;i<=2;i++)
  22. {
  23.      System.out.print("\nAcsending Order:"+temp[i]);
  24. }
  25. }
  26. }
What is missing is the sort. Either use bubble sort or Arrays.sort to sort the numbers.
Feb 13 '07 #2
hirak1984
316 100+
[font=Verdana][size=2]slightly modified your code:[/size][/font]
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. public class sign {
  3. public static void main (String args[])throws Exception
  4. {
  5. String number;
  6. int numberInteger;
  7. int[] temp; 
  8. temp = new int[3];
  9. int i,j;
  10. System.out.print("Input three numbers:");
  11. for (i=0;i<=2;i++) 
  12.     {
  13.  
  14. InputStreamReader no= new InputStreamReader(System.in);
  15.      BufferedReader num=new BufferedReader(no);
  16.      number=num.readLine();
  17.      numberInteger=Integer.parseInt(number);
  18.      temp[i]=numberInteger;
  19.     }
  20. for (i=1;i<=2;i++) 
  21. {
  22. if(temp[i-1]>temp[i])
  23. {
  24. j=temp[i-1];
  25. temp[i-1]=temp[i];
  26. temp[i]=j;
  27.  
  28. }
  29. }
  30.          for (i=0;i<=2;i++)
  31.          {
  32.          System.out.print("\nAcsending Order:"+temp[i]);
  33.          }
  34.      }
  35. }
  36.  
Feb 13 '07 #3
cess
19
[font=Verdana][size=2]slightly modified your code:[/size][/font]
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. public class sign {
  3. public static void main (String args[])throws Exception
  4. {
  5. String number;
  6. int numberInteger;
  7. int[] temp; 
  8. temp = new int[3];
  9. int i,j;
  10. System.out.print("Input three numbers:");
  11. for (i=0;i<=2;i++) 
  12.     {
  13.  
  14. InputStreamReader no= new InputStreamReader(System.in);
  15.      BufferedReader num=new BufferedReader(no);
  16.      number=num.readLine();
  17.      numberInteger=Integer.parseInt(number);
  18.      temp[i]=numberInteger;
  19.     }
  20. for (i=1;i<=2;i++) 
  21. {
  22. if(temp[i-1]>temp[i])
  23. {
  24. j=temp[i-1];
  25. temp[i-1]=temp[i];
  26. temp[i]=j;
  27.  
  28. }
  29. }
  30.          for (i=0;i<=2;i++)
  31.          {
  32.          System.out.print("\nAcsending Order:"+temp[i]);
  33.          }
  34.      }
  35. }
  36.  
a thousand thanks!!!!!
Feb 13 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: deko | last post by:
I have a text file ("eighty.txt") that looks like this: 83|84|85|86 I can read the file into an array like this: $numbers= file("eighty.txt"); But how do I key the array? I'd like to use...
6
by: hoover_richard | last post by:
I am a newbie to C++ and I need help with a simple program I am trying to write. My program is designed to print all of the odd integers contained in an array and output the sum of the odd...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
1
by: Prasad Karunakaran | last post by:
I am using the C# DirectoryEntry class to retrieve the Properties of an user object in the Active Directory. I need to get the First Name and Last Name as properties. I know it is not supported...
11
by: holla | last post by:
Write the following functions that can be called by a C program: Write a function to populate an array with random integers between 0 and 99. Use the following functions of which the prototypes...
0
by: JamC | last post by:
Hi I have a poker program... My code for Pair, 2 pair etc work, except when I code for a straight hand When I separate the sort function like below I can get it... public void straight() ...
1
by: chiefychf | last post by:
I'm working on a school project and I am having a few issues... The program calls for three arrays a,b,c that have to be sorted, then compared to even or odd and stored in arrays d & e, then merge...
2
by: Villanmac | last post by:
Okay basically I have built the code using the pseudocode given to me but i still have on psedocode to mark off and I dont know how to do what it says. Its says "a flag is just an integer whose...
9
ADezii
by: ADezii | last post by:
One question which pops up frequently here at TheScripts is: 'How do I retrieve data from a Recordset once I've created it?' One very efficient, and not that often used approach, is the GetRows()...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.