2015-11-01 95 views
-2

我有一个简单的问题。避免在使用java的文本文件中重复输出?

我有有这样记录的文本文件:

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."); 

} 
} 

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

+0

“程序崩溃”?你得到的确切例外是什么? –

+0

只是运行该程序来了解发生了什么。 –

+0

对不起,兄弟如果我的问题没有定义好,当我运行这个程序时,我每次搜索一条记录,它写道“错误的用户名或密码再试一次”....这可以显示编辑密码本身,这是什么我的意思是崩溃...程序写得不好。首先,我搜索用户名和密码,我想编辑它的密码,如果它找到了,那么它允许我改变,它不会显示此消息。但正如你所看到的,它在程序中的任何地方都会写这个消息 –

回答

1

您的呼叫写入“错误的用户名或密码”位于while循环中,因此会针对文件中的每一行不符合当前的搜索参数。要解决此问题,请将错误消息移至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... 
+0

找到记录我知道问题出在while循环但是如何把它弄到外面,这是个问题。 –

+1

@BeginnerProgrammer最简单的方法是在while循环之前设置'boolean found = false;',然后在找到搜索项时设置'found = true;'。 –

+0

请你可以为我写这段代码,Iam理解你,但我没有做到这一点。 –