Hi, I'm a beginner with Java and I need help with an assignment:

Write a program that will help the Toronto Blue Jay's scouts decide which players they should draft next year. For each player the scouts have been watching, a record has been prepared showing the player's name, age, position, and batting average. Design a program that will ask the scout to enter information for 10 players into arrays. The program should then check each of the players and display statistics of only those players who are under 25 years old and have a batting average of .280 or better. Display the players that qualify sorted by their age.


A menu is required that has the following options:
Enter Blue Jay Data
Display possible Draft Choices
Exit Program

I'm having trouble with having the program only display batters under 25 years of age and a batting average above or equal to .280.

public class Bluejays3
{
    static Console c;
    static String name[] = new String [11];
    static double bataverage[] = new double [11];
    static int age[] = new int [11];
    static String position[] = new String [11];
    static int x;

    public static void main (String[] args)
    {
        c = new Console (); //Opens the console.

        int choice;

        do
        {
            c.print ("     Main Menu\n");
            c.print ("     *********\n");
            c.print ("     1. Enter Blue Jay Data");
            c.print ("\n     2. Display Possible Draft Choices");
            c.print ("\n     3. Exit");
            c.print ("\n     Select your choice 1-3: ");
            choice = c.readInt ();

            if (choice == 1)
                Batterdata (); //opens data entry method
            if (choice == 2)
                Searchscouts (); //opens search method
            if (choice == 3)
            {
                c.print ("\nGoodbye!"); //exits program
            }
            else if (choice < 1 || choice > 3)
            {
                c.clear ();
                c.print ("Invalid Number, try again!\n\n"); //user must enter a proper value or this displays
            }
        }
        while (choice != 3);
    }


    public static void Batterdata ()
    {
        c.print ("You will need to enter 10 possible draft choices\n");
        for (x = 1 ; x <= 10 ; x++) //loops for values (0-9) User must enter data for 0-9.
        {

            c.print ("\nPlease enter the name of batter number " + x + ":");
            name [x] = c.readLine ();

            c.print ("Please enter this batters batting average (ex .300): ");
            bataverage [x] = c.readDouble ();

            c.print ("Please enter this batters position: ");
            position [x] = c.readLine ();

            c.print ("Please enter this batters age: ");
            age [x] = c.readInt ();
        }
    }


    public static void Searchscouts ()

    {
        {
            c.clear ();
            int smallest, i, temp;
            boolean found;
            c.print ("Possible Draft Choices\n\n");

            for (x = 1 ; x <= 10 ; x++)
            {
                smallest = x;
                for (i = x ; i <= 10 ; i++)
                {
                    while(age [i] < age [smallest])
                        smallest = i;
                }
                temp = age [x];
                age [x] = age [smallest];
                age [smallest] = temp;
            }            
                          
           for (x = 1 ; x <= 10 ; x++)//This is the problem,what can i do to change this so that is only displays batters younger than 25 and with an average of .280 or more?
            {
                c.println ("Name " + name[x] + " Age: " + age [x] + " Position: " + position[x] + " Batting Average: " + bataverage[x]);
            }
        }
    }
}

The problem is in the Searchscouts() method, any help is greatly appreciated, and please remember im a beginner so if you could explain your process I would be very thankful! :)

Recommended Answers

All 11 Replies

you need an if statement. you need to check if a record held on the array you stored information corresponds to a player that is younger than 25 AND that has a batting average greater or equal to .280. is this of any help?

you need an if statement. you need to check if a record held on the array you stored information corresponds to a player that is younger than 25 AND that has a batting average greater or equal to .280. is this of any help?

I've tried an if statement

for (x = 1 ; x <= 10 ; x++)
            if (age[x] < 25 && bataverage[x] >= .280)
            {
                c.println ("Name " + name[x] + " Age: " + age[x] + " Position: " + position[x] + " Batting Average: " + bataverage[x]);
            }

a few times before, it works partially. It displays the batters with better than .280 average, but then it lists both their ages at zero. I placed this after the "for" statement. Is this a problem with the location of the "if" statement or do I need a different variable in the statement?

no, the location of if statement is correct, it should be inside your for loop.

I'm still having trouble. I'll update the code I made in my first post, I now need to know why it says their age is 0 when I search for data.

public class Bluejays3
{
    static Console c;
    static String name[] = new String [11];
    static double bataverage[] = new double [11];
    static int age[] = new int [11];
    static String position[] = new String [11];
    static int x;

