Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 15th, 2012, 04:12 AM
jvt619 jvt619 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 11 jvt619 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 56 m 28 sec
Reputation 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
Quote:
jvt619 12345 Bert Java

To
Quote:
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;}}

Reply With Quote
  #2  
Old September 15th, 2012, 07:54 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
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.

Reply With Quote
  #3  
Old September 15th, 2012, 09:48 AM
jvt619 jvt619 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 11 jvt619 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 56 m 28 sec
Reputation 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
Quote:
jvt619 123 Bert Treb
jvt961 1234 Bert Treb
jvt195 12345 Bert Bert

After
Quote:
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){}}}

Reply With Quote
  #4  
Old September 15th, 2012, 10:06 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
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?

Reply With Quote
  #5  
Old September 15th, 2012, 10:33 AM
jvt619 jvt619 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 11 jvt619 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 56 m 28 sec
Reputation Power: 0
Quote:
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!

Reply With Quote
  #6  
Old September 15th, 2012, 11:03 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
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 

Reply With Quote
  #7  
Old September 15th, 2012, 11:33 PM
jvt619 jvt619 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 11 jvt619 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 56 m 28 sec
Reputation Power: 0
Quote:
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?

Reply With Quote
  #8  
Old September 16th, 2012, 07:10 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Yes you could rename it as long as the original filename doesn't exist anymore.

Reply With Quote
  #9  
Old September 18th, 2012, 06:39 AM
jvt619 jvt619 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 11 jvt619 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 56 m 28 sec
Reputation 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=(

Reply With Quote
  #10  
Old September 18th, 2012, 06:56 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
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.

Reply With Quote
  #11  
Old September 18th, 2012, 08:15 AM
jvt619 jvt619 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 11 jvt619 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 56 m 28 sec
Reputation Power: 0
yeah but i don't know how to copy all the needed info on the tempfile=(

Reply With Quote
  #12  
Old September 18th, 2012, 09:07 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Editing a Line!

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap