The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Editing a Line!
Discuss Editing a Line! in the Java Help forum on Dev Shed. Editing a Line! Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 15th, 2012, 04:12 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 11
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
To
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;}}
|

September 15th, 2012, 07:54 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
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.
|

September 15th, 2012, 09:48 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 11
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
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){}}}
|

September 15th, 2012, 10:06 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
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?
|

September 15th, 2012, 10:33 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 11
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!
|

September 15th, 2012, 11:03 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Here' some pseudo code:
PHP Code:
begin loop
read a line
Is this the line to change?
Yes, change line, write line to output & exit loop
No, copy line to output
end loop
copy rest of file to output
|

September 15th, 2012, 11:33 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 11
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?
Yes, change line, write 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?
|

September 16th, 2012, 07:10 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Yes you could rename it as long as the original filename doesn't exist anymore.
|

September 18th, 2012, 06:39 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 11
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=(
|

September 18th, 2012, 06:56 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
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.
|

September 18th, 2012, 08:15 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 11
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=(
|

September 18th, 2012, 09:07 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|