gpt4 book ai didi

java - 从.txt文件读取,只读取某些文档?

转载 作者:太空宇宙 更新时间:2023-11-04 07:00:54 25 4
gpt4 key购买 nike

我正在尝试加载包含多个字符串的 .txt 文件,但加载器选择仅输出某些文件(我正在使用两个文件进行测试 - 尽管它们都只是字符串 - 但我认为第二个文件包含大写字母的事实可能会导致问题?)。 第一个文本文件输出完美。我已经确保放置了文件大小读取器,但这没有什么区别:

    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.

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

// 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

加载器名为“Lab3Q2Loader”,类名为“Lab3Q2”

最佳答案

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

关于java - 从.txt文件读取,只读取某些文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22122317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com