- Java Reference Guide
- Overview
- Table of Contents
- J2SE: Standard Java
- Java Windows NT Services
- Advanced J2SE
- J2SE 1.5.0: "Tiger"
- Ease of Use and Presentation Features
- Variable Arguments
- Desktop Client
- JSRs
- Adaptive Garbage Collection
- Official Documentation
- Java SE 6
- Core Computer Science Principles in Java (Data Structures)
- Annotations
- Java Generics
- Java New I/O
- Java Sound
- Java Applets
- JavaFX
- Java SE Threading
- Resource Management Using Semaphores
- Java Atomic Operations
- JavaTemplate Pages
- Executing Templates with the JtpExecutor
- Java Cryptography Extensions (JCE)
- Java Database Connectivity (JDBC) API
- Jakarta Commons - Net Class Library
- Jakarta Commons HttpClient
- Apache POI
- Regular Expressions
- JavaMail
- Cool Tools
- Building an Really Simple Syndication (RSS) Java App
- Logging with Log4J
- Inside Swing
- Swing Components
- SwingX
- Swing Styled Documents
- Web Rendering in Java Swing Applications
- Java Look-and-Feel Graphics Repository
- Java Media Framework
- Quicktime for Java
- Media in Java Review 2008
- Graphs and Charts
- Holiday Special: Electronic Greeting Card
- Media Framework: Presenter Application
- Standard Widget Toolkit
- JFace
- Java Performance Tuning
- J2EE Performance Tuning
- Caches and Pools
- Java Caching System
- Java Compression and Decompression
- Obfuscating Java Applications
- Continuous Integration
- Load Testing
- Tomcat Clustering
- Enterprise Java Testing
- Automated Unit Testing with JUnit and Ant
- Unit Testing: Tips From The Trenches
- Custom Ant Tasks
- Extensible Markup Language (XML)
- Java Web Technologies
- Web Frameworks
- Struts 2
- Wicket
- JavaServer Faces
- Distributed Programming / RMI
- Servlet Filters
- Building a Robust Java Server
- J2EE: Enterprise Java
- Spring
- Java Design Patterns
- XDoclet
- Hibernate
- Project Backup
- J2EE Project: Hands-On
- Enterprise Java Beans (EJB) 3.0
- Disaster Recovery
- Java Management Extensions (JMX)
- Service-Oriented Architecture
- Web Services
- RESTful Web Services New
- Project: Building a Web Photo Gallery
- J2ME: Micro Java
- Specialized J2ME
- Optional Packages
- Other Java Technologies
- Derivatives and Competitors
- Java, Engineered for Integration
- Additional Resources
- The World of Java Tools
- Building Java Applications with Ant
- Managing Java Build Lifecycles with Maven
- Source Control with Subversion
- Inversion of Control and Dependency Injection
- Certification
- Roadmap: Becoming an Enterprise Java Developer
- Roadmap: Becoming an Enterprise Java Developer in 2007
- The Business of Enterprise Software
- JavaOne 2006
- JavaOne 2007
- JavaOne 2008 Wrap-Up
- JavaOne 2009 Wrap-Up
- How to Survive in a Turbulent Job Market
- How to Hire the Best Talent
- Cloud Computing
- Enterprise Java in 2008 and Beyond
- Predictions for 2018
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.




Account Sign In
View your cart