Results 1 to 14 of 14
  1. #1
    mrdestroy is offline Member
    Join Date
    Oct 2008
    Posts
    22
    Rep Power
    0

    Default 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").

    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
    This is my code:
    Java 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();
    		}
    	}
    }
    My code made all lines uppercase but
    I want to uppercase just the line of name.
    Attached Files Attached Files
    Last edited by mrdestroy; 10-29-2008 at 09:14 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Just use a basic validation there. I mean checking the length or else.

    If not simply you can an index there.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    I want to uppercase just the line of name
    If the name is on the first line, only uppercase the first line.
    Test which line you are looking at before doing the uppercase.

  4. #4
    mrdestroy is offline Member
    Join Date
    Oct 2008
    Posts
    22
    Rep Power
    0

    Default

    Yes, that's the first line I wanna do the uppercase. But I am not sure how to do.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    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.

  6. #6
    mrdestroy is offline Member
    Join Date
    Oct 2008
    Posts
    22
    Rep Power
    0

    Default

    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.:(

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    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.

  8. #8
    mrdestroy is offline Member
    Join Date
    Oct 2008
    Posts
    22
    Rep Power
    0

    Default

    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
    This is what I'm thinking to do. But I dont know how to code.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    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. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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;     
                }

  11. #11
    mrdestroy is offline Member
    Join Date
    Oct 2008
    Posts
    22
    Rep Power
    0

    Default

    This is what I've done following checking length(). But I wanna know the way to do read line for later exercises.
    Java Code:
    while (true) {
    				line = bufferedReader.readLine();
    				if (line == null)
    					break;
    				if (line.length() > 10)
    					print.println(line.toUpperCase());
    				else
    					print.println(line);
    			}
    Thanks so much.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Reading the length is not a good practice actually. There is no guarantee that name length is less than 10. Avoid that bad way.

  13. #13
    presh4u Guest

    Default

    very hard to understand java of long length

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by presh4u View Post
    very hard to understand java of long length
    What are you doing here in this forum. If you try to spam the forum, please stop it. May you get band in our community.

Similar Threads

  1. DES algorithm (Read and Write bytes to file)
    By JoaoPe in forum Advanced Java
    Replies: 6
    Last Post: 07-29-2008, 04:46 PM
  2. File read/write problems
    By p900128 in forum New To Java
    Replies: 4
    Last Post: 06-27-2008, 01:15 AM
  3. Read/Find Substring/Write to new file
    By hiklior in forum New To Java
    Replies: 6
    Last Post: 04-23-2008, 03:47 AM
  4. Read-File Write Display substring
    By hiklior in forum New To Java
    Replies: 3
    Last Post: 04-18-2008, 12:45 PM
  5. Replies: 0
    Last Post: 11-14-2007, 07:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •