0

My problem statement is:

Write a program that creates two instances of the generic class LinkedList.
The first instance is stadiumNames and will hold items of type String.
The second instance is gameRevenue and will hold items of type Double.
Within a loop, read data for the ball games played during a season.
The data for a game consists of a stadium name and the amount of money made for that game.
Add the game data to stadiumNames and gameRevenue.
Since more than one game could be played at a particular stadium, stadiumNames might have duplicate entries.
After reading the data for all of the games, read a stadium name and display the total amount of money made for all the games at that stadium.

I'm trying to get each input from the user and then add each input together and get its sum, it seems to get it right at first, but then it prints another totally different amount. Why is that? Any help appreciated.

Each input the stadiumName and gameRevenue were added to a linkedList.

Note that I already wrote both linked lists but it won't allow me to post a big chunk of code. Thank you.

boolean Data = true;
while (Data) {
    stadiumNames.add(name);
    gameRevenue.add(rev);
    System.out.println("Do you want another game? ");
    String yesorno = scan.next();
    if (yesorno.equals("No"))
        break;
    else {
        if (yesorno.equals("yes"))
            System.out.println("Enter stadium name: ");
        name = scan.next();
        System.out.println("Enter amount of money for the game: ");
        rev = scan.nextDouble();
        for (int i = 0; i < stadiumNames.size(); i++) {
            if (stadiumNames.get(i).equals(name)) {
                rev += gameRevenue.get(i);
                System.out.println("The total amount of money for " + name + " is " + rev);
            }
        }
    }
}

Attached image of the output

Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
  • 2
    Wecome to SO. Please cleanup the code. The indentation is bad. Good formatting makes the code easier to read and helps spot bugs. And also: [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) – 001 Nov 13 '21 at 15:37
  • This may be related to the issue: [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/q/13102045/669576) – 001 Nov 13 '21 at 15:40
  • Welcome to stackoverflow. I just formatted your question, please also 1. replace the image with text 2. provide example input, the output from your program and correct output you expect. – samabcde Nov 13 '21 at 15:41
  • @Christopher Prinze, please put the expected output and actual output. – rushi Nov 13 '21 at 15:47
  • I just added some variable initialiser and don't see problem you reported. You can run your own code here https://onecompiler.com/java/3xhbauf39 – Optional Nov 13 '21 at 17:04

1 Answers1

0

If you want to print running total while user is entering the data, total should be reset for each calculation.


while (true) {
    System.out.println("Do you want another game? ");
    String yesorno = scan.next();
    if (yesorno.equals("No"))
        break; // else not needed

    System.out.println("Enter stadium name: ");
    name = scan.next();
    System.out.println("Enter amount of money for the game: ");
    rev = scan.nextDouble();

    stadiumNames.add(name);
    gameRevenue.add(rev);

    double total = 0.0;

    // recalculating the total for the last stadium
    for (int i = 0; i < stadiumNames.size(); i++) {
        if (stadiumNames.get(i).equals(name)) {
            total += gameRevenue.get(i);
        }
    }
    System.out.println("The total amount of money for " + name + " is " + total);
}

However, it may be needed to calculate the totals for multiple different stadiums and a map needs to be created and filled for this after the while loop.
It is convenient to use Map::merge function to accumulate the totals per stadium name.

Map<String, Double> totals = new LinkedHashMap<>();
for (int i = 0; i < stadiumNames.size(); i++) {
    totals.merge(stadiumNames.get(i), gameRevenue.get(i), Double::sum);
}
totals.forEach((stad, sum) -> System.out.println("The total amount of money for " + stad + " is " + sum));

Aside comment: it is not recommended to use double for financial calculations because floating point maths is not precise.

Nowhere Man
  • 19,170
  • 9
  • 17
  • 42