I am checking on ways to update my text file existing record.
For example I wanted to update the "Address" & "SSN" for Mary Rose to other information
using the following code fragment. How can I make use of scanner to complete my code?

Code Fragment

case 1: System.out.print("Address: ");
        add=scn.nextLine();
        add=scn.nextLine();
        System.out.print("SSN: ");
        ssn=scn.nextLine();
        System.out.println("Your updated information:");
        System.out.println("Address: "+add);
        System.out.println("SSN: "+ssn);

Profile.txt

Mary Rose;23;Hilton Street;987-65-4320
Solid James;25;American Street;987-65-4329

Edited 6 Years Ago by avataralien: n/a

Code Fragment

It certainly is.

ways to update my text file existing record

What do you mean by "update"? Can you explain?

How can I make use of scanner to complete my code

Scanner methods are for reading and parsing data. I don't know how you would use it to update anything.

Edited 6 Years Ago by NormR1: n/a

It certainly is.

What do you mean by "update"? Can you explain?


Scanner methods are for reading and parsing data. I don't know how you would use it to update anything.

Changing the values of address & SSN for Mary Rose.

Mary Rose;23;Hilton Avenue;123-12-1234

Use an assignment statement to change the value of variables:
add = "Hilton Avenue";
ssn = "123-12-1234";

Use an assignment statement to change the value of variables:
add = "Hilton Avenue";
ssn = "123-12-1234";

But I am getting the values from user input using scanner method.

System.out.print("Address: ");
add=scn.nextLine();
System.out.print("SSN: ");
ssn=scn.nextLine();

Yes, I see you are reading some strings into some variables.

Can you explain what you want to do with the values you are reading into the variables?

Yes, I see you are reading some strings into some variables.

Can you explain what you want to do with the values you are reading into the variables?

Pls refer to the screenshot here: http://yfrog.com/n5screen1jj

I press '1' to edit the address & SSN No. Then I wish to update the updated address SSN No to the text file.

Can you post the code you are having a problem with along with a description of your problem?
I'm having a hard time figuring out what you are asking a question about.

Can you post the code you are having a problem with along with a description of your problem?
I'm having a hard time figuring out what you are asking a question about.

This is the partial code.

System.out.println("Please press 0 to confirm your address, SSN No and delivery details.");
        System.out.println("Press 1 to change your address and contact.");
        System.out.println("Press 2 to amend your delivery details.\n");
        System.out.print("Your Choice: ");
        ch= scn.nextInt();

        //Switch 
        switch(ch){
        	case 0: done=true;
        	        saveorder(field[0],add,con,dd,tt,b);
        	        System.out.println("Goodbye.");
        	        break;

        	case 1: System.out.print("Address: ");
        	        add=scn.nextLine();
        	        System.out.print("SSN No: ");
        	        con=scn.nextLine();
        	        System.out.println("Your updated information:");
        	        System.out.println("Address: "+add);
        	        System.out.println("SSN No: "+con);
                 //Can I use Printwriter here to update my text file record?

Edited 6 Years Ago by avataralien: n/a

To update a text file, you need to read it in some or all of the file and then write out a completely new file replacing the original one. There is a way to update internal parts of it, but I think those techniques are past your current knowledge.

To update a text file, you need to read it in some or all of the file and then write out a completely new file replacing the original one. There is a way to update internal parts of it, but I think those techniques are past your current knowledge.

How about deleting row from text file & then write the updated info back to file. Is this a good method?

deleting row from text file

There is not easy way to delete a row from a text file. A text file is a sequence of characters with a newline character every so often. A line or row is the data between two of those newline characters. If the new line is EXACTLY the same size as the old one it could be possible to overwrite the old line with the new line. If they are different sizes, then either there is some of the old line left over or some of the following line is overwritten. bytes in the file are not magically moved around as needed. YOU need to write code to move the bytes as needed.

Let's move away from the code a bit and look at what you want to do.
Lets represent the contents of a file as a series of bytes which it is.
Then let's see how to replace a record in the file with a different record.
Say the file is: AAAABBBCCCCDDDDDD
and we want to replace the BBB record with bbbb
To do that we read and copy to a new file the AAAA
When we see the BBB record, we now write to the new file immediately following the AAAA
the new record bbbb.
We continue reading the old file and throwing away all the Bs until we find the end of the BBB record, now we copy the rest of the old file to the new file.
When we're done we have: AAAAbbbbCCCCDDDDDD in the new file.
Now we rename or delete the old file and rename the new file to have the same name as the old file.


That's what your code has to do(the solution): Read and copy, write new, skip over old, and copy remainder.

This article has been dead for over six months. Start a new discussion instead.