My output program is skipping the second data entry, which is the first Name entry, and I can't seem to understand why. If I define n <=2 in my while-loop, then the program runs correctly. However, it should be based on the user entry and not predefined. I would greatly appreciate as to why the program is skipping the second data entry of a person's name, and leaving it blank. Thank you!
PhoneBookEntry Class:
public class PhoneBookEntry
{
public PhoneBookEntry(String Name, String PhoneNumber)
{
this.Name = Name;
this.PhoneNumber = PhoneNumber;
}
private String Name;
private String PhoneNumber;
public String getName()
{
return this.Name;
}
public void setName(String Name)
{
this.Name = Name;
}
public String getPhoneNumber()
{
return this.PhoneNumber;
}
public void setPhoneNumber(String PhoneNumber)
{
this.PhoneNumber = PhoneNumber;
}
}
PhoneBookEntry Driver Program:
import java.util.Scanner;
import java.util.ArrayList;
public class PhoneBookDriver
{
public static void main(String[] args)
{
ArrayList<PhoneBookEntry> List = new ArrayList<PhoneBookEntry>();
String a, b;
int n = 1;
int NumContacts;
Scanner scan = new Scanner(System.in);
System.out.println("Enter The Number Of Contacts You Want To Save: ");
NumContacts = scan.nextInt();
while (n <= NumContacts)
{
System.out.println("Enter A Name: ");
a = scan.nextLine();
System.out.println("Enter Their Phone Number: ");
b = scan.nextLine();
List.add(new PhoneBookEntry(a, b));
n++;
}
System.out.println("Here's The Data You Entered");
for (PhoneBookEntry entry : List)
{
System.out.printf("Name: %s\tNumber: %s\n", entry.getName(), entry.getPhoneNumber());
}
}
}
Output:
----jGRASP exec: java PhoneBookDriver
Enter The Number Of Contacts You Want To Save:
2
Enter A Name:
Enter Their Phone Number:
5869302958
Enter A Name:
Bob
Enter Their Phone Number:
7530572957
Here's The Data You Entered
Name: Number: 5869302958
Name: Bob Number: 7530572957
----jGRASP: operation complete.