0

The purpose of the program is to assign people money. For example when it say 200 3. Dave pays 200 dollars and the amount is split between the following three people: laura, owen, and vick. Dave gets the remainder when 200/3 and the three people get 66 dollars (200/3 rounded down). Additionally, Dave loses 200 dollars. The "5" at the beginning indicates the number of people followed by their names. The goal is to find the total amount of money for each person. I believe the logic of the program is fine as the output show up correctly.

The part I need help with is why I need to press the ENTER key twice for the output to show.

I think the reason that I must press enter twice is because I say "nextName = sc.nextLine" So on the very last time its in the while loop, it still gets that nextLine. I'm not sure how to fix this problem and still receive the correct answers.

Can someone please help me get the program to show the right answers by only pressing the ENTER key once?

I tried looking at other StackOver Flows, but nothing solved my problem.

Thanks!

import java.util.*;

public class gift2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        gift2 obj = new gift2();
        obj.run();
    }

    public void run() {
        Scanner sc = new Scanner(System.in);
        HashMap<String, Integer> map = new HashMap<>();
        double currentAmount = 0;
        int giverDollars = 0;
        int numGetters = 0;
        String newGetters = "";
        String giver = "";
        int leftOver = 0;
        String newLine = "";
        String tracker = "";

        int numInput = Integer.parseInt(sc.nextLine());
        for (int i = 0; i < numInput; i++) {
            map.put(sc.nextLine(), (int) currentAmount);
        }

        String nextName = sc.nextLine();
        giver = nextName;

        while (!(nextName.equals(""))) {

            String[] dollars = sc.nextLine().split(" ");
            giverDollars = Integer.parseInt(dollars[0]);
            numGetters = Integer.parseInt(dollars[1]);

            if (numGetters != 0) {
                currentAmount = (Math.floor(giverDollars / numGetters));
                leftOver = giverDollars % numGetters;
            }

            for (int i = 0; i < numGetters; i++) {
                newGetters = sc.nextLine();
                map.put(newGetters, (int) (map.get(newGetters) + currentAmount));
            }

            map.put(giver, (int) (map.get(giver) - giverDollars + leftOver));

            currentAmount = 0;

            nextName = sc.nextLine();

            giver = nextName;

        }

        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
    }
}

This is the input:

5
dave
laura
owen
vick
amr
dave
200 3
laura
owen
vick
owen
500 1
dave
amr
150 2
vick
owen
laura
0 2
amr
vick
vick
0 0
Spectric
  • 5,761
  • 2
  • 6
  • 27
  • 1
    Worth nothing that we call them "questions" or "Stack Overflow questions" rather than "StackOver Flows". Nice one though ;) – Spectric Nov 26 '20 at 19:23
  • `numGetters = 0` makes no sense, so the fact that last line is `0 0`, i.e. sets `numGetters = 0`, marks the end of the exercise. So, add `if (numGetters == 0) break;` right after `numGetters` is assigned, and you get what you want. – Andreas Nov 26 '20 at 19:31

1 Answers1

0

Judging by the format of the input the execution is supposed to terminate with 0 0, so you can implement a check such that the loop terminates when you get that input. For example you can use a do...while loop as such:

// Remove nextName definition and giver setting before the loop

do {
    String nextName = sc.nextLine();
    giver = nextName;
    String[] dollars = sc.nextLine().split(" ");
    giverDollars = Integer.parseInt(dollars[0]);
    numGetters = Integer.parseInt(dollars[1]);

    if (numGetters != 0) {
        currentAmount = (Math.floor(giverDollars / numGetters));
        leftOver = giverDollars % numGetters;
    }

    for (int i = 0; i < numGetters; i++) {
        newGetters = sc.nextLine();
        map.put(newGetters, (int) (map.get(newGetters) + currentAmount));
    }

    map.put(giver, (int) (map.get(giver) - giverDollars + leftOver));
    currentAmount = 0;
} while (giverDollars != 0 || numGetters != 0);

But you can rearrange your code however you like.