Thread: Editing a Line!

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Find/Replace a line in a Text File!


    I'm kinda lost now... How can i edit the password line?

    Password.txt sample
    Account Password F.Name L.Name

    From
    jvt619 12345 Bert Java
    To
    jvt619 help Bert Java
    Code:
    package assign2;
    
    import java.io.*;
    import java.util.*;
    
    public class RegAssignment2
    {
        static Scanner sc = new Scanner(System.in);   
        public static void main(String args[]) throws FileNotFoundException, IOException, NullPointerException
        {
               String name="", answer, password="", newName, newPassword, newFirstname, newLastname, lines="---", menuAnswers="---", linesOld="---",editpass="---";            
               answer=login();
    		if(answer.equals("b")) {
    			name=getName();
    			lines=findUser(name);}
                    
    		if(answer.equals("a")) {
    			newName=getName();
    			newPassword=getPassword();
    			newFirstname=getFirst();
                            newLastname=getLast();
               
                try {
                     BufferedWriter bw = new BufferedWriter(new FileWriter("Password.txt", true)); 
                     bw.newLine();
                     bw.write(newName + " " + newPassword + " " + newFirstname+ " "+newLastname); 
                     bw.close();} 
                catch 
                     (IOException e){}
                
                   System.out.println("\n                    Thank you for Registering!!");} 
                    
                      if(lines!="---") {
                              String info[]=lines.split(" ");
                              linesOld=(info[1]);
                              password=getPassword();
                           if(password.equals(info[1])) 
                                  menuAnswers=menu(info[1]);
                                else {
                                    System.out.println("Wrong password! (Last Try)");
                                    password=getPassword();
                                       if(password.equals(info[1])){
                                           menuAnswers=menu(info[1]);}
                                           else{
                                               System.out.println("EXITING...");}}
                                    
    		if(menuAnswers.equals("a")){
                    }                
                    
    		if(menuAnswers.equals("b")){
                            }
    
                    else {
                    System.exit(2);
                }
    		
    		if(answer!="a" && answer!="b") {
                    System.exit(2);}}}
        
            
    	public static String login(){
    		System.out.print("a) Register \nb) Login\n\n--> ");
    		return sc.nextLine();}
            
    	public static String getName(){
    		System.out.print("\nEnter your Account Name     -->  ");
    		return sc.nextLine();}
            
    	public static String getPassword(){
    		System.out.print("\nEnter your Account Password -->  ");
    		return sc.nextLine();}
            
    	public static String getFirst() {
    		System.out.print("\nEnter your First Name       -->  ");
    		return sc.nextLine();}
            
            public static String getLast() {
    		System.out.print("\nEnter your Last Name        -->  ");
    		return sc.nextLine();}
            
            public static String getnewPassword() {
                    System.out.print("\nEnter your New Account Password -->  ");
                    return sc.nextLine();}
            
            public static String findUser(String name)
            {
                try{
                    Scanner scan = new Scanner(new File("Password.txt"));
                    
                    while(scan.hasNextLine()){
                            
                    String lines=scan.nextLine();
                    String info=lines.split(" ")[0];
                    if(info.equals(name)){
                            return lines;}}}
                
                    catch (FileNotFoundException eq)
                    {System.out.println("\nPlease Register First");System.exit(2);}
                
                    System.out.println("Incorrect Account(Try Again)");
                    return "Incorrect Account(Try Again)";}        
            
            public static String menu(String info){
    		System.out.println("\n                                  Menu");
    		System.out.println("a) Change Password");
    		System.out.println("b) Delete Account");
    		System.out.print("\n--> ");
    		String menuAnswers=sc.nextLine();
    		return menuAnswers;}}
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    If the line is held in a String, the String class has methods for creating a new String with parts of the old String and parts of a new String. If there are spaces between the parts of the String and the password is the second token, the split() method might be good. Break the String up into tokens and extract the parts you want from the array to build the new String.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0
    so ummm here is something that I did but the problem is like if in a text file it has 3 accounts and then someone changed the password of an account the other accounts will be deleted except for the account that has been password changed.

    if I changed the password of jvt195 to 213

    Before
    jvt619 123 Bert Treb
    jvt961 1234 Bert Treb
    jvt195 12345 Bert Bert
    After
    jvt195 213 Bert Treb
    it will delete other accounts except for the account that has been password changed. how to fix this problem?

    For Editing the Password:
    Code:
    		if(menuAnswers.equals("a")){
                        String commanda = confirm();
                        if(commanda.equals("y")){
                            editPass=getnewPassword();
                            String findReplace[]=lines.split(" ");
                            findReplace[1]=String.valueOf(editPass);
                            String lines2=(findReplace[0]+ " " +findReplace[1]+ " " +findReplace[2]+ " " +findReplace[3]);
                    
                            try {
                                BufferedWriter out = new BufferedWriter(new FileWriter("Password.txt"));
                                out.newLine();
                                out.write(lines2);
                                System.out.println("\nYour password has been changed!");
                                out.close();
                            } catch (IOException e){}}}
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    If the text file has multiple lines and you only want to change the contents of one line,
    the code must copy all the lines before the the line to be change, then change the contents of the line to be changed and then copy all of the line following the changed line. Is your code copying the lines before and those following the changed line?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by NormR
    If the text file has multiple lines and you only want to change the contents of one line,
    the code must copy all the lines before the the line to be change, then change the contents of the line to be changed and then copy all of the line following the changed line. Is your code copying the lines before and those following the changed line?
    no, i dont have.
    can you show some code examples!
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Here' some pseudo code:
    PHP Code:
    begin loop
     read a line
     Is this the line to change
    ?
       
    Yeschange linewrite line to output & exit loop
       No
    copy line to output
    end loop
    copy rest of file to output 
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by NormR
    Here' some pseudo code:
    PHP Code:
    begin loop
     read a line
     Is this the line to change
    ?
       
    Yeschange linewrite line to output & exit loop
       No
    copy line to output
    end loop
    copy rest of file to output 
    is it also possible to have a temptext file and then just rename it to an original file?
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Yes you could rename it as long as the original filename doesn't exist anymore.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0
    can you teach me on how to make a tempfile that copies all the lines of the orignal file?
    cause i dont have any idea and i cant find anything on google=(
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    The code you posted has most of what you need. It creates a file, reads lines from a file, and writes a line to a file. The File class has a method to create a temp file.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0
    yeah but i don't know how to copy all the needed info on the tempfile=(
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    how to copy all the needed info
    Are you asking about how to read a line from one file and write that same line to another file?
    The code you posted is able to read lines and is able to write lines, put the two pieces of code together.

IMN logo majestic logo threadwatch logo seochat tools logo