# Two Parallel Arrays

• January 30th, 2013, 01:02 PM
jsmnr77
Two Parallel Arrays
I am having a lot of trouble. I have a new teacher and the class is a lot different. Here is the problem:
Write a class named HorseRace. The HorseRace class should have two parallel arrays: one called names for the names of the horses and one called position for the position of the horses. The program should ask the user for the number of horses in the race and the names of the horses. The names of the horses should be stored in the names array. All the horses should be the race at position zero so the position array should be initialized to 0. After all the names have been entered, the race begins.
The race is run by adding a random integer between 1 and 5 to each horse’s position. The race ends when all of the horses cross the finish line (reach or exceed position 100). You should model the race using a loop and each horse should be processed using a loop as well.
The program should output:
1. The winner of the race should be output, example: “Secretariat wins!”
2. The name of each of the non-winning horses should be output when it crosses the finish line, example: “Man o’ War crosses the finish line!”
Here is what I have but I can't get past the errors:

import java.util.Scanner ;
import java.util.Math;

class HorseRace {
public static void main (String args []){
int i=0,j,k,t,flag=0,k1=0;
Scanner input = new Scanner(System.in);
System.out.println("Enter number of Horses : ");
int number = input.nextInt();
int position[] = new int[number];
int match[] = new int[number];
String name[] = new String[number] ;
for(i=0;i<number;i++){
System.out.println("Enter name of Horse : ");
name[i]=input.toString();
position[i]=0;
}
for(i=0;i<number;i++){
}
for(j=0;j<number;i++){
}
for(t=0;t<=k1;t++){
}
if (j !=match[t]){
number=(Math.random() * 4)+1;
if(number>=100){
}
if(flag==0){
System.out.println("Wins" +name[i]);
flag=1;
match[k1]=i; k1++;
}
else{
System.out.println(name[i]+"’ War crosses the finish line ");
match[k1]=i; k1++;}
}
}
}
• January 30th, 2013, 01:10 PM
pbrockway2
Re: Two Parallel Arrays
It's hard to read the code in that state. Edit your post, or repost it with using "code" tags. Put [code] at the start of the code and [/code] at the end. Be careful to indent your code properly.

I'm guessing the "errors" you are talking about are compiler messages. It would help of you copied and posted the messages and indicated which lines of your code each referred to. Or maybe they are runtime exceptions? In either case post them.
• January 30th, 2013, 03:59 PM
jsmnr77
Re: Two Parallel Arrays
I can not generate the random number between 1-5 or have the horse win at 100 or greater

Code :

``` for(i=0;i<number;i++){ for (flag=0;flag<number;flag++){ for(t=0;t<=k;k++){ if (flag!=match[t]){ Random rand=new Random(); name[i]=rand.nextInt(5)+1;}```
error
if (name[i]>=100){
Code :

``` error System.out.println(name[i]+ "Wins!");end```
• January 30th, 2013, 04:26 PM
Norm
Re: Two Parallel Arrays
I added the / in the ending code tags but it looks like they were in the wrong places. The posted code needs some fixing to be properly formatted.
• January 30th, 2013, 06:00 PM
dave_alan
Re: Two Parallel Arrays
hi
1. your import is not correct try util.Random
2. You need to do one more thing with your random numbers. Do you know about casting or what it is.

You will get it to compile after that but there are a few logical problems too.
And as norm says format the code. It will be a huge help when your trying to debug.
• January 31st, 2013, 08:41 AM
jsmnr77
Re: Two Parallel Arrays
Quote:

Originally Posted by dave_alan
hi