0

I'm having trouble with this array I'm working on. In the for loop, I need to somehow calculate an on base percentage. The element at index 0 will store the OBP. How can I retain the information the user inputs to calculate the OBP? Thank you.

     for (int index = 0; index < years.length; index++)
     {
       System.out.print("For Year " + (index +1 ) + "\nEnter number of hits: ");
       years[index] = keyboard.nextInt();

       System.out.print("For Year " + (index +1) + "\nEnter number of walks: ");
       years[index] = keyboard.nextInt();

       System.out.print("For Year" + (index +1) + "\nEnter the number of times player"
                          + "has been hit by a pitch:");
       years[index] = keyboard.nextInt();

       System.out.print("For Year" + (index +1) + "\nEnter the number of at bats:");
       years[index] = keyboard.nextInt();

       System.out.print("For Year" + (index +1) + "\nEnter the number of sacrafice flies" 
                          + "that year: ");
       years[index] = keyboard.nextInt();

     }
0

3 Answers 3

1

I'd suggest a HashMap inside a HashMap for this use case.

Before loop:

HashMap<Integer, HashMap<String, Integer>> years = new HashMap<>();
HashMap<String, Integer> entry = new HashMap<>();

For every input from user's keyboard (example):

entry.put("hits", 5);
years.put(2019, entry);

entry.put("walks", 10);
years.put(2019, entry);

In the end you get a result such as:

{2019={hits=5, walks=10}}

Retrieving results is simple too:

// retrieve map of data for a specific year:
years.get(2019)

result: {hits=5, walks=10}

// retrieve specific data for a specific year:
years.get(2019).get("hits")

result: 5
Sign up to request clarification or add additional context in comments.

1 Comment

Of course, I'd personally create a simple class e.g. User and have separate fields for each data type.
0

You will want to store the values entered by the user in a variable. Instead of having an array of inputs (which is one way of doing it) - you can simply store each value in a separate variable and then do the calculation at the end.

See my sample code below:

public static void main(String args[]) {
    // let's take incoming values from the user for our calculations
    Scanner keyboard = new Scanner(System.in);

    // we'll use this array to store the values entered by the user
    // in position zero of the array we'll store the OBP
    // in positions 1-5 we'll store the inputs as entered by the uesr
    float[] values = new float[6];

    // we'll use this flag to determin if we should ask the user input again or quit the program
    boolean letsDoThisAgain = true;

    while (letsDoThisAgain){
        System.out.println("Enter a Year: ");
        int year = keyboard.nextInt();

        System.out.println("For Year " + year + ", enter number of:");
        System.out.println("Hits: ");
        values[1] = keyboard.nextFloat();

        System.out.println("Walks: ");
        values[2] = keyboard.nextFloat();

        System.out.println("Number of times player has been hit by a pitch: ");
        values[3] = keyboard.nextFloat();

        System.out.println("Number of at bats: " );
        values[4] = keyboard.nextFloat();

        System.out.println("Number of sacrifice flies: ");
        values[5] = keyboard.nextFloat();

        // calculate the OBP
        values[0] = (values[1] + values[2] + values[3] - values[5] ) / values[4]; // or however you calculate it

        System.out.println("OBP: " + values[0]);

        System.out.println("------");
        System.out.println("Do you want to do it again? (y/n): ");
        String quitOrDoItAgain = keyboard.next();

        if( "n".equalsIgnoreCase(quitOrDoItAgain)){
            letsDoThisAgain = false;
        }
    }
    System.out.println("Thanks for playing... Good Bye!");
}

5 Comments

Thank you so much for the help. I should've added this in the question and that is my fault. This assignment is an introduction to arrays, so it would have to be used through an array, unfortunately.
@Jusinr518 ah, I see... I’ll update my answer when I get back to my desk
@Jusinr518 I have updated my answer to use an array to store inputs instead of individual variables
Thank you so much for taking time out of your day to assist me with my issue. This is perfect and will definitely lead me towards the right direction.
@Jusinr518 I am glad I could help. Dont' forget to upvote useful answers :)
0

By the usage of a proper data structure like Map<string year, object playerstats> including OBP calculation on the object player stats before storing but this is an in-memory solution only last till the program is running if you like persistent then look on the database side

Also from the way you have presented your code it seems you are over-writing the value at year[index] always.

if you just want to use an array, then go like

years[index] = year[index] (math operation) keyboard.nextInt()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.