for my project, i have an arraylist for the user to input whatever names they want in one method as long as it isn’t stop. then in a separate method, the values i just inputted have to be called out again, so i can input more information for each name.
example:
- enter names:
- name: bob
- name: joe
-
name: Stop
this triggers another method to prompt more info: -
bob:
enter age:
enter address: -
joe:
enter age:
enter address:However, right now, the arraylist isn’t printing out correctly, and i’m getting repeats of certain names, while other names don’t show up in the second method. Also, the loop in the second method doesn’t end. i enter in info for the names i entered, but i keep getting prompted with “name:” and no actual arraylist value. I suspect something is wrong with my loops, but i don’t quite know how to fix it? I also thought that maybe the problem has something to do with how i’m using a wrapper to put in values into the arraylist, but i don’tknow how to fix that.
in the second method, I’ve tried swapping the countervariable to keep track of the order in the Array List with a separate counter in the second method, but it doesn’t seem to make a difference. In the first method, i’ve tried swapping the loop with different a boolean while loop, with a straight up while (!input.equals(“Stop”)), a for loop counter inside of the previous two options, if loops, and some combination of the above.
here is my code
Scanner console = new Scanner(System.in);
private ArrayList<Directory> nameList; //i have to use object oriented programming to store values in the Directory Class
public int i;
first method:
private void addName()
{
Scanner LocalInput = new Scanner(System.in);
Directory buffer = null;
String ID = null;
System.out.println("Enter Station Designations Below, Enter Stop to quit");
boolean breaker = false;
while(breaker ==false)
{
System.out.print("Name: ");
ID = (LocalInput.nextLine());
if(ID.equals("Stop"))
breaker = true;
else
buffer = new Directory(ID);
nameList.add(buffer);
}
}
second method:
private void getInfo()
{
Scanner LocalInput = new Scanner(System.in);
Directory buffer;
buffer = nameList.get(i);
double age; String address;
System.out.println("Enter Temperatures below...");
System.out.println("" + nameList.get(i));
for (i = 0; i < nameList.size(); i++)
{
System.out.println("Name: " + buffer.GetID()); //there's a getter and setter in the Directory class
System.out.println( "Age:\t");
age = LocalInput.nextDouble();
System.out.print( "Address:\t");
address = LocalInput.nextLine();
buffer= new Directory(age, address);
nameList.add(buffer);
}
}
Critique of first method
I haven’t looked closely yet, but I strongly suspect this is the problem:
You appear to be expecting the last statement only to be executing when ID is not equal to “Stop”, but actually it’s always going to execute. Unlike (say) Python, whitespace is irrelevant in Java. If you statements to be considered as a block, you need braces:
Personally I would put braces around both parts:
… and quite possibly get rid of the somewhat-irrelevant
buffervariable:I’d also get rid of the
breakervariable, and limit the scope ofID(changing its name, too, to comply with normal conventions) with a result of:Critique of second method
This is really odd at the moment. It’s not at all clear where the
ivariable is declared, or why you only fetchbufferonce, or why you’re just adding to the existing list rather than mutating the existing object. I suspect you really want:Note that your current loop will always continue until
iequalsnameList.size()– but you’re always increasing the size in the loop, so you’ll never terminate.