System.out.printf

Hi all.
Just a question query.
I've just been taught the uses of the printf method in Java, but I'm still a little confused as to how it works.
E.g.,
System.out.printf("%o", 12); I know the o refers to octal, but I don't know much more than this. I don't want the answer (output), to this, but just a few steps on how it works to aid me in getting the answer myself. Thanks. :)
Something so simple I know...lol. :(

According to the API, % is a literal and the conversion does not correspond to any argument.No - The % in "%o" means "convert the argument to octal" it isn't a literal. You might be confusing this with the part of the documentation that talks about "%%" the second % in this expression is to be taken as a literal % that does not correspond to any argument. As in
int result = 5;
int max = 10;
System.out.printf("%d out of %d is %.2f%%%n", result, max, (double)result / max * 100);Learn these as you need them:
%f means a floating point number
%d means an integer (tricky but it's d for decimal)
%s means a string
and
%n means a literal newline
%% means a literal %
The literal characters are just printed - they don't correspond to an argument like s/d/f/o etc do.
Well basically the steps you used in order to get from 12 to 99. It just doesn't seem obvious at all.99? It prints the string "14". It's just as confusing how you get from 12 to 14. That's because you don't: it's just the string that changes, the number stays the same.
It's a dozen - the number of eggs in a carton. The ancient Romans would have used the string "XII" when buying eggs. The ancient Octalians would have asked for "14". In case you should ever need eggs in Octalia you figure out what to say as follows:
Working from the right hand end, the "4" means 4 eggs. The "1" means the number of groups of 8 eggs.
If you wanted a hundred eggs, you would ask for "System.out.printf("%o eggs, please.", 100);" - which is "144".
4 eggs, plus 4 lots of 8 eggs, plus 1 lot of 8*8=64 eggs.

Similar Messages

  • System.out.printf problem

    I keep getting an error message when I type in
    System.out.printf(celsius / 5 = (fahrenheit - 32 ) / 9);
    the error keeps pointing at the first /, stating that its an unexpected type?
    How do i fix this?

    RizwanMportal wrote:
    There is System.out.print()
    and
    System.out.println()
    Do not try printf .... I think you are still in C...Since System.out is a PrintStream object, and since PrintStream has a printf method, which is wrong, the API or you Rizwan?
    see:
    http://java.sun.com/javase/6/docs/api/java/io/PrintStream.html#printf(java.lang.String,%20java.lang.Object...)

  • Cannot  resolve symbol - attempt to use System.out.printf

    Hi All,
    I adapted a HelloWorld program to use
    System.out.printf instead of System.out.println (just for a test)
    The println comand compiles fine with no errors, but for some reason the printf generates compile time error. Please let me know what am doing wrong.
    Below is the error:
    d:\j2sdk1.4.1_05\bin>javac HelloWorld.java
    HelloWorld.java:6: cannot resolve symbol
    symbol : method Printf (java.lang.String,java.lang.String)
    location: class java.io.PrintStream
    System.out.printf("%s","Hello, world!");
    ^
    1 error
    code:
    public class HelloWorld {
    public static void main (String[] args) {
    System.out.printf("%s","Hello, world!");
    }

    If you check the documentation you can see this method was added in version 1.5
    http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintStream.html#printf(java.util.Locale,%20java.lang.String,%20java.lang.Object...)
    BTW: I suggest you use a current version of java, either 1.4.2_09 or 1.5.0_04

  • System.out.printf vs System.out.println

    Hello!
    I would like to know which is faster: printf or println.
    eg:
    System.out.println("Hello " + "World");
    System.out.printf("Hello %s", "World");
    I think that println is faster, because it doesn't have to look for format parameters and then the arguments. But of course, there're sometimes that you need to use printf (System.out.printf("Wage %.2f", wage));
    Well...I just asking it, because How To Program uses it a lot, maybe it's only teaching purpose.

    Encephalopathic wrote:
    Sounds like a micro-optimization question to me where the answer is either mu or is very close to mu. IMHO, it's much better to ask which code is more understandable to me and to others helping me since that's more of a rate-limiting step in code development.Agreed. Printing to the console is not something where you need to measure performance (unless you're trying to gauge whether or not printing to the console is going to slow you down, in which case it 99.9% of the time will).

  • System.out.printf(); Why is it wrong?

    I use Eclipse write java program.
    In Linux+jdk1.6 System.out.{color:#ff0000}printf{color}("%8d",45); is right!
    But {color:#0000ff}in windows+jdk1.6 , it is wrong! {color}
    And it show that: {color:#0000ff}The method printf( String ,object[] ) in the type printStream is not applicable for the arguments ( String,int ){color}
    Why is it wrong ? what can I do ?
    Help me . Thanks!

    You may have Java 1.6 but Eclipse is probably using an earlier version. Check your settings that you are using the correct version to compile with.

  • How to capture System.out, done by another program

    Hi All,
    My application runs other application in back. How can I capture system.out.println() written by another application & display via dialog box.
    Like :execl() or execv() function in C runtime library. Where every printf() message printed to passed buffer.
    Thank you,
    Avin Patel

    You can create an InputStream from a started process. Then read all the bytes from the inputstream and write them to an outputstream (like System.out)
    Process p = Runtime.getRuntime().exec(COMMAND);
    InputStream is = p.getErrorStream();   //Or another stream
    int[] buffer = new int[128];
    while((read = is.read(buffer)) != -1){
    System.write.out(buffer, 0, read);
    }

  • System.out.println capture to buffer

    Hi All,
    My application runs other application in back. How can I capture system.out.println() written by another application & display via dialog box.
    Like :execl() or execv() function in C runtime library. Where every printf() message printed to passed buffer.
    Thank you,
    Avin Patel

    If i understand your question correctly, you should execute command for you help.html file. The system will run default browser.
    such as:
    void runBrowser(String file)
    {String cmd = null;   
         Process p;   
              try  {       
                   String os = System.getProperty("os.name");            
                   if (os != null && os.startsWith("Windows")) {           
                        if (os.startsWith("Windows 9") || os.startsWith("Windows Me")) 
                        // Windows 9x/Me               
                             cmd = "start " + file;
    else
                        // Windows NT/2000/XP               
                             cmd = "cmd /c start " + file;
    p = Runtime.getRuntime().exec(cmd);            
    catch(IOException x)
    // couldn't exec browser
    System.err.println("Could not invoke browser, command=" + cmd);
    }

  • How do I get System.out.format to print out doubles with the same precision

    Using the System.out.format, how do I print out the value of a double such that it looks exactly like it does if it were print from a System.out.println.
    For example, take the following code:
    double d = 12.48564734342343;       
    System.out.format("d as format: %f\n", d);
    System.out.println("d as   sout: " + d);Running the code, I get:
    <font face="courier">
    d as format: 12.485647
    d as sout: 12.48564734342343
    </font>
    The precision of d has been lost.
    I could bump up the precision as follows:
    double d = 12.48564734342343;
    System.out.format("d as format: %.14f\n", d);
    System.out.println("d as   sout: " + d);That appears to work, I get:
    <font face="courier">
    d as format: 12.48564734342343
    d as sout: 12.48564734342343
    </font>
    However, that solution fails if d has a different precision, say 12.48. In that case I get:
    <font face="courier">
    d as format: 12.48000000000000
    d as sout: 12.48
    </font>
    So how do I get System.out.format to print out doubles with the same precision as System.out.println?
    Thanks..

    YoungWinston wrote:
    Schmoe wrote:
    Interesting, but this is not what I am looking for...Your original question was "how do I print out the value of a double such that it looks exactly like it does if it were print from a System.out.println", and you've been told how to do that (although the pattern given by sabre may be a bit excessive - you should only need 15 '#'s).The initial phrase from my question was "Using the System.out.format, how do I..".
    It's worth remembering that, unlike the Format hierarchy, 'format()' is NOT native to Java. It's a convenience implementation of the 'printf()' and 'sprintf()' methods provided in C, and first appeared in Java 1.5. Those methods were designed to produced fixed-format output; 'println()' was not.Perhaps it is the case that this can't be done.
    Furthermore, Double.toString(), which is what is used by println() does not produce the same format in all cases; format("%.14f\n", d) does. TrySystem.out.println(1.8236473845783d);
    System.out.println(1823647384.5783d);and you'll see what I mean.I am fine with that. It still displays all the precision.
    I am simply looking for a way to quickly print out multiple variables on a sysout while debugging. I want it as syntactically sweet as possible. System.out.println can be a pain when outputting multiple variables like the following:
    "System.out.println("a: " + a + "; b:" + b + "; c: " + c);"
    For some reason, my fingers always typo the plus key.
    I was hoping that System.out.format would be easier,along the lines of:
    "System.out.format("a: %f, b: %f, c: %f\n", a, b, c);"
    From a syntactical sweetness point of view, it is easier. However, the %f on doubles truncates the precision. I figured there must be a way to get the full precision.
    DecimalFormat is syntactically sour for this purpose, as you need to instantiate the DecimalFormat.
    fwiw I have enjoyed reading the suggestions in this thread...

  • Is there a way to make system.out.println() scroll down as it goes?

    I am writing a program where I want to be able to read what's printed out in the console of my IDE through System.out.println() as the program runs/after the program runs. However, right now I run the program, the message prints out, and then when I go to read it the scroll bar is all the way up so I can see only the top of the print-out (the first thing printed out). Is there any way I can get the console to scroll along with the text, kind of like floating boxes you see on various websites for various reasons? In other words, can I get it so that when I look in the console at any given moment of the program running, I am looking at the newest text printed out (the text at the bottom of the "page")? Thank you!

    That would be an IDE problem and not a Java one. You could redirect output into a text file so you can read at your leisure and scroll up and down as much as you like.

  • Is there a way to force System.out.println to run when called

    I working on my first threaded program and having a hard time debugging. I've used System.out.println to let me know what's going on but due (I assume) to the nature of threads the output is not sequential. Is there a way to force println to execute immediatly so that they show up in the order they were called?
    Thanks --- Mike

    mjs1138 wrote:
    endasil, Thanks for the reply. I'm currenlty running the program from within the NetBeans IDE. It is the output displayed by in NetBeans "output" that I'm looking at.
    --- MikeI don't use Netbeans, but I would guess that it too pipes Standard Out and Standard Error to the same console. You didn't address my comment. Are you printing to System.err as well? This happens implicitly if you use Exception.printStackTrace(), for example.

  • System.out not working on eclipse

    Greetings...
    I am no longer a newbie on Java, but I have been facing an issue lately that I have been unable to resolve myself, even though it seems to be something so silly.
    I have been using eclipse for development, and by developing a simple "Just to practice" code I have found myself unable to use System class or any of its methods at a certain point. See the code below:
    package one;
    import java.util.Scanner;
    public ClassOne {
    public static void main (String args[]){
    Scanner reader = new Scanner(System.in); /*Works just fine here*/
    int x = 0;
    System.out.println("Type in the number of objects expected"); /*Does not work at all, like if System was something I could not use at all. Not an identifier */
    Scanner readertwo = new Scanner(System.in); /*System does not work here either*/
    }Even if I rewrite the code after that, by erasing the import statement and all other System references in the code, System does not work again.
    If anybody has ever faced the same issue and knows what I am doing wrong and what I can do to fix it, I'd really be grateful.
    Thanks to all, and happy 2009.
    Santana T

    Santana_Thiago wrote:
    It works on NetBeans.
    On eclipse I get the following error:
    ERROR: JDWP Unable to get JNI 1.2 Environment, jvm->GetEnv() return code = -2+
    *JDWP exit error AGENT_ERROR_NO_JNI_ENV(183): [../../../src/share/back/util.c:820]*+
    The funniest thing is that if copy and paste the code from somewhere else into eclipse it works. What a drag!!!!
    Thank you.
    Santana TNever seen that error but it sounds to me that your Eclipse installation is screwed up!

  • 32-bit JDK 7 System.out.println not working in IDE

    Hi folks,
    I have a 64-bit Windows 7 OS.
    Due to 3rd party library/jar dependencies, i had to install the 32-bit Java JDK 1.7 and Eclipse IDE.
    I also installed NetBeans.
    So i have a 64-bit OS and am running 32-bit Java JDK/JRE & IDEs.
    The problem I am having is that my program's System.out.println("...") statements are not outputting strings to either IDE debug console.
    Executing the compiled program from a command line/prompt produces the expected string output.
    The basic "Hello, World" program is enough to cause this behaviour to start occurring.
    I have not manually / intentionally changed any IDE-specific Debug Console or Windows environment settings.
    One caveat: This same environment has worked successfully in the past ?! Yes, this is one of those "..it worked last week & yesterday and today it isn't and i swear i didn't do anything..." issue.
    Thoughts ?

    Thanks for the reply.
    The 64-bit versions of Java & Eclipse were installed first.
    When i discovered I had to use the 32-bit versions, i un-installed the 64-bit ones & installed the 32-bits.
    Even after that initial un-install 64-bit/install 32-bit process, it was working.
    I have also been installing the Windows 7 64-bit OS updates when i am informed of them.
    I'm not sure if any of these would affect how the Eclipse / NetBeans IDEs behave.
    Behaviour has been inconsistent.
    Initially it was always working.
    But over the past several days, it has been working less and less.
    I don't have any large data structures.
    This isn't a large complicated program, couple hundred lines, so i highly doubt that i'm doing anything to the resources, but something has changed.
    The main project I am working on takes command line parameters, does some initial processing, produces output using System.out.printlns [SOP] then depending on the parameters, branches into 2 different processing paths, let's call them A & B. Each of these processing paths also use SOPs. When i run the program in the IDE going thru path A, sometimes the initial SOP statements will work and the SOP statements specific to path A will also work. If i immediately change the parameters to go thru path B & re-run it, not even the initial SOP statements before the branching decision work.
    I've tried doing System.flush()s too - no affect.
    I haven't tried the re-direction option to a file option you mentioned yet.
    It always works from a command prompt - that is telling me that the Java SOPs are working properly, correct ?
    Inside an Eclipse or NetBeans IDE, SOP output to the debug console is inconsistent.
    Running from a command prompt, the SOPs always work.
    It'd help to know if this an IDE issue, a Java issue, a Windows 7 issue so i can narrow down where to try and correct the situation.
    I have a Windows XP VM set up, i'll try running the program there and see if there's a difference.
    Thanks for your reply.

  • How can i put a system.out.println into txt file

    i want to generate a txt file instead of system.out.println. for the output How can i do that and what code can i use for that??
    Edited by: crystalarun on Oct 14, 2007 11:40 AM

    suppose u ant output in "Output.txt"
    then code can be
    PrintStream out = new PrintStream(new FileOutputStream("Output.txt"));
    System.setOut(out)

  • System.out.println () is not working properly

    Hi, Why does this happens:
    Object temp=null;
    System.out.println ("temp is null? "+temp==null);
    just prints: true
    expected: temp is null? true
    Why does this happens????
    If I do System.out.println ("temp is null? "+(temp==null)); it works bu it should work without the pharentesis too

    MelGohan wrote:
    Hi, Why does this happens:
    Object temp=null;
    System.out.println ("temp is null? "+temp==null);
    just prints: trueThat is odd, mine prints "false".
    type Test126.java
    public class Test126 {
        public static void main(String[] args) {
            Object temp=null;
            System.out.println ("temp is null? "+temp);
            System.out.println ("temp is null? "+temp==null);
    }javac Test126.java
    java Test126
    temp is null? null
    false

  • System.out.println in Web Dynpro Java

    I call System.out.println in some components in web dynpro java.  But I cannot find the standard console output file of SAP J2EE Engine. Anyone know where is the location of standard output file?

    Thanks Deepak. Your link blog is working.
    Sreeni: I cannot find a start-up log file. Could you please tell me the real path?
    Edited by: Nuttakorn Boonthamtanarung on Apr 1, 2010 6:58 AM

Maybe you are looking for

  • TV screen flickers after connecting to HDMI cable with DV6 laptop

    When i connect my ps3 to samsung LCD tv it works completely fine.  But When i connect my hp laptop to same tv. The screen start's to fliker after 2-3 mins. Tried different refesh rate's and resolutions. didn't work for me . Also updated my display dr

  • Audio help wanted exporting to QT and back into FCP.

    I have a sequence with live sound on A1 and A2, music on A3 and A4 and some extra voice on A5 and A6. I have gone into audio outputs and clicked "6 outputs" and "dual mono" for each and tried checking all the options for "Discrete channels" "Stereo d

  • How to make niconfig.daq active?

    I have a system using Win 95, Run Time Engine 5.1.1, Ni-Daq 6.1, and an executable application which worked previously until the customer uninstalled everything.  Everything has been re-installed and all error messages eliminated.  No particular orde

  • Proper way to configure JDNI datasource

    Hi all. I'm a little bit confused about the myriad of ways to potentially configure a JNDI resource reference (in this case a datasource). I'm using Tomcat 4.1 and Struts. It appears that JNDI references can be specified in the files: Tomcat: server.

  • BTHUB 5 opening ports doesn't seem to work.

    I've was recently got a upgrad to my phone line and got the Unlimited Faster Broadband package. The extra speed is great, but I'm unable to access thing like my online banking and various other sites. I do a little gaming and purchased a new game (Wi