The following solution might be an advanced solution for you...but since you are learning Java, I am sure that sooner or later...you will come across Classes, Arrays, etc.
The following is a class that defines Simmer and should be saved in "
Simmer.java" file
public class Simmer {
private String name; // simmer name
private float time; // simmer age
// default constructor
public Simmer() {
}
public Simmer( final String name, final float time ) {
this.setName( name );
this.setTime( time );
}
public void setName( final String name ) {
if( "".equals( name ) ) {
// throw exception or try and ask user to enter name agin
} else {
this.name = name;
} //-- ends else
} //-- ends setName
public String getName() {
return this.name;
} //-- ends getName
public void setTime( final float time ) {
if( time <= 0 ) {
// throw exception...or ask user to enter time again
} else {
this.time = time;
} //-- ends else
} //-- ends setTime
public float getTime() {
return this.time;
} //-- ends instance method getTime
public String toString() {
return "Name: " + this.name + "\tTime: " + this.time;
} //-- ends toString
} //-- ends class definition
The following is a class that has a main method, which glue everything together. Save this file in
SimmerRunner.java
public class SimmerRunner {
public static void main( String [] args ) {
// get the input from user as you have done in your sample
// and then populate the array as shown below
Simmer [] simmerCollection = new Simmer[3];
simmerCollection[0] = new Simmer( "John Smith", 145.11f );
simmerCollection[1] = new Simmer( "Mark DoLittle", 138.28f );
simmerCollection[2] = new Simmer( "John Summer", 145.45f );
// print all simmer as they have been entered
System.out.println("-------- Simmers Entered Competition -------");
for( int i = 0; i < simmerCollection.length; i++ ) {
System.out.println(simmerCollection[i].toString());
} //-- ends for loop
// now, we have to loop through the array twice using if statement
// to establish who finished first
for( int i = 0; i < simmerCollection.length; i++ ) {
for( int j = 0; j < simmerCollection.length - 1; j++ ) {
// we use if statements to check the finishing times
if( simmerCollection[j].getTime() > simmerCollection[j+1].getTime() ) {
// if someone stored in posisiotn zero in the array finished
// after someone in position 1, we swap the reference in the array
Simmer temp = simmerCollection[j];
simmerCollection[j] = simmerCollection[j+1];
simmerCollection[j+1] = temp;
temp = null;
} //-- ends if
} //-- ends inner for loop
} //-- ends for loop
// print all simmer as they have been entered
System.out.println("\n---- Simmers Ordered by Finishing Time ----");
for( int i = 0; i < simmerCollection.length; i++ ) {
System.out.println(simmerCollection[i].toString());
} //-- ends for loop
} //-- ends class method main
} //-- ends class definition
once you have compiled the files, and executed SimmerRunner, the output should match the following:
-------- Simmers Entered Competition -------
Name: John Smith Time: 145.11
Name: Mark DoLittle Time: 138.28
Name: John Summer Time: 145.45
---- Simmers Ordered by Finishing Time ----
Name: Mark DoLittle Time: 138.28
Name: John Smith Time: 145.11
Name: John Summer Time: 145.45
Greetings
Eric