I'm trying to use an enhanced for loop to call a method many times in a row, but if I'm iterating more than once, the first iteration will only read the first line of code. Here are both of the methods I'm using:
public Account() {
this.subDetails = new HashMap<Integer, String>();
System.out.println("What is the account type? (Individual/Family)");
String planType = keyboard.nextLine();
System.out.println("Enter your card number: ");
this.cardNum = keyboard.nextLine();
System.out.println("Enter the expiration date: ");
this.cardExp = keyboard.nextLine();
if (planType.equals("Family")) {
System.out.println("How many users?");
int numUsers = keyboard.nextInt();
for (int z=0; z<numUsers; z++) {
this.addUser();
}
StreamingService.numFamUsers = StreamingService.numFamUsers + numUsers;
StreamingService.monthlyRevenue = StreamingService.monthlyRevenue + 14.99;
StreamingService.monthlyFamRevenue = StreamingService.monthlyFamRevenue + 14.99;
}
else if (planType.equals("Individual")) {
this.addUser();
StreamingService.numIndUsers++;
StreamingService.monthlyRevenue = StreamingService.monthlyRevenue + 9.99;
StreamingService.monthlyIndRevenue = StreamingService.monthlyIndRevenue + 9.99;
}
StreamingService.numAccounts++;
this.subDetails.put(i, planType);
i++;
}
public void addUser() {
System.out.println("What is the email of the next user?");
String e = keyboard.nextLine();
User y = new User(e, i);
StreamingService.userEmails.add(y);
StreamingService.numUsers++;
this.acctUsers.add(e);
}
And here is the output (Data quality is poor, just used as an example):
What is the account type? (Individual/Family)
Individual
Enter your card number:
1234123412341234
Enter the expiration date:
02/24
What is the email of the next user?
abc@def.com
What is the account type? (Individual/Family)
Family
Enter your card number:
1234123412341234
Enter the expiration date:
02/24
How many users?
3
What is the email of the next user? What is the email of the next user?
abc@def.com
What is the email of the next user?
abc@defg.com
What is the account type? (Individual/Family)
Family
Enter your card number:
1234123412341234
Enter the expiration date:
02/24
How many users?
2
What is the email of the next user? What is the email of the next user?
xyz@abc.com
Does anyone know how to fix this?
Scanner.nextInt
does NOT consume the newline-charakter you enter after the nubmers. Because of this, any readline afterwards will consume the newline - and stop, becasue that's what it does.
Because of this, the first email prompt after entering the number of users will always return an empty string. You can see this in your output: it's the cases where the email prompt is duplicated.
To fix this, put a call to keyboard.nextLine()
after all of your keyboard.nextInt()
calls (and just ignore the output of that function).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.