使用java避免文本文件中的重复输出?

使用java避免文本文件中的重复输出?,java,file-io,text-files,Java,File Io,Text Files,我有一个简单的问题 我有一个文本文件,其中包含如下记录: HAMADA 115599 KARIM 224466 BOSY 47896512 该文件实际上定义了用户帐户的用户名和密码 现在我编写了一个简单的代码来编辑特定用户的密码 这是我的密码: import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Ob

我有一个简单的问题

我有一个文本文件,其中包含如下记录:

HAMADA 115599
KARIM 224466
BOSY 47896512
该文件实际上定义了用户帐户的用户名和密码

现在我编写了一个简单的代码来编辑特定用户的密码

这是我的密码:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Objects;
import java.util.Scanner;

public class Test6 {
public static void main(String[] args)throws IOException {

String newPassword=null;
boolean checked = true;

File f= new File("C:\\Users\\فاطمة\\Downloads\\accounts.txt");// path to your file
File tempFile = new File("C:\\Users\\فاطمة\\Downloads\\accounts2.txt"); // create a temp file in same path
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
Scanner sc = new Scanner(f);
System.out.println("Change account password !!");
System.out.println("Validate your account please !!");
System.out.printf("Username: ");
Scanner sc2 = new Scanner(System.in);
String username = sc2.next().toUpperCase();
System.out.printf("Old Password: ");
String password = sc2.next();
while(sc.hasNextLine())
{
String currentLine= sc.nextLine();
String[] tokens = currentLine.split(" ");
if(Objects.equals(tokens[0], username) && Objects.equals(tokens[1], password) && checked)
{
     sc2.nextLine();                          
     System.out.printf("New Password: ");
     newPassword= sc2.nextLine();
     if(newPassword.length() >= 6)
     {
     currentLine = tokens[0]+" "+newPassword;
     checked = false;
     }
     else
         System.out.println("Short Password, Password must be at least 6 characters.");
 }
    else{System.out.println("Wrong username or password .. try again !!");}
 writer.write(currentLine + System.getProperty("line.separator"));

 }
 writer.close(); 
 sc.close();
 f.delete();
 boolean successful = tempFile.renameTo(f);
 if(successful == true)
 System.out.println("Your password has been changed successfully.");
 else
 System.out.println("Error occurred during changing password, please try again.");

 }
 }
问题是:如果我在上面提到的文件中有很多记录,那么现在它会在每个记录停止时写入消息“错误的用户名或密码”。我只想让程序只在输入的记录上说这条消息,而该记录在文件中找不到,然后程序停止。如果在文件中找到了该记录,则允许用户更改该记录的密码

当循环时,您对写入“错误用户名或密码”的调用在
循环中,因此会对文件中与当前搜索参数不匹配的每一行调用该记录。要解决此问题,请将错误消息移到
while
循环之外,以便只能调用一次

在适当缩进的代码中很容易发现这些错误,因为这样一来,循环中的内容和不存在的内容就变得很明显了。通常,使用适当的格式可以简化调试

编辑:例如,您可以执行以下操作:

// Open the file...
boolean found = false;
while(sc.hasNextLine())
{
    // Read the line...
    if (Objects.equals(tokens[0], username) && Objects.equals(tokens[1], password) && checked)
    {
        // Do stuff...
        found = true;
        break; // Done looking
    }
}
if (!found)
{
    System.out.println("Wrong username or password .. try again !!");
}
// Close the file...

“程序崩溃”?你得到的确切异常是什么?只需运行程序来了解发生了什么。很抱歉,兄弟,如果我的问题没有定义好,当我每次搜索记录时运行此程序时,它会写入“错误的用户名或密码,请重试”。。。。这可以在编辑密码时显示出来,这就是我所说的崩溃。。。这个程序写得不好。首先,我搜索用户名和密码,我想编辑它的密码,如果它找到了,然后它允许我更改,它不会显示此消息。但正如您所看到的,它在程序中的任何地方都会写入此消息。那么,当您在调试器中逐步执行代码时,您学到了什么?编辑后,您可以看到我的问题,我将再次解释此问题。只有在找不到用户帐户时,但当我输入存在的用户名和密码时,才会显示“错误的用户名或密码”消息。。。它搜索文件中的每一行,并为未找到记录的每一行写入消息。inI知道问题在while循环中,但如何将其取出,这是一个问题。@初学者程序员最简单的方法是设置
boolean found=false在while循环之前,然后设置
found=true当找到搜索词时。请你能帮我写这段代码吗?我理解你,但我没能做到。@初学者程序员我已经给了你一个大纲,但请记住,StackOverlfow不是一个代码编写服务。我们将帮助您找到解决方案,但您应该自己实现它们。是的,我知道StackOverflow,我自己编写代码,我只是被困在这个问题上,您给了我解决方案:)