File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sort number

 
Wiil Bari
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question regarding a sorting number, I can't figured out to make prompt users for the number and search.
here is my code:
import java.awt.*;
import java.io.*;
import java.lang.*;
import java.util.*;
import java.text.*;
public class Sor {
public static void sort(float[] nums) {

float finput ;
String inputline = " 67.000";
finput = Float.valueOf(inputline).floatValue();
NumberFormat nf = NumberFormat.getNumberInstance();
String numberout;
for(int i = 0; i < nums.length; i++) {
numberout = nf.format(nums[i]);
System.out.println(numberout);
}
for(int i = 0; i < nums.length; i++) {
int min = i;
for(int j = i; j < nums.length; j++) {
if (nums[j] < nums[min]) min = j;
}
float tmp;
tmp = nums[i];
nums[i] = nums[min];
nums[min] = tmp;
}
for(int i = 0; i < nums.length; i++) {
numberout = nf.format(nums[i]);
System.out.println(numberout);
}

}
public static void search( float[] nums) {

float x = 1;
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter float number");
try{
String line = in.readLine();
x = Float.valueOf(line).floatValue();
}
catch(Exception e){}
NumberFormat nf = NumberFormat.getNumberInstance();
String numstring;
for(int i = 0, j = 1; j != 1; i++) {
if (x > nums[i] ) {
if( i == 0 ){
numstring = nf.format(nums[i]);
System.out.println(numstring);
}
else {
numstring = nf.format(nums[i]);
System.out.println( numstring ) ;
numstring = nf.format(nums[i - 1]);
System.out.println( numstring ) ;

}
j =0;
}
if ( i == nums.length ) {
numstring = nf.format(nums[i]);
System.out.println( numstring ) ;
j = 0;
}

//String line;

}

}

public static void main(String[] args) {

float[] nums = new float[10];

for(int i = 0; i < nums.length; i++)
nums[i] = (float)Math.random() * 100;
sort(nums);

search(nums);
//for(int i = 0; i < nums.length; i++)
//System.out.println(nums[i]);

}
}
I believe my search and prompt part is not working and I need your help.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using the "code" tags would help you get a response.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Wiil Bari:
I have a question regarding a sorting number, I can't figured out to make prompt users for the number and search.
here is my code:


I believe my search and prompt part is not working and I need your help.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so your code does the sort correctly. After adding a line break between printing out the original numbers and the sorted numbers I was able to see that.
So what do you want to do now?
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the formatted repost Marilyn.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that this line might be a problem:
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wiil, are you aware of the java.util.Arrays class?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic