【问题标题】:How to print an ArrayList from a seperate method?如何从单独的方法打印 ArrayList?
【发布时间】:2019-09-27 09:48:41
【问题描述】:

我正在尝试这样做:如果用户选择此选项,程序会将读入的数据打印到屏幕上。

样本输出:

日期:2018 年 1 月 10 日输出:236.9 日期:2018 年 1 月 11 日产量:267.6 日期:2018 年 1 月 12 日输出:278.1

编写一个名为 PrintData 的方法来完成此任务。选择适当的参数和返回类型。

打印完成后,将再次显示主菜单。

我已经在我的上传数据方法中将数据读入我的数组列表,我现在正试图弄清楚如何制作一个单独的方法来打印 UploadData 的数组列表。

我不认为我现在拥有的东西不会起作用。

           : `  public static void main(String[] args) throws IOException {
     Scanner keyboard = new Scanner(System.in);
    //calling menu
    showMenu();
   int userChoice=keyboard.nextInt();
    while (userChoice == 1)
    {
        UploadData();
        showMenu();
        int userchoice=keyboard.nextInt();
    }
    while(userChoice ==2)
    {

        PrintData();
        showMenu();
        int userchoice=keyboard.nextInt();
    }
}

public static void showMenu()
{
    //creating menu
    System.out.println("Welcome to the Power Plant Analyzer program. Please choose from the following options:\n"
            + " 1. Upload Data\n 2. View Data \n 3. Download Statistics \n 4. Print Month \n 5. Exit the program");

}
public static String[] UploadData() throws IOException
{
    Scanner keyboard = new Scanner(System.in);
    //Asking for file
    System.out.println("What is the name of the file that contains the data?");
    String inputFileName=keyboard.nextLine();
   //creating file
    File f = new File(inputFileName);
    Scanner inputFile = new Scanner(f);

    //creating arraylist
    ArrayList<Entry> MonthList = new ArrayList<>();
    //reading the file
    while(inputFile.hasNext())
    {
        //read a line
        String m = inputFile.next();
        String d = inputFile.next();
        String y = inputFile.next();
        float p = inputFile.nextFloat();

        //create Entry with info read in
        Entry i = new Entry(m,d,y,p);
        //add it to the arraylist
        MonthList.add(i);

    }
    //print Entry's into arraylist
    //for(int i =0; i <MonthList.size(); i++)
        //MonthList.get(i).print();
    return null;

}
public static void PrintData(ArrayList<Entry> MonthList)
{
    for(int i =0; i <MonthList.size(); i++)
        MonthList.get(i).print();


    //ArrayList<Entry> MonthList = new ArrayList<>(Arrays.asList());
    //int pos = Collections.binarySearch(MonthList);

}

}`

  //declaring variables
private String month;
private String day;
private String year;
private float powerOutput;

//Constructors
public Entry(){}

public Entry (String m, String d, String y, float p)
{
    month = m;
    day =d;
    year =y;
    powerOutput=p;
}
//creating print to call ArrayList in main
public void print()
{
    System.out.println("Month: " + month + " Day: " + day + " Year: " + year + " Power Output: " + powerOutput);
}

}

【问题讨论】:

  • 不相关:尝试使用有意义的名称,即使是参数名称也应该超过 m d y p。并且:始终遵循 java 约定。方法是 camelCase(),所以不要做一些 camelCase() 和一些 UpperCase()。
  • 对于我主要的这一行:PrintData();我收到一个错误“类 Project 中的方法 PrintData 不能应用于给定类型:必需:ArrayList
  • MonthList 是静态声明项吗?
  • 我不太确定,但我想是的

标签: java arrays class arraylist methods


【解决方案1】:

这看起来像一个课堂练习,所以我不会为你写一个解决方案,但我会尝试给你一些提示,为你指明正确的方向

1) 您不能直接访问在方法UploadData() 中创建的arraylist,因为它是一个局部变量。鉴于您的描述要求您“选择适当的参数和返回类型”,您应该从 UploadData() 返回“MonthList”而不是 null。 [注意:您可能需要将 MonthList 结果添加到主类中的另一个 List,因为您在 while 循环中调用此方法]

2) 您的方法 PrintData()ArrayList&lt;Entry&gt; MonthList 作为参数,但在您的 main 方法中,您试图在不传入任何参数的情况下调用它。在您传递给调用的主要方法中有一个 ArrayList

希望对你有帮助

【讨论】:

    【解决方案2】:

    在您的 uploadData 方法中,使其返回一个 List&lt;Entry&gt; (而不是您甚至没有返回的 String[] ),因此不要使用 return null; ,而是执行 return MonthList; 并将其签名更改为:

    public static List<Entry> UploadData() throws IOException {
    

    然后在您的主要方法代码中,添加 this 的声明,并将其与您的 UploadData 和 PrintData 方法一起使用:

    public static void main(String[] args) throws IOException {
        ArrayList<Entry> MonthList = null;
        ...
    
        while (userChoice == 1)
        {
            MonthList = UploadData();
            showMenu();
            int userchoice=keyboard.nextInt();
        }
        while(userChoice ==2)
        {
    
            PrintData(MonthList);
            showMenu();
            int userchoice=keyboard.nextInt();
        }
    

    【讨论】:

    • 我将方法更改为:public static List UploadData() throws IOException but I'm getting symbol not found under List
    • 好吧,这是我的第一堂编程课,所以我承认我有点菜鸟,在某些时候这是一个压倒性的话题,而且我的教授通过一些章节速度很快,所以我在阅读方面明显落后。我对实际结果有疑问,如果我输入 2,它会再次要求输入文件,然后不打印任何内容。我认为我的 while 循环有问题?
    • 我将它们更改为 if 语句,如果用户选择 2 它不打印任何内容并且程序停止,是因为这一行吗? ArrayList MonthList =null;
    猜你喜欢
    • 1970-01-01
    • 2020-12-29
    • 2020-08-14
    • 2012-10-14
    • 2018-12-22
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多