我想打印出时间不为0的人的名字,但也许我传递参数的方式不起作用?我输入的条目数为 5
注意: 我知道数组列表,但我不想在这里使用数组列表。如果除了有关数组的事情之外不对程序的其他部分进行任何更改,我将不胜感激。代码不必非常灵活,因此我没有找出数组维度的代码中有多少行,而是手动要求用户输入它
CSV文件中存储的数据(按姓名、年龄、时间顺序):
Lana,20,6.3
Daniel,30,0
Lucas,29,4.9
Henry,19,8.2
Johnny,59,5.3
代码:
import java.util.*;
import java.io.*;
public class personProgram
{
public static void main(String[] args)
{
int numOfEntries;
Scanner sc = new Scanner(System.in);
System.out.println("How many entries are there in the file?");
numOfEntries = sc.nextInt();
readFile("PersonStorage.csv", numOfEntries);
System.out.println("These are the people that do not have their time as 0");
timeNotZero(persons, numOfEntries)
}
public static void readFile(String pFileName, int numOfEntries)
{
FileInputStream fileStream = null;
InputStreamReader rdr;
BufferedReader bufRdr;
int lineNum;
String line;
try
{
fileStream = new FileInputStream(pFileName);
rdr = new InputStreamReader(fileStream);
bufRdr = new BufferedReader(rdr);
lineNum = 0;
line = bufRdr.readLine();
Person[] persons = new Person[numOfEntries];
while(line != null)
{
processLine(line, lineNum, persons);
line = bufRdr.readLine();
lineNum++;
}
fileStream.close();
}
catch (IOException errorDetails)
{
if(fileStream != null)
{
try
{
fileStream.close();
}
catch(IOException ex2)
{ }
}
System.out.println("Error in fileProcessing: " + errorDetails.getMessage());
}
}
private static void processLine(String csvRow, int lineNum, Person[] persons)
{
String[] data;
data = csvRow.split(",");
int lineLength = data.length;
String name = data[0];
int age = Integer.parseInt(data[1]);
double time = Double.parseDouble(data[2]);
persons[lineNum] = new Person(name, age, time);
}
private static void timeNotZero(Person[] persons, int numberOfEntries)
{
for (int j=0; j < numOfEntries; j++)
{
if (persons[j].getTime() != 0)
{
System.out.println(drivers[j].getName());
}
}
}
错误:
error: cannot find symbol
timeNotZero(persons, numOfEntries)
^
symbol: variable persons
尝试创建调用函数的 person obj timeNotZero