Results 1 to 15 of 15
Like Tree2Likes
  • 1 Post By pbrockway2
  • 1 Post By Tolls

Thread: printf VS printWriter.printf

  1. #1
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default printf VS printWriter.printf

    Hi,

    While writing a program that writes into a file, I saw that there's a difference in the printf method.

    While in a "regular" printf or print I use only \n in order to pass a line, in the printWriter.printf I need to use \r\n .

    That one I've been able to find out, but some of the other functions of printf seems to work differently and I can't figure out why .

    when I'm writing :

    newFile.printf("%-16s",abc);
    newFile.print("here\r\n");
    newFile.printf("%-16s",abcd);
    newFile.print("here\r\n");
    newFile.printf("%-16s",abcde);
    newFile.print("here\r\n");
    newFile.printf("%-16s",abcdef);
    newFile.print("here\r\n");

    I expect to see the "here"s always in the same place, after 16 spaces.
    But instead, I find the "here"s in different places according to the length of the word before it (abc\abcd\abcde\abcdef)

    What am I supposed to do to make it right? (When I say "right" i mean all the "here"s in the same place one below the other)

    Thanks!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default Re: printf VS printWriter.printf

    Quote Originally Posted by Mapisto View Post
    Hi,

    While writing a program that writes into a file, I saw that there's a difference in the printf method.

    While in a "regular" printf or print I use only \n in order to pass a line, in the printWriter.printf I need to use \r\n .
    Are you sure it's not down to the text editor your are reading these in?
    '\n' doesn't show up in Notepad for example.

    Quote Originally Posted by Mapisto View Post
    That one I've been able to find out, but some of the other functions of printf seems to work differently and I can't figure out why .

    when I'm writing :

    newFile.printf("%-16s",abc);
    newFile.print("here\r\n");
    newFile.printf("%-16s",abcd);
    newFile.print("here\r\n");
    newFile.printf("%-16s",abcde);
    newFile.print("here\r\n");
    newFile.printf("%-16s",abcdef);
    newFile.print("here\r\n");

    I expect to see the "here"s always in the same place, after 16 spaces.
    But instead, I find the "here"s in different places according to the length of the word before it (abc\abcd\abcde\abcdef)

    What am I supposed to do to make it right? (When I say "right" i mean all the "here"s in the same place one below the other)

    Thanks!
    Java Code:
    public class Scratch {
    
       public static void main(String[] args) {
          System.out.printf("%-16s", "abc");
          System.out.println("x");
          System.out.printf("%-16s", "abcd");
          System.out.println("x");
          System.out.printf("%-16s", "abcde");
          System.out.println("x");
          System.out.printf("%-16s", "abcdef");
          System.out.println("x");
       }
    }
    That (base don your code) lines up correctly, so could it be your file reader casuing some problems?

  3. #3
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: printf VS printWriter.printf

    I'm using Windows and opening the file with notepad.

    When using the System.out.printf everything works just fine (in the console).

    When I'm using printWriter.printf it prints it different in the file.

    I figure the problem must be with the notepad, nontheless, it must be readable in notepad : \ . so how can i make it readable in notepad ?

    Thanks!

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default Re: printf VS printWriter.printf

    No it's not.
    I would blame notepad before anything else.
    As I said, it still doesn't recognise '\n'.
    Java Code:
       public static void main(String[] args) {
          //      System.out.println(0x31);
          PrintWriter pw = null;
          try {
             pw = new PrintWriter(new FileWriter("Test.txt"));
             pw.printf("%-16s", "abc");
             pw.println("x");
             pw.printf("%-16s", "abcd");
             pw.println("x");
             pw.printf("%-16s", "abcde");
             pw.println("x");
             pw.printf("%-16s", "abcdef");
             pw.println("x");
          } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
          } finally {
             if (pw != null) {
                pw.close();
             }
          }
       }
    That works and displays correctly in notepad.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default Re: printf VS printWriter.printf

    As does:
    Java Code:
       public static void main(String[] args) {
          //      System.out.println(0x31);
          PrintWriter pw = null;
          try {
             pw = new PrintWriter(new FileWriter("Test.txt"));
             pw.printf("%-16s", "abc");
             pw.print("x\r\n");
             pw.printf("%-16s", "abcd");
             pw.print("x\r\n");
             pw.printf("%-16s", "abcde");
             pw.print("x\r\n");
             pw.printf("%-16s", "abcdef");
             pw.print("x\r\n");
          } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
          } finally {
             if (pw != null) {
                pw.close();
             }
          }
       }
    So I have no idea what you're seeing.

  6. #6
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: printf VS printWriter.printf

    ok,

    here's the line i use:

    Java Code:
    newFile.printf("%2d %-16s %-5d \r\n",this.id, this.fullName, this.salary);
    in the file it appears like this:


    4 SissoVeSimhoo__4000
    1 SassonSassoni __1000
    3 JamesBond007___ 3000
    2 Cleopatra_____2000
    8 WinnieThePooh__8000
    5 Sinderela_____5000
    7 Shilgiya_____7000
    6 ElivsIsAlive___6000


    I can't figure out what I've done wrong :| I need it to appear like this:

    4 SissoVeSimhoo____4000
    1 SassonSassoni____1000
    3 JamesBond007____3000
    2 Cleopatra________2000
    8 WinnieThePooh___8000
    5 Sinderela________5000
    7 Shilgiya_________7000
    6 ElivsIsAlive______6000



    _ = space

    What i mean is that every salary will appear one just below the other.
    Last edited by Mapisto; 01-19-2012 at 05:42 PM.

  7. #7
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: printf VS printWriter.printf

    oh hell, it won't show it right here either :| I'll edit it

    ---Done---
    Last edited by Mapisto; 01-19-2012 at 05:40 PM.

  8. #8
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: printf VS printWriter.printf

    Oh,
    and btw, that does the catch and finally do ?

  9. #9
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: printf VS printWriter.printf

    Please help :(
    I need to give this program on sunday and that's the only thing I haven't been able to solve :(
    No matter what I'm trying, it won't place the numbers in the right order.

    I've noticed another thing while trying to solve it: if i change the flag for the salary (it's called flag, right ? : \ ) from d (which supposed to accept an int) to s (which supposed to accept a string) it didn't show any error and did the same, isn't it supposed to give an error ?

    Thanks again,
    I know I'm annoying, but I have no choice

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: printf VS printWriter.printf

    The various versions of printf() do the same thing. Whether or not that is the the thing you want.

    You can also get new lines using %n which is designed to do the right thing for the platform on which it is run. (Although just not using Notepad is a better solution as that program is broken.) If you have platform specific requirements for the newline, use %n.

    As regards the behaviour of %s it is also described in the API docs. (which is the first place to check). The %s conversion is listed in the table there in the "general" category ie it accepts any argument type. Although its behaviour with primitives is not mentioned - either there or in the details section -, it's a fair bet that it does a String.valueOf() or similar.

  11. #11
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: printf VS printWriter.printf

    Hi, thanks.

    The thing is that it is supposed to be opened by notepad : \ that's what they're asking.

    Is there a way to fix this thing in the program? so it would open correctly in notepad?

    If not, with which program can I check the file to see if it's just a notepad problem for sure?

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: printf VS printWriter.printf

    Did you try %n?

    Java Code:
    import java.awt.Desktop;
    import java.io.File;
    import java.io.PrintWriter;
    
    public class NotepadEg {
        public static void main(String[] args) throws Exception {
            File file = new File("c:/out.txt");
            PrintWriter pw = new PrintWriter(file);
            for(int i = 0; i < 42; i++) {
                pw.printf("Line %d here%n", i + 1);
            }
            pw.close();
            Desktop.getDesktop().open(file);
        }
    }
    This opens Notepad with 42 distinct lines for me.
    Mapisto likes this.

  13. #13
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: printf VS printWriter.printf

    I've just sent the file to a friend.
    It appeared fine in his comp!

    So it's only my stupid notepad.

    Could be frustrating :|

    Thanks a lot!!

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default Re: printf VS printWriter.printf

    Quote Originally Posted by Mapisto View Post
    Oh,
    and btw, that does the catch and finally do ?
    That's exception handling.
    Opening a file, or reading from one, can throw an IOException, so I handle it in the catch block (well, I print the exception).
    The finally block is always executed whether there was an exception or not, and it's usually used to close off resources. In this case I use it to close the file stream.
    Mapisto likes this.

  15. #15
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

Similar Threads

  1. Using printf without specifiers
    By FAkamo in forum New To Java
    Replies: 1
    Last Post: 10-17-2011, 08:46 AM
  2. printf
    By razzle in forum New To Java
    Replies: 4
    Last Post: 11-12-2010, 02:13 PM
  3. Printf problem!!!
    By pinar in forum New To Java
    Replies: 3
    Last Post: 11-06-2010, 09:44 PM
  4. Printf error
    By ks1615 in forum New To Java
    Replies: 4
    Last Post: 03-10-2009, 06:59 AM
  5. printf
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 04:31 AM

Posting Permissions

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