• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sort number

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using the "code" tags would help you get a response.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the formatted repost Marilyn.
 
William Barnes
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that this line might be a problem:
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wiil, are you aware of the java.util.Arrays class?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic