简体   繁体   English

arraylist在单独的循环方法中没有正确打印出来? 包装类或循环责备?

[英]arraylist isn't printing out correctly in a separate method for loop? wrapper class or loops to blame?

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. 对于我的项目,我有一个arraylist供用户在一个方法中输入他们想要的任何名称,只要它不停止。 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: 例:

  1. enter names: 输入名称:
  2. name: bob 名字:鲍勃
  3. name: joe 姓名:乔
  4. name: Stop this triggers another method to prompt more info: name:停止这会触发另一种提示更多信息的方法:

  5. bob: enter age: enter address: bob:输入年龄:输入地址:

  6. 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. 但是,现在,arraylist没有正确打印出来,我正在重复某些名称,而其他名称没有出现在第二种方法中。 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. 我输入我输入的名称的信息,但我不断收到“name:”提示,并且没有实际的arraylist值。 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. 我也认为这个问题可能与我如何使用包装器将值放入arraylist有关,但我不知道如何解决这个问题。

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. 在第一种方法中,我尝试使用不同的boolean while循环交换循环,同时直接向上(!input.equals(“Stop”)),在前两个选项内部的for循环计数器,如果循环,以及上面的一些组合。

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: 我还没有密切关注,但我强烈怀疑这是问题所在:

if(ID.equals("Stop"))
    breaker = true;
else
    buffer = new Directory(ID); 
    nameList.add(buffer); 

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. 当ID不等于“停止”时,您似乎期望最后一个语句只执行,但实际上它总是会执行。 Unlike (say) Python, whitespace is irrelevant in Java. 与(比如)Python不同,空格与Java无关。 If you statements to be considered as a block, you need braces: 如果您将语句视为块,则需要大括号:

if(ID.equals("Stop"))
    breaker = true;
else {
    buffer = new Directory(ID); 
    nameList.add(buffer); 
}

Personally I would put braces around both parts: 就个人而言,我会在两个部分周围放置括号:

if (ID.equals("Stop")) {
    breaker = true;
} else {
    buffer = new Directory(ID); 
    nameList.add(buffer); 
}

... and quite possibly get rid of the somewhat-irrelevant buffer variable: ......并且很可能摆脱了有些无关的buffer变量:

if (ID.equals("Stop")) {
    breaker = true;
} else {
    nameList.add(new Directory(ID));
}

I'd also get rid of the breaker variable, and limit the scope of ID (changing its name, too, to comply with normal conventions) with a result of: 还将摆脱breaker变量,并限制ID的范围(更改其名称,以符合常规约定),结果为:

while (true) {
    System.out.print("Name:  ");
    string id = LocalInput.nextLine();
    if (id.equals("Stop")) {
        break;
    }
    nameList.add(new Directory(ID));
}

Critique of second method 第二种方法的批判

This is really odd at the moment. 目前这真的很奇怪。 It's not at all clear where the i variable is declared, or why you only fetch buffer once, or why you're just adding to the existing list rather than mutating the existing object. 它根本不清楚声明i变量的位置,或者为什么你只获取buffer一次,或者为什么你只是添加到现有列表而不是改变现有对象。 I suspect you really want: 我怀疑你真的想要:

for (Directory entry : nameList) {
    System.out.println("Name: " + entry.GetID());
    System.out.println( "Age:\t");
    double age = LocalInput.nextDouble();
    entry.setAge(age);
    System.out.print( "Address:\t");   
    String address = LocalInput.nextLine();
    entry.setAddress(address);
}

Note that your current loop will always continue until i equals nameList.size() - but you're always increasing the size in the loop, so you'll never terminate. 请注意,您的当前循环将一直持续到i等于nameList.size() - 但是您总是在循环中增加大小,因此您永远不会终止。

private void getInfo()
{
    Scanner LocalInput = new Scanner(System.in);
    double age; String address;

    for (Directory name : nameList) {
    System.out.println("Name: " + name .GetID());
    System.out.println( "Age:\t");
    double age = LocalInput.nextDouble();
    name.setAge(age);
    System.out.print( "Address:\t");   
    String address = LocalInput.nextLine();
    name.setAddress(address);
}
}

get method should be like this get方法应该是这样的

and Add must be - 和添加必须 -

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); 
        }
    }
}

Consider using the break keyword, then you don't need the breaker flag and the else branch: 考虑使用break关键字,然后您不需要breaker标志和else分支:

while(true)
{
    System.out.print("Name:  ");
    ID = (LocalInput.nextLine());
    if(ID.equals("Stop"))
        break;

    buffer = new Directory(ID); 
    nameList.add(buffer); 
}

Sorry was already posted... 抱歉 ,已发布...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM