在Java中尝试将文件中的字符串转换为数组

在Java中尝试将文件中的字符串转换为数组,java,arrays,string,exception,Java,Arrays,String,Exception,很抱歉,如果这是一个非常明显的问题,但我已经尝试了这么多的代码在这一点上,似乎没有任何工作。我有一个扫描过的文件,然后保存在字符串中。现在,我需要将字符串中的单词转换为数组,稍后我将拆分为一个JTextfield。这是我到目前为止的代码,当我试图打印数组的内容时,我得到了一个“没有这样的元素异常”。 也为所有注释掉的代码道歉,但它可能会显示我已经尝试了什么,什么对我有效/无效 我之前也扫描过它,并立即将其转换为ArrayList,但这不适合项目的其余部分,因此我不得不将其改为string pub

很抱歉,如果这是一个非常明显的问题,但我已经尝试了这么多的代码在这一点上,似乎没有任何工作。我有一个扫描过的文件,然后保存在字符串中。现在,我需要将字符串中的单词转换为数组,稍后我将拆分为一个JTextfield。这是我到目前为止的代码,当我试图打印数组的内容时,我得到了一个“没有这样的元素异常”。 也为所有注释掉的代码道歉,但它可能会显示我已经尝试了什么,什么对我有效/无效

我之前也扫描过它,并立即将其转换为ArrayList,但这不适合项目的其余部分,因此我不得不将其改为string

public void classesIn() {

    try {

        Scanner input = new Scanner(file);
        // input.useDelimiter("[,\\s]");

        String theTimetable = null;

        while ((theTimetable = input.next()) != null) {
            // System.out.println(theTimetable);

            dasTimetable = theTimetable;
            // System.out.println(dasTimetable);

        }

        // System.out.println(theTimetable[2]);
        // theTimetable.add(input.next());

        input.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

public void stringtoArray(String dt, String[] tt) {

    tt = timeTables;

    // String[]timeTables ;

    dt = dasTimetable;

    timeTables = dasTimetable.split("\\s+");
    for (int i = 0; i < timeTables.length; i++) {
        timeTables[i] = timeTables[i].replaceAll("^\\w]", "");
        System.out.println(timeTable[i]);

        // System.out.println(Arrays.toString(timeTables));
        // System.out.println(words);
        // System.out.println(dasTimetable);

    }
}
public void classesIn(){
试一试{
扫描仪输入=新扫描仪(文件);
//input.useDelimiter(“[,\\s]”);
字符串theTimetable=null;
while((theTimetable=input.next())!=null){
//System.out.println(可计量);
dasTimetable=计量表;
//System.out.println(数据可估计);
}
//System.out.println(度量表[2]);
//timetable.add(input.next());
input.close();
}捕获(例外情况除外){
例如printStackTrace();
}
}
公共无效字符串数组(字符串dt,字符串[]tt){
tt=时间表;
//字符串[]时间表;
dt=可估计的数据;
时间表=dasTimetable.split(\\s+);
对于(int i=0;i
问题在于:

while ((theTimetable = input.next()) !=null) {
    //System.out.println(theTimetable);

    dasTimetable = theTimetable;
    //System.out.println(dasTimetable);

}
您正在用input.next()的内容覆盖字符串。在该循环终止后,您将只在dasTimetable中保留input.next()的列表值


我建议您将每次迭代的值附加到ArrayList中,然后将其所有内容连接到一个字符串中。

如果要将文件中的单词转换为数组,最简单的方法是将单词添加到列表中,然后在完成后将列表转换为数组

下面是将文件转换为单词数组的代码段:

    Scanner input = new Scanner(file);
    input.useDelimiter("[,\\s]");
    List<String> words = new ArrayList<String>();
    while (input.hasNext()) {
        String s = input.next();
        if (!s.isEmpty()) {
            //System.out.println(s);
            words.add(s);
        }
    }
    input.close();
    // now convert list to an array     
    String[] table = words.toArray(new String[words.size()]);

您正在尝试将字符串拆分为单词或字符吗?这些方法是如何关联的?您能告诉我们发生错误的行吗?顺便说一句,input.next()
已经将输入分解为单词。您应该提供一个运行的小示例,您显示的代码中有许多未定义的变量,这将导致许多错误。提高代码的可读性将帮助其他人了解发生了什么
StringBuilder sb = StringBuilder();
while (input.hasNext()) {
        String s = input.next();
        if (!s.isEmpty()) {
            if (sb.length() != 0) sb.append(' ');
            sb.append(s);
        }
}
// create concatenated string with all words separated by whitespace
String words = sb.toString();