Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:如何在将数据保存到文本文件之前检查字符串?_Java - Fatal编程技术网

Java:如何在将数据保存到文本文件之前检查字符串?

Java:如何在将数据保存到文本文件之前检查字符串?,java,Java,我正在为java类创建一个员工时钟。在接受“打卡时间”之前,我的程序的这一部分需要检查是否有“打卡时间” 当我只使用if循环时,该部分似乎有效。当我添加else时,它忽略if,只执行else 我能得到一些关于更好方法的反馈吗 public static void punchIn() throws IOException { Scanner sc = new Scanner(System.in); System.out.print("Enter Date and time (for

我正在为java类创建一个员工时钟。在接受“打卡时间”之前,我的程序的这一部分需要检查是否有“打卡时间”

当我只使用
if
循环时,该部分似乎有效。当我添加
else
时,它忽略
if
,只执行
else

我能得到一些关于更好方法的反馈吗

public static void punchIn() throws IOException {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter Date and time (format MM/dd/yyyy HH:mm:ss): ");
    String timeentry = sc.nextLine();

    System.out.print("Enter the employee ID number: ");
    String idnumber = sc.nextLine() + " ";

    String inorout = "in";

    System.out.println("The Punch-in date / time is: " + timeentry);
    System.out.println("The employee ID number is: " + idnumber);
    System.out.println("The employee is punched-" + inorout);

    PunchinPunchoutData product = new PunchinPunchoutData();
    product.setTimeentry(timeentry);
    product.setIdnumber(idnumber);
    product.setInorout(inorout);

    productDAO.punchIn(product);

    System.out.println();
    System.out.print("Press enter to continue ");
    sc.nextLine();
}


public static void punchOut() throws FileNotFoundException, IOException {

    Scanner sc = new Scanner(System.in);
    System.out.print("Enter Date and time (format MM/dd/yyyy HH:mm:ss): ");
    String timeentry = sc.nextLine();

    br = new BufferedReader(new FileReader("timeclock1.txt"));

    String line = "";

    System.out.print("Enter an employee ID number: ");
    String idnumber = sc.next() + " ";//read the choice
    sc.nextLine();// discard any other data entered on the line

    while ((line = br.readLine()) != null) {

        if (line.contains(idnumber + " ") && line.endsWith("in")) {
            break;
        }

        else  { 
            System.out.println("There is no punch-in record for ID number: 
            " + idnumber);
            System.out.println("A punch-in entry must be saved first");
            punchIn();
            break;
        }
    }

    String inorout = "out";

    System.out.println("The Punch-out date / time is: " + timeentry);
    System.out.println("The employee ID number is: " + idnumber);
    System.out.println("The employee is punched-" + inorout + ".");

    PunchinPunchoutData product = new PunchinPunchoutData();
    product.setTimeentry(timeentry);
    product.setIdnumber(idnumber);
    product.setInorout(inorout);

    productDAO.punchOut(product);

    System.out.println();
    System.out.print("Press enter to continue ");
    sc.nextLine();

}

您似乎在逐行读取文件,以检查员工是否有“打卡”记录。在代码中,当您有一行不属于该员工的代码时,您正在调用
punchIn
,这很可能会在每次调用
punchOut
时添加一个“打孔”。您应该在整个文件上循环,并且仅当文件中没有任何行包含记录时才调用
punchIn

boolean foundPunchIn = false;

while ((line = br.readLine()) != null) {
    if (line.contains(idnumber + " ") && line.endsWith("in")) {
       foundPunchIn = true;
       break;
    }   
}

if(!foundPunchIn) {
    System.out.println("There is no punch-in record for ID number: " + idnumber);
    System.out.println("A punch-in entry must be saved first");
    punchIn();
}

String inorout = "out";
...

谢谢你的回复。我试试看。成功。代码中肯定还有另一个问题,但在我进行了一些故障排除之后,您的解决方案很简单,而且有效。谢谢