
11-12-2009, 03:18 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 15
Rep Power: 0
|
|
How to store data from textfile to vector and delete a selected row.
Can someone teach me how to store data from textfile to vector and delete a selected row. And after deleting, i want to write the changes in my textfile.
Do someone has an idea?
|
|

11-12-2009, 03:21 PM
|
 |
Senior Member
|
|
Join Date: Apr 2009
Location: Germany
Posts: 491
Rep Power: 1
|
|
|
|
|

11-12-2009, 03:22 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 15
Rep Power: 0
|
|
Thanks for the reply. But is it the same when i am using a vector?
|
|

11-12-2009, 03:23 PM
|
 |
Senior Member
|
|
Join Date: Apr 2009
Location: Germany
Posts: 491
Rep Power: 1
|
|
|
Do you have to use a Vector? Vector is a bit deprecated. You can use any kind of List.
|
|

11-12-2009, 03:25 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 15
Rep Power: 0
|
|
Sorry sir, my teacher says we should use vectors.. I am able to add and delete files from the textfile.. But i have a big problem regarding for editing and deleting a file in my textfile.
Thanks for the time
|
|

11-12-2009, 03:27 PM
|
|
Senior Member
|
|
Join Date: Aug 2009
Posts: 1,895
Rep Power: 2
|
|
|
You mean you are able to add and delete lines?
Well to edit, just edit the value inside the Vector itself and then rewrite the file again the same you rewrote it after deleting lines.
|
|

11-12-2009, 03:28 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 15
Rep Power: 0
|
|
Can you give me some idea on how to edit sir.. Please?
|
|

11-12-2009, 03:28 PM
|
 |
Senior Member
|
|
Join Date: Apr 2009
Location: Germany
Posts: 491
Rep Power: 1
|
|
|
Basically do this:
1) read file and store every line as a String in your Vector
2) modify or delete lines as you need in the Vector
3) write every line from the Vector back into the file.
1) and 3) are covered in the tutorial
2) is your job as we cannot tell you what to change.
|
|

11-12-2009, 03:29 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 15
Rep Power: 0
|
|
Thanks sir. wait, i have to try this task  I want to learn.. Thanks tutorial.. i'll give an update soon
Regards,
Nemesis
|
|

11-12-2009, 03:31 PM
|
|
Senior Member
|
|
Join Date: Aug 2009
Posts: 1,895
Rep Power: 2
|
|
Originally Posted by nemesis
|
Can you give me some idea on how to edit sir.. Please?
|
The first step is to find the one that you want to delete from the vector.
There are several methods already in the Vector class for doing this. Then you simply replace that element with the new one.
|
|

11-12-2009, 03:31 PM
|
 |
Senior Member
|
|
Join Date: Apr 2009
Location: Germany
Posts: 491
Rep Power: 1
|
|
|
|
|

11-13-2009, 02:56 AM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 15
Rep Power: 0
|
|
I have this problem in storing files from the textfile to vector.
Is this okay?
|
Code:
|
public static void main(String[] args) throws FileNotFoundException
{
Scanner inFile = new Scanner(System.in);
Vector <Subject> subj = new Vector<Subject>() |
This is for the main()..
Can someone check this code for me?
|
Code:
|
while(scanner.hasNextLine())
{
String lineremove = scanner.nextLine();
z++;
System.out.println(z + " " + lineremove);
}
System.out.println("What do you want to remove?");
System.out.println();
selectRemove = inFile.nextInt();
Subject v = new Subject();
while (scanner.hasNext())
{
String lineforremove = scanner.nextLine();
v.add(lineforremove);
}
System.out.println(subj.size());
try
{
boolean append = true;
pw = new PrintWriter(new FileWriter(new File("subjects.txt"), append));
pw.println(subj.toString());
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
pw.close();
} |
|
|

11-13-2009, 08:00 AM
|
|
Senior Member
|
|
Join Date: Aug 2009
Posts: 1,895
Rep Power: 2
|
|
|
Write at least three separate methods.
1.) Reads the file lines into a Vector.
2.) Writes the lines in the vector to a file.
3.) Edits an entry in the vector (has nothing to do with files).
|
|

11-13-2009, 09:51 AM
|
 |
Senior Member
|
|
Join Date: Apr 2009
Location: Chennai
Posts: 589
Rep Power: 1
|
|
|
Before started writing,just try to put comments before the methodd that what logic u are going to perform.How the data is formatted in ur text file like that...Then only it will be simpler for the people to give suggestions.
__________________
Ramya
|
|

11-13-2009, 12:00 PM
|
 |
Senior Member
|
|
Join Date: Oct 2009
Location: Australia, Land of the Upsidedown people
Posts: 246
Rep Power: 0
|
|
|
Also as a standard, you teacher should be teaching you to write variables with camel case.(each word start uppercase except the first) lineforremove should be lineForRemove as an example as this is easier to read.
He/she should also probably not be teaching you to use depreciated classes either, sounds like he needs to brush up on his/her java skills.
|
|
| Thread Tools |
|
|
| Display Modes |
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
|
|
|
All times are GMT +2. The time now is 12:33 AM.
|
|
VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org