The Artima Developer Community
Sponsored Link

Java Answers Forum
sorting question

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
ducaale

Posts: 4
Nickname: guure03
Registered: Sep, 2002

sorting question Posted: Sep 20, 2002 6:37 AM
Reply to this message Reply
Advertisement
write a program that uses sort class to sort an array of 100 floating-point numbers Be sure to use static methods for this program (that is, there is no need to create search objects). You can random numbers using java.util.Random() or java.lang.Math.random() ). You must enter the target float number interactively. Note (as shown below) that the question requires type float not type double. Your program should deal gracefully with any errors or anomalies associated with entering the target float number. The following code is for getting a float from a String (inputline).

I have a problem for searching and prompt users.

here is my work.
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);
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;
nums = nums[min];
nums[min] = tmp;
}
for(int i = 0; i < nums.length; i++) {
numberout = nf.format(nums);
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 ) {
if( i == 0 ){
numstring = nf.format(nums);
System.out.println(numstring);
}
else {
numstring = nf.format(nums);
System.out.println( numstring ) ;
numstring = nf.format(nums[i - 1]);
System.out.println( numstring ) ;

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


}

}

public static void main(String[] args) {

float[] nums = new float[10];

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

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

}
}

Topic: PKCS12 Previous Topic   Next Topic Topic: How can I do next step of entering user name and password?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use