You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何正确传递数组到方法?解决Java中cannot find symbol错误

问题:打印time字段不为0的人员姓名时出现编译错误

我想要打印出time字段不为0的人员姓名,程序无法运行,已输入条目数为5。要求仅修改数组相关部分,不使用ArrayList,数组维度由手动输入的条目数决定。

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

问题原因与修复方案

核心问题

  1. 变量作用域错误:persons数组是readFile方法的局部变量,main方法无法直接访问。
  2. 变量名不匹配:timeNotZero方法参数是numberOfEntries,但循环中用了未定义的numOfEntries;打印时错误使用了不存在的drivers数组。
  3. 语法错误:main中调用timeNotZero后缺少分号。
  4. 缺失Person类:原代码中没有定义Person类,编译时会额外报错。

修复后的完整代码

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返回的数组
        Person[] persons = readFile("PersonStorage.csv", numOfEntries);

        System.out.println("These are the people that do not have their time as 0");
        timeNotZero(persons, numOfEntries); // 添加分号
        
    }

    // 修改方法返回值为Person[],返回创建好的数组
    public static Person[] readFile(String pFileName, int numOfEntries)
    {   
        FileInputStream fileStream = null;
        InputStreamReader rdr;
        BufferedReader bufRdr;
        int lineNum;
        String line;
        Person[] persons = new Person[numOfEntries]; // 提前定义数组
        
        try
        {
            fileStream = new FileInputStream(pFileName);
            rdr = new InputStreamReader(fileStream);
            bufRdr = new BufferedReader(rdr);
            lineNum = 0;
            line = bufRdr.readLine();

            // 添加数组边界判断,避免越界
            while(line != null && lineNum < numOfEntries)
            {
                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());
        }
        return persons; // 返回数组给main方法
    }

    private static void processLine(String csvRow, int lineNum, Person[] persons)
    {
        String[] data;
        data = csvRow.split(",");
        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)
    {
        // 使用方法参数numberOfEntries作为循环边界
        for (int j=0; j < numberOfEntries; j++)
        {
            if (persons[j].getTime() != 0)
            { 
                // 使用正确的persons数组获取姓名
                System.out.println(persons[j].getName());
            }
        }
    }
}

// 补充缺失的Person类
class Person {
    private String name;
    private int age;
    private double time;

    public Person(String name, int age, double time) {
        this.name = name;
        this.age = age;
        this.time = time;
    }

    public String getName() {
        return name;
    }

    public double getTime() {
        return time;
    }
}

修复说明

  • 调整readFile方法的返回类型,将创建好的persons数组返回给main方法,解决作用域问题。
  • 修正timeNotZero方法中的变量名错误,确保循环和打印操作使用正确的数组与参数。
  • 补充main方法中调用timeNotZero后的分号,修复语法错误。
  • 添加缺失的Person类,确保程序能正常编译运行。
  • 在readFile的循环中添加数组边界判断,避免因文件行数超过输入条目数导致的数组越界。

内容的提问来源于stack exchange,提问作者boomchikaci

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 13:59:51