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