Please help with this particular problem. I can\'t figure out the best way to do
ID: 3550482 • Letter: P
Question
Please help with this particular problem. I can't figure out the best way to do it. Here's what I have to do:
Write a program that asks for the names of three horses and the time, in seconds, it took each of them to finish a race. The program should display the names of the horses in the order they finished.
Here's what I have so far. What am I doing wrong?
import java.util.Scanner;
import javax.swing.JOptionPane;
public class ErnestTeeHorseRace {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String inputString; //allows for string input
String horseOne; //stores the first horse
String horseTwo; //stores the second horse
String horseThree; //stores the third horse
double horseOneTime; //stores the first time entry
double horseTwoTime; //stores the second time entry
double horseThreeTime; //stores the third time entry
inputString=JOptionPane.showInputDialog("Please enter " +
"the first horse's name: ");
horseOne=keyboard.nextLine();
inputString=JOptionPane.showInputDialog("Please enter " +
"the first horse's time: ");
horseOneTime=keyboard.nextDouble();
inputString=JOptionPane.showInputDialog("Please enter " +
"the second horse's name: ");
horseTwo=keyboard.nextLine();
inputString=JOptionPane.showInputDialog("Please enter " +
"the second horse's time: ");
horseTwoTime=keyboard.nextDouble();
inputString=JOptionPane.showInputDialog("Please enter " +
"the third horse's name: ");
horseThree=keyboard.nextLine();
inputString=JOptionPane.showInputDialog("Please enter " +
"the third horse's time: ");
horseThreeTime=keyboard.nextDouble();
if ((horseOneTime > horseTwoTime) && (horseOneTime > horseThreeTime) && //order 1,2,3
(horseTwoTime > horseThreeTime))
{System.out.println("The order is as follows: ");
System.out.println("FIRST PLACE: " + horseOne + " TIME: "+ horseOneTime + ". ");
System.out.println("SECOND PLACE: " + horseTwo + " TIME: "+ horseTwoTime + ". ");
System.out.println("THIRD PLACE: " + horseThree + " TIME: "+ horseThreeTime + ". ");}
else if ((horseOneTime > horseTwoTime) && (horseOneTime > horseThreeTime) && //order 1.3.2
(horseThreeTime > horseTwoTime))
{System.out.println("The order is as follows: ");
System.out.println("FIRST PLACE: " + horseOne + " TIME: "+ horseOneTime + ". ");
System.out.println("SECOND PLACE: " + horseThree + " TIME: "+ horseThreeTime + ". ");
System.out.println("THIRD PLACE: " + horseTwo + " TIME: "+ horseTwoTime + ". ");}
else if ((horseTwoTime > horseOneTime) && (horseTwoTime > horseThreeTime) && //order 2,1,3
(horseOneTime > horseThreeTime))
{System.out.println("The order is as follows: ");
System.out.println("FIRST PLACE: " + horseTwo + " TIME: "+ horseTwoTime + ". ");
System.out.println("SECOND PLACE: " + horseOne + " TIME: "+ horseOneTime + ". ");
System.out.println("THIRD PLACE: " + horseThree + " TIME: "+ horseThreeTime + ". ");}
else if ((horseTwoTime > horseOneTime) && (horseTwoTime > horseThreeTime) && //order 2,3,1
(horseThreeTime > horseOneTime))
{System.out.println("The order is as follows: ");
System.out.println("FIRST PLACE: " + horseTwo + " TIME: "+ horseTwoTime + ". ");
System.out.println("SECOND PLACE: " + horseThree + " TIME: "+ horseThreeTime + ". ");
System.out.println("THIRD PLACE: " + horseOne + " TIME: "+ horseOneTime + ". ");}
else if ((horseThreeTime > horseOneTime) && (horseThreeTime > horseTwoTime) && //order 3,2,1
(horseTwoTime > horseOneTime))
{System.out.println("The order is as follows: ");
System.out.println("FIRST PLACE: " + horseThree + " TIME: "+ horseThreeTime + ". ");
System.out.println("SECOND PLACE: " + horseTwo + " TIME: "+ horseTwoTime + ". ");
System.out.println("THIRD PLACE: " + horseOne + " TIME: "+ horseOneTime + ". ");}
else if ((horseThreeTime > horseOneTime) && (horseThreeTime > horseTwoTime) && //order 3,1,2
(horseOneTime > horseTwoTime))
{System.out.println("The order is as follows: ");
System.out.println("FIRST PLACE: " + horseThree + " TIME: "+ horseThreeTime + ". ");
System.out.println("SECOND PLACE: " + horseOne + " TIME: "+ horseOneTime + ". ");
System.out.println("THIRD PLACE: " + horseTwo + " TIME: "+ horseTwoTime + ". ");}
System.exit(0); //exits
Where have I gone wrong?
Explanation / Answer
import javax.swing.JOptionPane;
/*
* You did three mistakes.
* 1. There is no need if Scanner with JOptionPane.showInputDialog. It will take a string argument itself
* 2. All the '>' signs must be '<' because when I executed the program, the horse with maximum time was winning
* 3. (Maybe that was your requirement) The output is on the console and not on the GUI frame as it should be consistent
* throughout the program
*
* Try running it, if you have any issues let me know.
*/
public class ErnestTeeHorseRace {
public static void main(String[] args) {
String horseOne; //stores the first horse
String horseTwo; //stores the second horse
String horseThree; //stores the third horse
double horseOneTime; //stores the first time entry
double horseTwoTime; //stores the second time entry
double horseThreeTime; //stores the third time entry
horseOne=JOptionPane.showInputDialog("Please enter " +
"the first horse's name: ");
String time1=JOptionPane.showInputDialog("Please enter " +
"the first horse's time: ");
horseTwo=JOptionPane.showInputDialog("Please enter " +
"the second horse's name: ");
String time2=JOptionPane.showInputDialog("Please enter " +
"the second horse's time: ");
horseThree=JOptionPane.showInputDialog("Please enter " +
"the third horse's name: ");
String time3=JOptionPane.showInputDialog("Please enter " +
"the third horse's time: ");
// the arguments are string by default, so they needs to be converted to double
horseOneTime = Double.parseDouble(time1);
horseTwoTime = Double.parseDouble(time2);
horseThreeTime = Double.parseDouble(time3);
if ((horseOneTime < horseTwoTime) && (horseOneTime < horseThreeTime) && //order 1,2,3
(horseTwoTime < horseThreeTime)){
System.out.println("The order is as follows: ");
System.out.println("FIRST PLACE: " + horseOne + " TIME: "+ horseOneTime + ". ");
System.out.println("SECOND PLACE: " + horseTwo + " TIME: "+ horseTwoTime + ". ");
System.out.println("THIRD PLACE: " + horseThree + " TIME: "+ horseThreeTime + ". ");
JOptionPane.showMessageDialog(null, horseOne+" wins ");
}
else if ((horseOneTime < horseTwoTime) && (horseOneTime < horseThreeTime) && //order 1.3.2
(horseThreeTime < horseTwoTime)){
System.out.println("The order is as follows: ");
System.out.println("FIRST PLACE: " + horseOne + " TIME: "+ horseOneTime + ". ");
System.out.println("SECOND PLACE: " + horseThree + " TIME: "+ horseThreeTime + ". ");
System.out.println("THIRD PLACE: " + horseTwo + " TIME: "+ horseTwoTime + ". ");
JOptionPane.showMessageDialog(null, horseOne+" wins ");
}
else if ((horseTwoTime < horseOneTime) && (horseTwoTime < horseThreeTime) && //order 2,1,3
(horseOneTime < horseThreeTime)){
System.out.println("The order is as follows: ");
System.out.println("FIRST PLACE: " + horseTwo + " TIME: "+ horseTwoTime + ". ");
System.out.println("SECOND PLACE: " + horseOne + " TIME: "+ horseOneTime + ". ");
System.out.println("THIRD PLACE: " + horseThree + " TIME: "+ horseThreeTime + ". ");
JOptionPane.showMessageDialog(null, horseTwo+" wins ");
}
else if ((horseTwoTime < horseOneTime) && (horseTwoTime < horseThreeTime) && //order 2,3,1
(horseThreeTime < horseOneTime)){
System.out.println("The order is as follows: ");
System.out.println("FIRST PLACE: " + horseTwo + " TIME: "+ horseTwoTime + ". ");
System.out.println("SECOND PLACE: " + horseThree + " TIME: "+ horseThreeTime + ". ");
System.out.println("THIRD PLACE: " + horseOne + " TIME: "+ horseOneTime + ". ");
JOptionPane.showMessageDialog(null, horseTwo+" wins ");
}
else if ((horseThreeTime < horseOneTime) && (horseThreeTime < horseTwoTime) && //order 3,2,1
(horseTwoTime < horseOneTime)){
System.out.println("The order is as follows: ");
System.out.println("FIRST PLACE: " + horseThree + " TIME: "+ horseThreeTime + ". ");
System.out.println("SECOND PLACE: " + horseTwo + " TIME: "+ horseTwoTime + ". ");
System.out.println("THIRD PLACE: " + horseOne + " TIME: "+ horseOneTime + ". ");
JOptionPane.showMessageDialog(null, horseThree+" wins ");
}
else if ((horseThreeTime < horseOneTime) && (horseThreeTime < horseTwoTime) && //order 3,1,2
(horseOneTime < horseTwoTime)){
System.out.println("The order is as follows: ");
System.out.println("FIRST PLACE: " + horseThree + " TIME: "+ horseThreeTime + ". ");
System.out.println("SECOND PLACE: " + horseOne + " TIME: "+ horseOneTime + ". ");
System.out.println("THIRD PLACE: " + horseTwo + " TIME: "+ horseTwoTime + ". ");
JOptionPane.showMessageDialog(null, horseThree+" wins ");
}
System.exit(0); //exits
}
}