【问题标题】:Saving User Input to an ArrayList with Multiple Variable Types将用户输入保存到具有多种变量类型的 ArrayList
【发布时间】:2018-12-31 00:26:31
【问题描述】:

我正在做一个家庭作业,我需要创建一个 Java ArrayList 来保存从 Scanner 读取的多个变量类型,以描述有关车辆的详细信息(即品牌、型号、颜色、年份、里程)。这应该是一个车辆库存应用程序。实际上,我很难将用户输入的信息添加到 ArrayList 中,这样我就可以将 ArrayList 打印到控制台以读回用户输入的内容。

这是我第一次编写具有多种变量类型(String、int)的 ArrayList,因此我很难将所有信息集中在一个地方。

感谢大家的帮助——我是编程的超级新手,感谢您为改进我的代码提供的任何见解。

import java.util.ArrayList;
import java.util.Scanner;

/*
 * This is a program that will store and allow
 * the user to manipulate information regarding 
 * a vehicle in an inventory 
*/



public class Automobileinventory {


public static void main(String[] args) {

Automobile automobile = new Automobile();
addVehicle(); 

}


    public static void addVehicle() {

    Scanner scnr = new Scanner(System.in);

    String make;
    String model;
    String color;
    int year;
    int mileage;

    ArrayList<Automobile> list = new ArrayList<Automobile> ();
    list.add (new Automobile());

    System.out.println("Enter vehicle make:");
    make = scnr.nextLine();

    System.out.println("Enter vehicle model:");
    model = scnr.nextLine();

    System.out.println("Enter vehicle color:");
    color = scnr.nextLine();

    System.out.println("Enter vehicle year:");
    year = scnr.nextInt();

    System.out.println("Enter vehicle mileage");
    mileage = scnr.nextInt();


    Automobile c = new Automobile ();

    list.add(c);



    for(int i=0;i<list.size();i++){

        System.out.println(list.get(i));

    } 


    }

输出:

现在——当我运行代码时,我得到了:

输入车型: 庞蒂亚克

输入车辆品牌: 大上午

输入车辆颜色: 绿色

输入车辆年份: 1997

输入车辆里程 200000

汽车@55f96302 汽车@3d4eac69

代码应该返回:

预期输出:

输入车型: 庞蒂亚克

输入车型: 大上午

输入车辆颜色: 绿色

输入车辆年份: 1997

输入车辆里程 200000

庞蒂亚克 Grand Am Green 1997 200000

【问题讨论】:

  • 您的输出语句System.out.println(list.get(i)); 打印列表中的每个Automobile。它利用toString() 来做到这一点。这默认为Object.toString()。您需要在 Automobile 上覆盖 toString() 以获得所需的结果。

标签: java


【解决方案1】:

你快到了,但有几个变化,

String make,String model,String color, int year,int mileage 的所有属性都应该是 Automobile 类的一部分,Getters and Setters

class Automobile{

 String make;
    String model;
    String color;
    int year;
    int mileage;

    public String getMake() {
        return make;
    }
    public void setMake(String make) {
        this.make = make;
    }
    public String getModel() {
        return model;
    }
    public void setModel(String model) {
        this.model = model;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
    public int getMileage() {
        return mileage;
    }
    public void setMileage(int mileage) {
        this.mileage = mileage;
    }
}

用所需的输出覆盖Automobile 类的toString() 方法,示例

@Override
    public String toString() {
        return "Automobile [make=" + make + ", model=" + model + ", color=" + color + ", year=" + year
                + ", mileage=" + mileage + "]";
    }

addVehicle 中始终为每辆车创建Automobile 对象并设置所有这些属性,然后将其添加到列表中

public static void addVehicle() {

        Scanner scnr = new Scanner(System.in);

        ArrayList<Automobile> list = new ArrayList<Automobile> ();
        Automobile automobile = new Automobile();

        System.out.println("Enter vehicle make:");
        automobile.setMake(scnr.nextLine());

        System.out.println("Enter vehicle model:");
        automobile.setModel(scnr.nextLine());

        System.out.println("Enter vehicle color:");
        automobile.setColor(scnr.nextLine());

        System.out.println("Enter vehicle year:");
        automobile.setYear(scnr.nextInt());

        System.out.println("Enter vehicle mileage");
        automobile.setMileage(scnr.nextInt());

        list.add(automobile);



        for(int i=0;i<list.size();i++){

            System.out.println(list.get(i)); //this will print the tostring representation of automobile object

        } 

    }

【讨论】:

  • 经典风格:/
  • 感谢@Deadpool——我能够结合来自回复的一些建议来获得一些很好的结果。我做了一些更改,现在我成功地使用 getter 和 setter 打印用户输入。为了存储用户输入,我现在使用代码: System.out.println("Enter vehicle make:"); c.make = (scnr.nextLine());
【解决方案2】:

要实现这一点,您需要覆盖 Automobile 类的 toString 方法:

private static final String SEPARATOR = " ";

@Override
public String toString() {
    return new StringBuilder(getModel())
                            .append(SEPARATOR)
                            .append(getMake())
                            .append(SEPARATOR)
                            .append(getColor())
                            .append(SEPARATOR)
                            .append(getYear())
                            .append(SEPARATOR)
                            .append(getMileage())
                            .toString();
}

Here you can find the documentationObject.toString

根据文档的方法的目的:

返回对象的字符串表示形式。通常,toString 方法返回一个“以文本形式表示”该对象的字符串。结果应该是一个简洁但信息丰富的表示,易于人们阅读。建议所有子类重写此方法。

【讨论】:

    【解决方案3】:

    你的代码有一些问题:

    您得到了正确的所有信息,但将它们存储在辅助变量中,并且在添加到列表之前永远不会进入c。 你需要这样做:

    Automobile c = new Automobile ();
    
    System.out.println("Enter vehicle make:");
    c.make = scnr.nextLine();
    
    System.out.println("Enter vehicle model:");
    c.model = scnr.nextLine();
    
    System.out.println("Enter vehicle color:");
    c.color = scnr.nextLine();
    
    System.out.println("Enter vehicle year:");
    c.year = scnr.nextInt();
    
    System.out.println("Enter vehicle mileage");
    c.mileage = scnr.nextInt();
    
    list.add(c);
    

    第二个问题在

    for(int i=0;i<list.size();i++){
    
        System.out.println(list.get(i));
    
    }
    

    System.out.println 不会打印任何类型的对象,例如魔法,您需要传递一些字符串,例如 i.name 才能打印。如果您像在上面的代码中那样通过,您只是传递了一个要打印的 指针,您可以在输出中看到 java 地址引用。

    OBS.:尝试使用一些好的做法,例如Encapsulation

    祝你好运,欢迎来到编程世界。

    编辑:

    cmets 中的其他人通过system.out.println 展示了如何使对象可打印,并且是您输出的解决方案之一。进行更正以确保您存储的信息是正确的,并且他们会尝试该方法。

    【讨论】:

    • 感谢您在编辑中的澄清——我能够使用每个人的帮助来运行代码。
    • 很高兴看到我们提供了帮助!如果这是您的解决方案,请标记为解决方案以帮助其他人
    猜你喜欢
    • 1970-01-01
    • 2017-09-15
    • 2019-03-12
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2014-02-07
    相关资源
    最近更新 更多