1

I have a problem and hope to find a solution.

now i have created a simple program to change password of user account using text files in java.

now i should enter username of the account then change password of that account but there it shows me an error.

here is my code:

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("Enter account username you want to edit password?"); Scanner sc2 = new Scanner(System.in); String username = sc2.next(); while(sc.hasNextLine()) { String currentLine= sc.nextLine(); String[] tokens = currentLine.split(" "); if(Objects.equals(Integer.valueOf(tokens[0]), username) && checked) { sc2.nextLine(); System.out.println("New Password:"); newPassword= sc2.nextLine(); currentLine = tokens[0]+" "+newPassword; checked = false; } writer.write(currentLine + System.getProperty("line.separator")); } writer.close(); sc.close(); f.delete(); boolean successful = tempFile.renameTo(f); } } 

the error shows to me:

Exception in thread "main" java.lang.NumberFormatException: For input string: "HAMADA" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.valueOf(Integer.java:766) at Test6.main(Test6.java:25) 

the format of my text file is like that:

HAMADA 115599 JOHNY 4477100 
0

1 Answer 1

2

Change Integer.valueOf(tokens[0]) on line 25 to just tokens[0].

In your code, you try to get the integer value of the username, when you should be getting its String representation. You do not need the Integer.valueOf(). (The error is thrown because you are trying to get the Integer representation of a non-integer type.)

On a side note, you should never have password-storing text files, especially when the passwords and the files are both unencrypted. Use a database instead.

Sign up to request clarification or add additional context in comments.

1 Comment

@BeginnerProgrammer No problem. I'm glad it helped!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.