【问题标题】:I am getting the error in Java : cannot be resolved to a variable我在 Java 中遇到错误:无法解析为变量
【发布时间】:2021-08-31 18:52:47
【问题描述】:

我正在运行此代码并收到错误

System.out.print(name[i]+" ");
System.out.print(age[i]+" ");
System.out.print(country[i]+" ");

那个

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    name cannot be resolved to a variable
    age cannot be resolved to a variable
    country cannot be resolved to a variable

我该如何解决这个问题?


public class AnyInteger
{
    public static void main(String arg[]) throws FileNotFoundException
   
 {
        int n = 0;
         
try {
        BufferedReader in = new BufferedReader(new FileReader("D:\\Java\\eclipse-workspace\\AnyInteger\\src\\file.txt"));
        
String str;

        while ((str = in.readLine())!= null) {
            String[] arr =str.split("#");
            n = arr.length;
            String name[] = new String[n];
            int age[] = new int[n];
            String country[] = new String[n];
            int i=0;
            while(i<n)
            {
                for(int j=0;j<3;j++)
                {
                    name[j] = arr[i];
                    i = i+1;
                    age[j] = Integer.parseInt(arr[i]);
                    i = i+1;
                    country[j] = arr[i];
                    i = i+1;
                }
            }
        }
        in.close();
    }

catch (IOException e) {
        System.out.println("File Read Error");
    }
  
    
    System.out.println("Names: ");
    for(int i=0;i<n;i++)
    {
        System.out.print(name[i]+" ");
    }
    
     System.out.println("Ages: ");
    for(int i=0;i<n;i++)
    {
        System.out.print(age[i]+" ");
    }
    
     System.out.println("Countries: ");
    for(int i=0;i<n;i++)
    {
        System.out.print(country[i]+" ");
    }
    
    }
}

【问题讨论】:

  • names数组是while循环的局部变量,不能在while循环外使用
  • 同样agecountry
  • 那我该怎么改,请指教
  • 显示输入示例

标签: java oop


【解决方案1】:

您应该在 try/catch 语句之外声明数组 name[]age[]country[],如果需要,只需在 while 循环中实例化它。

类似这样的:

public class AnyInteger
{
    public static void main(String arg[]) throws FileNotFoundException
   
 {
        int n = 0;
        String name[];
        int age[];
        String country[];
         
try {
        BufferedReader in = new BufferedReader(new FileReader("D:\\Java\\eclipse-workspace\\AnyInteger\\src\\file.txt"));
        
String str;

        while ((str = in.readLine())!= null) {
            String[] arr =str.split("#");
            n = arr.length;
            name = new String[n];
            age = new int[n];
            country = new String[n];
            int i=0;
            while(i<n)
            {
                for(int j=0;j<3;j++)
                {
                    name[j] = arr[i];
                    i = i+1;
                    age[j] = Integer.parseInt(arr[i]);
                    i = i+1;
                    country[j] = arr[i];
                    i = i+1;
                }
            }
        }
        in.close();
    }

catch (IOException e) {
        System.out.println("File Read Error");
    }
  
    
    System.out.println("Names: ");
    for(int i=0;i<n;i++)
    {
        System.out.print(name[i]+" ");
    }
    
     System.out.println("Ages: ");
    for(int i=0;i<n;i++)
    {
        System.out.print(age[i]+" ");
    }
    
     System.out.println("Countries: ");
    for(int i=0;i<n;i++)
    {
        System.out.print(country[i]+" ");
    }
    
    }
}

【讨论】:

  • 它现在显示运行时错误:线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException:索引 3 超出长度 3 的范围
【解决方案2】:

您的代码无法编译,因为变量 name, age, country 已输出 范围,但是

这样的东西应该可以工作(未经测试)

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class AnyInteger {
    public static void main(String arg[]) throws FileNotFoundException {
        int n = 0;

        try {
            BufferedReader in = new BufferedReader(new FileReader("D:\\Java\\eclipse-workspace\\AnyInteger\\src\\file.txt"));

            String str;

            while ((str = in.readLine()) != null) {
                String[] arr = str.split("#");
                n = arr.length;
                String name[] = new String[n];
                int age[] = new int[n];
                String country[] = new String[n];
                int i = 0;
                while (i < n) {
                    for (int j = 0; j < 3; j++) {
                        name[j] = arr[i];
                        i = i + 1;
                        age[j] = Integer.parseInt(arr[i]);
                        i = i + 1;
                        country[j] = arr[i];
                        i = i + 1;
                    }
                    printMyStuff(name, age, country, n);
                }
            }
            in.close();
        } catch (IOException e) {
            System.out.println("File Read Error");
        }

    }

    private static void printMyStuff(String[] name, int[] age, String[] country, int n) {


        System.out.println("Names: ");
        for (int i = 0; i < n; i++) {
            System.out.print(name[i] + " ");
        }

        System.out.println("Ages: ");
        for (int i = 0; i < n; i++) {
            System.out.print(age[i] + " ");
        }

        System.out.println("Countries: ");
        for (int i = 0; i < n; i++) {
            System.out.print(country[i] + " ");
        }
    }

通常,使用静态方法不是一个好主意,但对于初学者来说应该没问题。

【讨论】:

  • 它现在显示运行时错误:线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException:索引 3 超出长度 3 的范围
【解决方案3】:

只要在while循环外声明名字、年龄和国家,一旦你知道'n'的值(在while循环内)实例化名字、年龄和国家。

您的代码应按以下方式更改:

    String[] name ;
    int[] age;
    String[] country;
    
    while ((str = in.readLine())!= null) {
        String[] arr =str.split("#");
        n = arr.length;
        name = new String[n];
        age = new int[n];
        country = new String[n];    
        
    }

注意:为了避免 ArrayOutOfBound 异常,只需确保 n 值始终大于或等于您在 while 内部使用的 for 循环中使用的数字 3。

【讨论】:

  • 你能帮我重写代码吗?我是新手,无法理解这些变化,尽我所能理解java的概念。
  • // 在 2 cmets 中提供答案,因为我没有足够的空间 // 不管逻辑如何,为您提供可以正常工作的代码。 //假设 n = 2 name = new String[n]; // name 的数组长度为 2。所以它将只有 2 个索引。从 0 和 1 开始 age = new int[n]; // 年龄数组的长度为 2. country = new String[n]; // 国家/地区数组的长度为 2 int i=0;
  • while(i
【解决方案4】:

您可以分享您的输入文件(file.txt)的样本吗?这将有助于提供最合适的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多