5 Replies - 5486 Views - Last Post: 01 March 2009 - 05:17 PM Rate Topic: -----

#1 villalandron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 24-September 08

Runners program problems

Post icon  Posted 01 March 2009 - 10:01 AM

I am having big issues trying to create this program. The program should ask for the names of three runners and the time, in minutes, it took each of them to finish a race. The program should display the names of the runners in the order that they finished. I keep on getting error messages. Here is the code:

import java.util.Scanner;

/*
PROGRAM: RunningtheRace.java
Written by Julio C. Villa
Write a program that asks for the names of three runners and the time, in minutes, it took each of them
to finish a race. The program should display the names of the runners in the order that they finished.
*/

public class RunningtheRace
{
public static void main(String[ ] args)
{

Scanner keyboard = new Scanner(System.in);


System.out.print("What is the name of the first runner? ");
int runner1;
runner1 = keyboard.nextInt();

System.out.print("What is this runner's score in minutes? ");
int score1;
score1 = keyboard.nextInt();

System.out.print("What is the name of the second runner? ");
int runner2;
runner2 = keyboard.nextInt();

System.out.print("What is this runner's score in minutes? ");
int score2;
score2 = keyboard.nextInt();

System.out.print("What is the name of the third runner? ");
int runner3;
runner3 = keyboard.nextInt();

System.out.print("What is this runner's score in minutes? ");
int score3;
score3 = keyboard.nextInt();

if (score1 < score2 && score1 < score3)
{
if (score2 < score3)
{
System.out.println("The first place runner is " + runner1 + "\n The second place runner is "
+ runner2 + "\n The third place runner is " + runner3 + ". ");
}
else
{
System.out.println("The first place runner is " + runner1 + "\n The second place runner is "
+ runner3 + "\n The third place runner is " + runner2 + ". ");
}
}

else if (score2 < score1 && score2 < score3)
{
if (score1 < score3)
{
System.out.println("The first place runner is " + runner2 + "\n The second place runner is "
+ runner1 + "\n The third place runner is " + runner3 + ". ");
}
else
{
System.out.println("The first place runner is " + runner2 + "\n The second place runner is "
+ runner3 + "\n The third place runner is " + runner1 + ". ");
}
}

else if (score3 < score1 && score3 < score2)
{
if (score1 < score2)
{
System.out.println("The first place runner is " + runner3 + "\n The second place runner is "
+ runner1 + "\n The third place runner is " + runner2 + ".");
}
else
{
System.out.println("The first place runner is " + runner3 + "\n The second place runner is "
+ runner2 + "\n The third place runner is " + runner1 + ".");
}
}
}
}

I thought I did everything correct. Any help in fixing these errors or teaching me a better way to do this code will be appreciated. Thank you!

Jay

Is This A Good Question/Topic? 0
  • +

Replies To: Runners program problems

#2 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Runners program problems

Posted 01 March 2009 - 10:06 AM

Please post your code within code tags :code: so that it is better readable thanks :)
Was This Post Helpful? 0
  • +
  • -

#3 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Runners program problems

Posted 01 March 2009 - 10:11 AM

you were read the runners names as ints but they should be Strings, e.g.
System.out.print("What is the name of the first runner? ");
String runner1;
runner1 = keyboard.nextLine();

System.out.print("What is this runner's score in minutes? ");
int score1;
score1 = keyboard.nextInt();



Was This Post Helpful? 0
  • +
  • -

#4 villalandron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 24-September 08

Re: Runners program problems

Posted 01 March 2009 - 10:57 AM

View Posthorace, on 1 Mar, 2009 - 09:11 AM, said:

you were read the runners names as ints but they should be Strings, e.g.
System.out.print("What is the name of the first runner? ");
String runner1;
runner1 = keyboard.nextLine();

System.out.print("What is this runner's score in minutes? ");
int score1;
score1 = keyboard.nextInt();




Something that small like changing the names to strings helped me greatly. The problem that I have now is that for some reason, from my second string and on, it asks for the second string and the second int at the same time. Here is my updated version.

import java.util.Scanner;

/*
PROGRAM: RunningtheRace.java
Written by Julio C. Villa
Write a program that asks for the names of three runners and the time, in minutes, it took each of them
to finish a race. The program should display the names of the runners in the order that they finished.
*/

public class RunningtheRace
{
public static void main(String[ ] args)
{

Scanner keyboard = new Scanner(System.in);


System.out.print("What is the name of the first runner? ");
String runner1;
runner1 = keyboard.nextLine();

System.out.print("What is this runner's score in minutes? ");
int score1;
score1 = keyboard.nextInt();

System.out.print("What is the name of the second runner? ");
String runner2;
runner2 = keyboard.nextLine();

System.out.print("What is this runner's score in minutes? ");
int score2;
score2 = keyboard.nextInt();

System.out.print("What is the name of the third runner? ");
String runner3;
runner3 = keyboard.nextLine();

System.out.print("What is this runner's score in minutes? ");
int score3;
score3 = keyboard.nextInt();

if (score1 < score2 && score1 < score3)
{
if (score2 < score3)
{
System.out.println("The first place runner is " + runner1 + "\n The second place runner is "
+ runner2 + "\n The third place runner is " + runner3 + ". ");
}
else
{
System.out.println("The first place runner is " + runner1 + "\n The second place runner is "
+ runner3 + "\n The third place runner is " + runner2 + ". ");
}
}

else if (score2 < score1 && score2 < score3)
{
if (score1 < score3)
{
System.out.println("The first place runner is " + runner2 + "\n The second place runner is "
+ runner1 + "\n The third place runner is " + runner3 + ". ");
}
else
{
System.out.println("The first place runner is " + runner2 + "\n The second place runner is "
+ runner3 + "\n The third place runner is " + runner1 + ". ");
}
}

else if (score3 < score1 && score3 < score2)
{
if (score1 < score2)
{
System.out.println("The first place runner is " + runner3 + "\n The second place runner is "
+ runner1 + "\n The third place runner is " + runner2 + ".");
}
else
{
System.out.println("The first place runner is " + runner3 + "\n The second place runner is "
+ runner2 + "\n The third place runner is " + runner1 + ".");
}
}
}
}
Thank you!
Was This Post Helpful? 0
  • +
  • -

#5 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Runners program problems

Posted 01 March 2009 - 11:17 AM

when you enter the first runners score the \n is left in the input stream and is read by keyboard.nextLine(); as the second runners name. You can use a dummy keyboard.nextLine(); call to read the \n from the stream e.g.
System.out.print("What is the name of the first runner? ");
String runner1;
runner1 = keyboard.nextLine();

System.out.print("What is this runner's score in minutes? ");
int score1;
score1 = keyboard.nextInt();

keyboard.nextLine();	// read the \n character from stream
System.out.print("What is the name of the second runner? ");
String runner2;
runner2 = keyboard.nextLine();

System.out.print("What is this runner's score in minutes? ");
int score2;
score2 = keyboard.nextInt();


This post has been edited by horace: 01 March 2009 - 11:18 AM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Runners program problems

Posted 01 March 2009 - 05:17 PM

Yooooo hooooooo ......

:code:

how many times will we have to repeat it ?

Go back and Edit your posts :angry:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1