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