Home > Guides > Programming > Java

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

Ease of Use and Presentation Features

Last updated Feb 23, 2004.

One thing that we had in C programming that has been painfully missed in Java is formatted input and output. In C, we would write something like the following:

printf( "The value of our float is %4.2f", myfloat );

Assuming that myfloat was a floating point number, this told the compiler to display at least four digits to the left of the decimal point (filling with zeros if needed) and two digits to the right of the decimal point. This new feature is convenient to Java programmers; I expect it to help make the translation of legacy C applications to Java much more straightforward.

The printf function is a new method of the java.io.PrintStream class; one common implementation is the System.out variable. For more specific implementation of all the printf() method supported features, see the J2SE 1.5 documentation and specifically the java.util.Formatter class, which acts as an interpreter for printf-style format strings.

In practice, you would use it as follows:

System.out.printf( "This is my float: %4.2f", myfloat );

The complement to formatted output is formatted input. C accomplished it by using the scanf() function; Java implements formatted input through the java.util.Scanner class. You can use the Scanner class to parse specific data types from the command line by wrapping System.in:

Scanner sc = Scanner.create(System.in);
int i = sc.nextInt();

Once you have created a Scanner instance, you can use its nextXXX() methods to extract the type of variable you are expecting:

String next()
String next(Pattern pattern)
String next(String pattern) 
BigDecimal nextBigDecimal()
BigInteger nextBigInteger()
BigInteger nextBigInteger(int radix) 
boolean nextBoolean() 
byte nextByte() 
byte nextByte(int radix) 
double nextDouble() 
float nextFloat() 
int nextInt() 
int nextInt(int radix) 
String nextLine() 
long nextLong() 
long nextLong(int radix) 
short nextShort() 
short nextShort(int radix) 

A couple points of note here:

  • The java.util.regex.Pattern class can be used to construct a regular expression to match to extract the next token

  • The nextLine() method advances the scanner past the end of the current line and returns all of the data that was skipped as a String

The rest should be pretty self explanatory.

Discussions

Read and display the table in the document
Posted Nov 12, 2008 06:01 AM by StrongHead
1 Replies
Correction
Posted Nov 4, 2008 06:09 PM by youssef.mohammed
1 Replies
Instead of synchronising getInstance
Posted Nov 3, 2008 05:42 AM by grahamkelly
1 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

Dustin SullivanIf You Are New to Java Programming...
By Dustin SullivanJune 2, 20092 Comments

We recently sat down with several top Java developers to talk about that state of the language as we approach this year's JavaOne.  As we were wrapping up, we threw one last question at them out of curiosity, and we thought you'd like to see what some of them said.

Steven HainesOracle Buys Sun of $7.4B
By Steven HainesApril 20, 2009 No Comments

In a stunning turn of events, Oracle steps in and buys Sun amist the breakdown of IBM's attempt to acquire Sun.

Steven HainesIBM in talks to buy Sun Microsystems for at least $6.5B
By Steven HainesMarch 18, 2009 No Comments

Reuters reported this morning that IBM is in talks to buy Sun Microsystems, which could "bolster their computer server products against rivals such as Hewlett-Packard Co."

See More Blogs

Informit Network