    public static void main (String[] args)
    {
        c = new Console (); //Opens the console.

        int choice;

        do
        {
            c.print ("     Main Menu\n");
            c.print ("     *********\n");
            c.print ("     1. Enter Blue Jay Data");
            c.print ("\n     2. Display Possible Draft Choices");
            c.print ("\n     3. Exit");
            c.print ("\n     Select your choice 1-3: ");
            choice = c.readInt ();

            if (choice == 1)
                Batterdata (); //opens data entry method
            if (choice == 2)
                Searchscouts (); //opens search method
            if (choice == 3)
            {
                c.print ("\nGoodbye!"); //exits program
            }
            else if (choice < 1 || choice > 3)
            {
                c.clear ();
                c.print ("Invalid Number, try again!\n\n"); //user must enter a proper value or this displays
            }
        }
        while (choice != 3);
    }


    public static void Batterdata ()
    {
        c.print ("You will need to enter 10 possible draft choices\n");
        for (x = 1 ; x <= 10 ; x++) //loops for values (0-9) User must enter data for 0-9.
        {

            c.print ("\nPlease enter the name of batter number " + x + ":");
            name [x] = c.readLine ();

            c.print ("Please enter this batters batting average (ex .300): ");
            bataverage [x] = c.readDouble ();

            c.print ("Please enter this batters position: ");
            position [x] = c.readLine ();

            c.print ("Please enter this batters age: ");
            age [x] = c.readInt ();
        }
    }


    public static void Searchscouts ()

    {
        {
            c.clear ();
            int smallest, i, temp;
            c.print ("Possible Draft Choices\n\n");
            for (x = 1 ; x <= 10 ; x++)
            {
                smallest = x;

                for (i = x ; i <= 10 ; i++)
                {
                    while (age [i] < age [smallest]) smallest = i;                        
                }
                temp = age [x];
                age [x] = age [smallest];
                age [smallest] = temp;
            }

            for (x = 1 ; x <= 10 ; x++)
            {
                if (age [x] <= 24 && bataverage [x] >= .280)

                    {
                        c.println ("Name " + name [x] + " Age: " + age[x] + " Position: " + position [x] + " Batting Average: " + bataverage [x]);

switch line 87 with line 88. the if statement should be inside the for loop, that means behind the bracket:

for (x = 1 ; x <= 10 ; x++){
if (age[x] <= 24 && bataverage[x] >= .280) //This is where the problem is
{
c.println ("Name: " + name[x]);
c.println ("Age: " + age[x]); //this prints age=0, even though the value stored in the area doesn't=0
c.println ("Position: " + position[x]);
c.println ("Batting Average " + bataverage[x]);
}
}

besides, in lines 80 through 83, you seem to change the values held by elements on array age. i don't know why would you want to do that. i tried compiling your code, but my compiler can't find Console class.

Oh my god. Lines 80-83 were the problem, I have no idea why my lesson plan told me to use that. It works like a beauty now.

Thank you sooooo much for your patience with me, I greatly appreciate it. :D

you are welcome. i'm glad i helped!

Sorry I spoke too soon, I'm so frustrated. Now that I removed lines 80-83, suddenly the data isn't sorted by age anymore. It displays data only for batters with age below 25 and average above .280 now, but it doesn't sort them by age. Is there a simpler way to sort them by age and still follow the other rules? :(

public static void Searchscouts ()

    {
        {
            c.clear ();
            int smallest, i, temp;
            c.print ("Possible Draft Choices\n\n");
            for (x = 1 ; x <= 10 ; x++)
            {
                smallest = x;

                for (i = x ; i <= 10 ; i++)
                {
                    while (age [i] < age [smallest])
                        smallest = i;
                }
            }

            for (x = 1 ; x <= 10 ; x++)
            {
                if (age [x] <= 24 && bataverage [x] >= .280)
                    {
                        c.println ("Name " + name [x] + " Age: " + age [x] + " Position: " + position [x] + " Batting Average: " + bataverage [x]);
                    }
            }
        }
    }

go ahead do the sorting. just google for 'bubble sorting in java' and you will find a solution. however, inside your sorting, you need to do parallel rearrangement of your other arrays. hope this helps.

Ok, now I'm certain it works. Thanks again for all the help.
I'm doing this course online and my teacher just doesn't do the trick when it comes to helping me understand things.

welcome :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.