So, I'm trying to make an "Unlimited" Array List of names and ages that the user inputs, specifically when they're asked if they want to enter another name and age, and say "yes". However, I'm able to only get the name and age that's been inputted the most recently by the user.
I'm trying to have the program the names and ages to the Console when the user says "no" to inputting another name and age. However, it will print the name and age before I even it an answer to that question.
I'll post the code in question below.
String add;
String answer;
ArrayList<String> names = new ArrayList<>();
ArrayList<int[]> ages = new ArrayList<>();
do
{
// Get the users name
System.out.println("What's your name? ");
name = keyboard.nextLine();
// Get the users name
System.out.println("What's your age? ");
age = keyboard.nextInt();
// Adds the names and ages from the user input into the Arrays
names.add(name);
ages.add(age);
// Ask the user if they want to enter in another record
System.out.println("Do you want to enter an additional record? ");
answer = keyboard.nextLine();
}
while (answer.equals("yes"));
do
{
// System.out.println(name);
// System.out.println(age);
/* System.out.println(Arrays.asList(print) + ", " + (ages));
// Printing the records to different lines of the console
System.out.println("");
*/
for (String print : names)
{
// System.out.println(names);
// System.out.println(ages);
System.out.println(print + ", " + (ages));
// Printing the records to different lines of the console
System.out.println("");
break;
}
}
while (answer.equals("no"));
Also, I'm asking the user if they want to write the name and age Arrays to a file. Yes means write to the file, no means I write a message that says "Thanks for playing". However, depending on where I the following if I type in "yes" or "no", it will either not give the file prompt, or goes into an infinite loop with the names and ages printed, depending on where I put it.
String answer2;
String write;
String fileName;
ArrayList<String> names = new ArrayList<>();
ArrayList<String> ages = new ArrayList<>();
// If I put it here, it will do a infinite loop of the names and ages inputted.
// Prompt the user if they want to write to a file.
System.out.println("Do you want to write to a file? ");
answer2 = keyboard.nextLine();
do
{
// If I put it here, it will continue to ask me the question infinitely.
// Prompt the user if they want to write to a file.
// System.out.println("Do you want to write to a file? ");
//answer2 = keyboard.nextLine();
}
while (answer.equals("no"));
do
{
// Prompt for the filename
System.out.println("Enter your filename: ");
fileName = keyboard.nextLine();
//break;
//PrintWriter outputFile;
try
{
PrintWriter outputFile = new PrintWriter(fileName);
// Write the name(s) and age(s) to the file
outputFile.println(names);
outputFile.println(ages);
outputFile.close();
}
catch (FileNotFoundException e)
{
//
e.printStackTrace();
break;
}
}
while (answer2.equals("yes"));
do
{
System.out.println("Thanks for playing!");
break;
}
while (answer2.equals("no"));
keyboard.close();