【问题标题】:Reading from .txt file, only reading certain documents?从 .txt 文件中读取,只读取某些文件?
【发布时间】:2014-03-02 00:26:48
【问题描述】:

我正在尝试加载一个包含多个字符串的 .txt 文件,但加载程序选择只输出某些文件(我正在测试两个 - 尽管它们都只是字符串 - 但我认为第二个filee 包含大写字母可能会导致问题?)。 第一个文本文件输出完美。我已经确定了文件大小阅读器,但它没有任何区别:

    this.inputString = new Lab3Q2 [size]; // create arrays
    this.outputString = new String [size];

控制台上的错误信息如下: java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1 在 java.lang.String.substring(未知来源) 在 Lab3Q2.processStrings(Lab3Q2.java:80) 在 Lab3Q2Loader.loadFile(Lab3Q2Loader.java:60) 在 Lab3Q2Loader.main(Lab3Q2Loader.java:77)

第一个 .txt 文件内容为:

2
how are you today
i am doing well 

第二个 .txt 文件内容如下:

5
The king looked; well
The cook worked long hours in the darkened kitchen
Well, I think this king is crazied 88
All 2567 kings liked the king of ID.
The cooled computer room at DSSS is used for computers.

这是加载程序的代码(请原谅可怜的cmets):

// The "IDSpeakLoader" class.
import java.io.*;
import javax.swing.*;

public class Lab3Q2Loader
{

    // private data for input and output strings
    private Lab3Q2 inputString[];
    private String outputString[];


    //Creates a new instance of <code>IDSpeakLoader</code>.

    public Lab3Q2Loader ()
    {
        this.inputString = null;
        this.outputString = null;
    }


    //method to get the private output data
    public String[] getOutputString ()
    {
        return this.outputString;
    }


    //method to load the file and send it to Lab3Q2 fo processing
    public void loadFile () throws IOException
    {

        FileReader fr = new FileReader ("String.txt"); // file must be with class file in
        BufferedReader inputFile = new BufferedReader (fr);

        int size = Integer.parseInt (inputFile.readLine ()); // read size

        this.inputString = new Lab3Q2 [size]; // create arrays
        this.outputString = new String [size];

        for (int i = 0 ; i < inputString.length ; i++)
        {

            String string1;

            string1 = inputFile.readLine ();

            // Create an array of Lab3Q2 objects
            this.inputString [i] = new Lab3Q2 ();

            // call methods in each of those objects to save and process the strings
            this.inputString [i].setInputStrings (string1);
            this.outputString [i] = this.inputString [i].processStrings ().toUpperCase (); //process and save

        }

        inputFile.close (); // close the file
    }


    //@param args the command line arguments

    public static void main (String[] args) throws IOException
    {

        // create an object of the loader
        Lab3Q2Loader strL = new Lab3Q2Loader ();

        // load the file
        strL.loadFile ();

        String output = ""; // string to hold ouput information

        // get the information from loader
        for (int i = 0 ; i < strL.getOutputString ().length ; i++)
        {
            output = output + strL.getOutputString () [i] + "\n"; // add info for each element to
        } // output + a new line

        JTextArea text = new JTextArea (); // set a text area to display
        text.setText (output);

        //display the information
        JOptionPane.showMessageDialog (null, text);

    } // main method
} // class

Loader 名为“Lab3Q2Loader”,类名为“Lab3Q2”

【问题讨论】:

  • Lab3Q2.processStrings(Lab3Q2.java:80) 或只是 Lab3Q2.processStrings() 的样子。

标签: java string file text


【解决方案1】:

如果没有看到 Lab3Q2 的来源,很难具体说明,但您很可能会执行 String.indexOf 来查找主字符串中不存在的子字符串,从而导致 indexOf 返回 -1。然后您将该索引输入String.substring,导致您的 StringIndexOutOfBoundsException。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多