从.txt文件中读取,只读取某些文件?

时间:2014-03-02 00:26:48

标签: java string file text

我试图加载一个包含多个字符串的.txt文件,但加载程序选择只输出某些文件(我测试了两个文件 - 尽管它们只是字符串 - 但我认为第二个档案包括首都的事实可能会导致问题?)。 第一个文本文件输出完美。我已确保放入文件大小读取器,但它没有任何区别:

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

控制台上的错误消息显示: java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1         at java.lang.String.substring(Unknown Source)         在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.

这是加载程序的代码(请原谅不好的评论):

// 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命名为&#34; Lab3Q2Loader&#34;这个班叫做&#34; Lab3Q2&#34;

1 个答案:

答案 0 :(得分:1)

如果没有看到Lab3Q2的来源很难具体,但你最有可能做String.indexOf来找到主字符串中不存在的子字符串,导致indexOf返回-1 。然后你将该索引提供给String.substring,导致你的StringIndexOutOfBoundsException。