java - 如何从单独的方法打印 ArrayList?
问题描述
我正在尝试这样做:如果用户选择此选项,程序会将读取的数据打印到屏幕上。
样本输出:
日期:2018 年 1 月 10 日 输出:236.9 日期:2018 年 1 月 11 日 输出:267.6 日期:2018 年 1 月 12 日 输出:278.1
编写一个名为 PrintData 的方法来完成此任务。选择适当的参数和返回类型。
打印完成后,将再次显示主菜单。
我已经在我的方法 Upload Data 中将数据读入我的数组列表,我现在正试图弄清楚如何制作一个单独的方法来打印 UploadData 的 Arraylist。
我现在拥有的东西不会起作用,我不认为。
: ` 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);
}
}
解决方案
在您的 uploadData 方法中,使其返回 a List<Entry>
(而不是您甚至没有返回的 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();
}
推荐阅读
- macos - 当我的角色从屏幕上掉下来时,我怎样才能让我的角色死掉?
- docker - CloudSQL docker容器一段时间无法连接到sql实例然后在k8s中连接
- r - 如何使ggplot2中的所有轴刻度都朝内,包括辅助轴
- javascript - 发布依赖于返回 true 的 javascript 函数的表单
- android - 如何限制服务更新Activity Textview?
- forms - 关注 NativeScript+Angular 应用程序中的文本字段时,表单字段不会向上移动
- vue.js - v-treeview 在初始加载期间检查项目
- javascript - 包含动态值作为子字符串的字符串的 Postman 断言
- php - Elasticsearch,忽略 GET 响应中的内置映射细节
- python - 如何在嵌套的 for 循环中使用“i”和“j”编写线性递增函数?