Results 1 to 14 of 14
Thread: Read and Write file
- 10-29-2008, 08:11 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 22
- Rep Power
- 0
Read and Write file
Suppose that we already have a file which contains the students’ records (See file Student.txt). Each student record includes following information: Fullname, Location, Age in that order. You should write a program to read that file and create another file (named Student_copied.txt) with the contents as follows:
• FullName: Copy and change to uppercase letters in new file.
• Location, Age: Copy exactly the content of old file.
In this exercise, you should specify only file name, don’t specify the path. For
example: new FileWriter("Student_copied.txt").
This is my code:Java Code:Nguyen Xuan Phuong -------> NGUYEN XUAN PHUONG //uppercase Hcm Hcm //the same 19 19 Nguyen Van Ngoc NGUYEN VAN NGOC //uppercase Hcm Hcm//the same 20 20
My code made all lines uppercase butJava Code:public static void main(String[] args) { String line; try { BufferedReader bufferedReader = new BufferedReader(new FileReader("Student.txt")); PrintWriter print = new PrintWriter(new FileWriter("Student_copied.txt")); while (true) { line = bufferedReader.readLine(); if(line == null) break; String str = line.toUpperCase(); print.println(str); } bufferedReader.close(); print.close(); } catch (Exception e) { e.printStackTrace(); } } }
I want to uppercase just the line of name.Last edited by mrdestroy; 10-29-2008 at 08:14 AM.
- 10-29-2008, 08:17 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Just use a basic validation there. I mean checking the length or else.
If not simply you can an index there.
- 10-29-2008, 02:12 PM #3
If the name is on the first line, only uppercase the first line.I want to uppercase just the line of name
Test which line you are looking at before doing the uppercase.
- 10-29-2008, 02:19 PM #4
Member
- Join Date
- Oct 2008
- Posts
- 22
- Rep Power
- 0
Yes, that's the first line I wanna do the uppercase. But I am not sure how to do.
- 10-29-2008, 02:34 PM #5
Use an if () statement with a counter or boolean to determine which is the first line.
if(<firstline?> {
<uppercase>
}
increment a counter as each line is read. the counter will only have the initial value the first time, after that it will have greater values.
- 10-29-2008, 05:31 PM #6
Member
- Join Date
- Oct 2008
- Posts
- 22
- Rep Power
- 0
Nguyen Xuan Phuong//toUpperCase
HCM
19
Nguyen Van Ngoc//toUpperCase
HCM
20
Nguyen Phong//toUpperCase
Hanoi
21
Nguyen Thi Hoa//toUpperCase
Hanoi
20
I want to do the uppercase these lines, I'm in stuck.:(
- 10-29-2008, 07:37 PM #7
How do you identify which line you want to UC? Can you use that technique in your program?
It looks like the lines to UC are 1, 4, 7, 10, 13, etc => the pattern is the first line, count 3, then UC, count 3 then UC etc
So what can you do in your program to associate an int value with each line?
How would you do it with your fingers? You fold them done as each line is read.
- 10-29-2008, 10:51 PM #8
Member
- Join Date
- Oct 2008
- Posts
- 22
- Rep Power
- 0
This is what I'm thinking to do. But I dont know how to code.Java Code:It looks like the lines to UC are 1, 4, 7, 10, 13, etc => the pattern is the first line, count 3, then UC, count 3 then UC etc
- 10-29-2008, 11:42 PM #9
use an int for the counter.
Add one to the counter for each line read
reset the value to 0 after the third line is read
us an: if(anInt == aValue) {} to detect the desired lines.
The first line would be counter=0 and the last of 3 lines would be 2
- 10-30-2008, 06:18 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Here is a simple logic to do this. Carefully look at what happen there, specially on the counter value handling.
Java Code:int count = 0; while (true) { line = bufferedReader.readLine(); if(line == null) break; // Print the result if(count == 0) { print.println(line.toUpperCase()); } else { print.println(line); } // Handling the counter count++; if(count == 3) count = 0; }
- 10-30-2008, 06:53 AM #11
Member
- Join Date
- Oct 2008
- Posts
- 22
- Rep Power
- 0
This is what I've done following checking length(). But I wanna know the way to do read line for later exercises.
Thanks so much.Java Code:while (true) { line = bufferedReader.readLine(); if (line == null) break; if (line.length() > 10) print.println(line.toUpperCase()); else print.println(line); }
- 10-30-2008, 06:55 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Reading the length is not a good practice actually. There is no guarantee that name length is less than 10. Avoid that bad way.
- 10-31-2008, 12:07 PM #13presh4u Guest
very hard to understand java of long length
- 10-31-2008, 12:11 PM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
DES algorithm (Read and Write bytes to file)
By JoaoPe in forum Advanced JavaReplies: 6Last Post: 07-29-2008, 03:46 PM -
File read/write problems
By p900128 in forum New To JavaReplies: 4Last Post: 06-27-2008, 12:15 AM -
Read/Find Substring/Write to new file
By hiklior in forum New To JavaReplies: 6Last Post: 04-23-2008, 02:47 AM -
Read-File Write Display substring
By hiklior in forum New To JavaReplies: 3Last Post: 04-18-2008, 11:45 AM -
.jnlp cant read & write on browser despite signed .jar
By bongia in forum New To JavaReplies: 0Last Post: 11-14-2007, 06:04 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks