1

该程序的整个想法是逐行读取文件并将每个单词保存到数组token[]中。我正在尝试使用 for 循环将数组 token[] 中的元素打印到控制台上。但它说变量令牌尚未初始化。

import java.io.*;

public class ReadFile{
    public static void main(String args[]){
        String[] token;
        int i;

            try{
                // Open and read the file
                FileInputStream fstream = new FileInputStream("a.txt");
                BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
                String strLine;
                //Read file line by line and storing data in the form of tokens
                while((strLine = br.readLine()) != null){
                    token = strLine.split(" ");
                }   
                in.close();//Close the input stream
            }
            catch (Exception e){//Catch exception if any
                System.err.println("Error: " + e.getMessage());
            }

                   // Why can't I do this printing part?
            for(i=0;i<=token.length;i++){
                System.out.println(token[i]);
            }``
        }// close main()
}// close Class
4

4 回答 4

2

当你在一个方法中时,比如 main,变量声明没有被初始化,你必须自己为它们提供一个初始值。

例如:

字符串 [] 数组 = 新字符串 [0];

甚至

字符串 [] 数组 = 空;

于 2013-03-27T18:09:32.450 回答
1

以下代码:

    public class ReadFile{
    public static void main(String args[]){
    String[] token;
    int i;
    try{
        // Open and read the file
        FileInputStream fstream = new FileInputStream("a.txt");
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        //Read file line by line and storing data in the form of tokens
        while((strLine = br.readLine()) != null){
            token = strLine.split(" ");
        }   
        in.close();//Close the input stream
    }
    catch (Exception e){//Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }

应该改为这个:

    public class ReadFile{
    public static void main(String args[]){
    String[] token = null;//initialize token with null
    int i;   
try{
        // Open and read the file
        FileInputStream fstream = new FileInputStream("a.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(fStream));//Don't use DataInputStream as this class is good for reading primitives, length-prefixed strings e.t.c 
        StringBuilder sBuilder = new StringBuilder();
        String strLine;
        //Read file line by line and storing data in the form of tokens
        while((strLine = br.readLine()) != null){
            //token = strLine.split(" ");
            sBuilder.append(strLine);
        }   
        token = (sBuilder.toString()).split(" ");//Write this line here.
        in.close();//Close the input stream
    }
    catch (Exception e){//Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }

还要更改 for 循环结构:

for(i=0;i<=token.length;i++){//This will give ArrayOutOfBoundException
        System.out.println(token[i]);
    }

if (token != null)
for(i=0; i<token.length; i++){// condition should be checked for i < token.length
        System.out.println(token[i]);
    }

注意:正如@Peter Lawrey 所建议的,DataInputStream应该特别用于阅读原语。

于 2013-03-27T18:11:10.293 回答
1

这是一个范围问题。

范围内main()token已定义但未初始化。如果要token在此范围级别使用,则必须对其进行初始化。

在 try...catch 块的范围内,token被初始化。奇怪的是,它的值只对最后一行很重要,因为每次都会split()创建一个新的String[],并且您将它重新分配给token.

于 2013-03-27T18:13:54.993 回答
0

检查此代码,您将了解问题所在。

import java.io.*;
import java.util.*;
public class ReadFile{
public static void main(String args[]){
ArrayList<String[]> tokenlist = new ArrayList<>();
int i;

try{
    // Open and read the file
    FileInputStream fstream = new FileInputStream("health.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
    String strLine;
    //Read file line by line and storing data in the form of tokens
    while((strLine = br.readLine()) != null){
         String[] token = strLine.split(" ");
        tokenlist.add(token);
    }   
    //in.close();//Close the input stream
}
catch (Exception e){//Catch exception if any
    System.err.println("Error: " + e.getMessage());
}

       // Why can't I do this printing part?
for(int count=0; count<tokenlist.size();count++){
    String[] token = tokenlist.get(count);
    for(i=0;i<token.length;i++){
        System.out.println(token[i]);
    }
 }

 }// close main()
 }
于 2013-03-27T18:08:56.937 回答