Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2015
    Posts
    8
    Rep Power
    0

    Default File Handling problems

    This program is meant to replace any occurrence of " * " in a file with the empty character (null, or any character of my choice) then save the results in a new file. For some reasons, my new file remains empty even after running the program. I suspect there is something wrong in the logic of the program. Comments and suggestions are welcome!

    Java Code:
    File file = new File("C:/Users/2564832/Documents/input.txt");  
    Scanner scanner = new Scanner(file);      
    PrintWriter writer = new PrintWriter("C:/Users/243577844/Documents/output.txt");
    while(scanner.hasNextLine()){ 
    String line = scanner.nextLine(); 
    String newLine="";
    for (int i = 0; i < line.length(); i++){
    if (line.charAt(i) == '*') {  // 
    newLine += line.charAt(i);
    writer.println(newLine);
    }}
    Last edited by Prince_Jobless; 10-15-2015 at 01:28 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: File Handling problems

    I suspect there is something wrong in the logic of the program. Comments and suggestions are welcome!
    Do some debugging by adding print statements to print out the values returned by methods and the values of varibles every time they are changed. The print out will help you understand what the code is doing when it is executed.

    Note: The code has lost its formatting (no indentations for nested statements) making it harder to read. Please edit the post and fix the formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: File Handling problems

    Java Code:
    if (line.charAt(i) == '*') {  // 
      newLine += line.charAt(i);
    Debugging is great, but sometimes it also helps to just spell out what code does in plain English.

    if and only if the character is a star, ... (then what?)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: File Handling problems

    What is the content of the input.txt file?

    ETA: And, as an addition, is that all your code? Does this method, or whatever, exit at the end of that loop?
    Last edited by Tolls; 10-15-2015 at 02:34 PM.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: File Handling problems

    Well, since you asked for comments and suggestions. Are you required to do it this way?

    Have you considered String.replaceAll? Or using the Files class to generate a stream to do this.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. File handling.
    By zarak in forum New To Java
    Replies: 2
    Last Post: 10-16-2011, 10:42 PM
  2. File Handling
    By sindhiya arora in forum Eclipse
    Replies: 3
    Last Post: 03-29-2011, 08:27 AM
  3. File handling
    By Dayanand in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 09:28 AM
  4. File handling
    By Dayanand in forum New To Java
    Replies: 7
    Last Post: 03-23-2011, 07:31 AM
  5. Regarding File Handling
    By ravjot28 in forum New To Java
    Replies: 1
    Last Post: 01-20-2010, 09:15 PM

Tags for this Thread

Posting Permissions

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