Java 使用FileReader加载textfile后,在.setText()方法中不插入任何内容

Java 使用FileReader加载textfile后,在.setText()方法中不插入任何内容,java,textfield,bufferedreader,filereader,illegalargumentexception,Java,Textfield,Bufferedreader,Filereader,Illegalargumentexception,我正在为学校完成一个java项目,但我在某一点上被卡住了 任务是制作一个任务计划器,它应该能够保存任务、添加/编辑或删除,并将信息保存在计算机目录中的文本文件中 这样,每次在开始打开应用程序时,保存的任务信息都会加载到GUI中 我面临的问题是,我必须在GUI的某些文本字段中插入信息(加载时)。。。然而,有时并非所有文本字段都应填写-换句话说,它不是强制性的- 这就是为什么有时候.setText()方法会有一个空参数,而代码无法工作 我试图通过每次检测到空参数时插入一个“”(空格)来解决这个问题,

我正在为学校完成一个java项目,但我在某一点上被卡住了

任务是制作一个任务计划器,它应该能够保存任务、添加/编辑或删除,并将信息保存在计算机目录中的文本文件中

这样,每次在开始打开应用程序时,保存的任务信息都会加载到GUI中

我面临的问题是,我必须在GUI的某些文本字段中插入信息(加载时)。。。然而,有时并非所有文本字段都应填写-换句话说,它不是强制性的-

这就是为什么有时候.setText()方法会有一个空参数,而代码无法工作

我试图通过每次检测到空参数时插入一个“”(空格)来解决这个问题,但我发现这个解决方案很难看,因为它适用于当单击带有新的空文本字段的新GUI窗口时,会自动出现一个空格,用户应该在写入内容之前按backspace

有更好的解决办法吗

下面是加载所有任务的代码

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

 public class ReadTask extends Planner {

    public static void read(){

        String[] myArray = new String[8];
        String line = "";

        TaskWindowNew currentTask = null;

        try 
        {
            BufferedReader br = new BufferedReader(new FileReader("PlannerText.txt"));          

            while (( line = br.readLine()) !=null) 
            {
                myArray = line.split("\\|");

                for(int i=0;i<myArray.length;i++)
                {
                    if( myArray[i].isEmpty())
                    {
                        myArray[i] = "yoooo";
                    }
                }

                System.out.println(myArray.toString());

                try{
                    currentTask = new TaskWindowNew();
                    currentTask.popupFrame.setVisible(false);

                    // IMPORTANT = If one or more field(s) is left empty the .doClick function will not work
                    // we need to find a solution for this.
                    currentTask.titleField.setText(myArray[1]);
                    currentTask.minuteField.setText(myArray[2]);
                    currentTask.hourField.setText(myArray[3]);
                    currentTask.daysMonthField.setText(myArray[4]);
                    currentTask.MonthField.setText(myArray[5]);
                    currentTask.daysWeekField.setText(myArray[6]);
                    currentTask.specialSymbolField.setText(myArray[7]);

                    //this emulates the action of the OK-button in the new task window !
                    currentTask.buttonOk.doClick();

                 } catch (ArrayIndexOutOfBoundsException e)
                 {

                 }
            }

            br.close(); 

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        updateScrollPanel();


    }

}
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
公共类ReadTask扩展计划器{
公共静态无效读取(){
字符串[]myArray=新字符串[8];
字符串行=”;
TaskWindowNew currentTask=null;
尝试
{
BufferedReader br=新的BufferedReader(新文件阅读器(“PlannerText.txt”);
而((line=br.readLine())!=null)
{
myArray=line.split(“\\\\”);

对于(int i=0;i您所做的工作与文件内容密切相关。这意味着在数组索引1处会有一个标题字段,在索引3处会有一个小时字段。但如果用户未输入任何值,则索引会更改,在某些情况下,您可能会获得ArrayIndexOutOfBoundsException

因此,当用户未输入非必填字段时,您不能期望下次在UI上显示它们

您应该做的是将键值对存储在文件中。这将使您能够检查值用于哪个字段,而不是依赖数组索引

我的意思是这样的:

标题:Mr

时间:四点

特殊符号:#

然后,当您预填充它时,您应该获得每一行,拆分键和值,如果值不为空,则将其设置在TaskWindowNew的相应变量(您将从键中了解到)中


希望这能有所帮助。

谢谢大家的回复/评论

我通过擦除myArray并直接将.split结果分配给一个数组解决了这个问题。 我不知道为什么会这样

对于那些对我的SaveTask类感兴趣的人,我会这样保存数据

outputStream.println(order + "|"+ title +"|"+ minutes +"|"+ hours +"|"+ daysPerMonth +"|"+ months +"|"+ daysPerWeek +"|"+ specialSymbol +"|"+"end");
order++;
但是下面的代码如果结尾没有“end”-标志将无法工作…否则它不会使数组的长度变为8个插槽,它只会使数组的长度变为填充的最后一个文本字段的长度..(这给了我ArrayIndexOutOfBoundsException问题)

所以我就是这样修好的

我认为@skandigraun很困惑,因为StringTokenizer实际上做的恰恰相反(我相信)

请看这里:

以及有效的代码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

 public class ReadTask extends Planner {

    public static void read()
    {

        //try-catch for IOExceptions
        try 
        {
            //Make the inputStream
            BufferedReader br = new BufferedReader(new FileReader("PlannerText.txt"));

            //The String containing each line
            String line = null;

            //For each line do the following
            while ( (line = br.readLine()) !=null) 
            {               
                //Make a new Task-window
                TaskWindowNew currentTask = new TaskWindowNew();

                //Set visibility to false
                currentTask.popupFrame.setVisible(false);

                //An array containing each string seperated by the delimiter
                String[] currTask = line.split("\\|");

                System.out.println(currTask.length);

                System.out.println(currTask[0]);

                //Fill each field with the corresponding String
                currentTask.titleField.setText(currTask[1]);
                currentTask.minuteField.setText(currTask[2]);
                currentTask.hourField.setText(currTask[3]);
                currentTask.daysMonthField.setText(currTask[4]);
                currentTask.MonthField.setText(currTask[5]);
                currentTask.daysWeekField.setText(currTask[6]);
                currentTask.specialSymbolField.setText(currTask[7]);

                //this emulates the action of the OK-button in the new task window !
                currentTask.buttonOk.doClick();
            }

            //Close the stream
            br.close(); 

        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }

        updateScrollPanel();            
    }
}

如果第二个元素无效,则永远不要在try-catch-finallyImagine中创建/设置/修改GUI或更新GUI。之后的任何代码(包括doClick方法)都不会执行。如果重要,则应将doClick添加到最后一个理论块中,在哪里插入
“”
?我似乎在您的代码中找不到它。您可以只执行
a.setText(“”)
,这样您就没有空间了。它可以通过多种方式实现。我个人认为这些方式最简单:1-不要将字符串存储在数组中,而是存储在stringtokenizer中,并将一些未使用的字符设置为分隔符。这样,当您返回两个分隔符后面的部分时,它会给出一个空字符串。(或者如果它是空的,您可以设置null)2-使用您想要的数据创建一个可序列化的类,它可以很容易地保存和加载,并且您可以很容易地保存null值also@mKorbel当前位置我不太明白你的意思,为什么我永远不应该这样做?嗨,是的,这是一个好主意!我会尝试一下,并说它是否有效!