Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Two Parallel Arrays

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy 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++;}
    }
    }
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Two Parallel Arrays

    I can not generate the random number between 1-5 or have the horse win at 100 or greater

                  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){
     error
    						System.out.println(name[i]+ "Wins!");end
    Last edited by Norm; January 30th, 2013 at 05:24 PM. Reason: Added / in code tags

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Location
    Ireland
    Posts
    6
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Two Parallel Arrays

    hi
    A problem your having genarating your random numbers is
    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.

  6. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Two Parallel Arrays

    Quote Originally Posted by dave_alan View Post
    hi
    A problem your having genarating your random numbers is
    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.
    No I haven't done casting. The teacher said that it shouldn't be necessary to use casting. I know I have to use two parallels and there should be a method(s). I am just having a lot of trouble. New teacher and a different way of teaching. Plus my computer is in the shop getting fixed so I am not able to have a computer readily available. Thanks

Similar Threads

  1. Parallel vectors synchronize
    By speaker in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 18th, 2012, 03:27 PM
  2. Hello! parallel vectors nightmare
    By speaker in forum Collections and Generics
    Replies: 2
    Last Post: May 18th, 2012, 02:12 PM
  3. Counting Matches in Parallel Arrays
    By dx8292 in forum Object Oriented Programming
    Replies: 3
    Last Post: February 1st, 2012, 09:45 AM
  4. Help with parallel array
    By kid20304 in forum Collections and Generics
    Replies: 3
    Last Post: December 6th, 2011, 05:41 PM
  5. Help with code dealing with parallel arrays.
    By danielp1213 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 13th, 2011, 07:43 PM

Tags for this Thread