Results 1 to 3 of 3
  1. #1
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default change stringBuilder to String?

    Write a program that replace text in a source file and save the changes into the original file.

    I have few questions on following code.
    Line 34: output.printf("%s\r\n", sb.toString());

    1) why need to printf %s\r\n, since output.printf(sb.toString()); yields the same result
    2) why need to change the Stringbuilder sb to String since output.print(sb) still display the output?

    Java Code:
    import java.io.*;
    import java.util.*;
    
    class Exercise9_20 {
      public static void main(String[] args) throws Exception {
        // Check command line parameter usage
        if (args.length != 3) {
          System.out.println(
            "Usage: java Exercise9_20 sourceFile oldStr newStr");
          System.exit(0);
        }
    
        // Check if source file exists
        File sourceFile = new File(args[0]);
        if (!sourceFile.exists()) {
           System.out.println("Source file " + args[0] + " not exist");
           System.exit(0);
        }
    
        // Read text from the file and save it in a string builder
        Scanner input = new Scanner(sourceFile);
        StringBuilder sb = new StringBuilder();
    
        while (input.hasNext()) {
          String s1 = input.nextLine();
          String s2 = s1.replaceAll(args[1], args[2]);
          sb.append("\r\n" + s2);
        }
    
        input.close();
    
        // Write back to the file
        PrintWriter output = new PrintWriter(sourceFile);
        output.printf("%s\r\n", sb.toString());
        output.close();
      }
    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,029
    Rep Power
    4

    Default Re: change stringBuilder to String?

    1) printf uses the formatter to replace %s with the String parameter. In your case, an extra carriage return/linefeed is added.
    2) output.print(sb) handles an Object as parameter, which then internally does a String.valueOf(), which in turn does a toString() on it. output.print(sb.toString()) calls the method with a String parameter. The output is the same, but the method call is different. Try leaving sb = null to see different results.
    Last edited by SurfMan; 09-28-2013 at 01:37 PM. Reason: Changed answer 1

  3. #3
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Re: change stringBuilder to String?

    I see. thanks for the clarification

Similar Threads

  1. Stringbuilder hogging up the string >:(
    By MonkeyMan in forum New To Java
    Replies: 10
    Last Post: 01-20-2012, 11:07 AM
  2. String vs. StringBuilder vs. StringBuffer
    By stchman in forum New To Java
    Replies: 5
    Last Post: 01-12-2012, 08:52 AM
  3. String concatenation vs. StringBuilder.
    By stchman in forum New To Java
    Replies: 5
    Last Post: 08-23-2011, 12:17 PM
  4. How To change String to binary.
    By Khan05 in forum New To Java
    Replies: 16
    Last Post: 07-20-2011, 05:21 PM
  5. Change to string
    By PhQ in forum New To Java
    Replies: 7
    Last Post: 04-14-2010, 04:26 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
  •