23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

for (double degrees = 0.0; degrees < 360.0; degrees++) {<br />

y += lineHeight;<br />

String degreeString = myFormat.format(degrees, new StringBuffer(),<br />

fp).toString();<br />

String intPart = degreeString.substring(0, fp.getEndIndex());<br />

g.drawString(degreeString, xmargin + desiredPixelWidth<br />

- fm.stringWidth(intPart), y);<br />

String radianString = myFormat.format(Math.PI*degrees/180.0,<br />

new StringBuffer(), fp).toString();<br />

intPart = radianString.substring(0, fp.getEndIndex());<br />

g.drawString(radianString,<br />

xmargin + fieldWidth + desiredPixelWidth -<br />

fm.stringWidth(intPart), y);<br />

String gradString = myFormat.format(400 * degrees / 360,<br />

new StringBuffer(), fp).toString();<br />

intPart = gradString.substring(0, fp.getEndIndex());<br />

g.drawString(gradString,<br />

xmargin + 2*fieldWidth + desiredPixelWidth -<br />

fm.stringWidth(intPart), y);<br />

}<br />

}<br />

}<br />

16.5 Parsing Input<br />

<strong>Java</strong> I/O<br />

Number formats also handle input. When used for input, a number format converts a string in<br />

the appropriate format to a binary number, achieving more flexible conversions than you can<br />

get with the methods in the type wrapper classes (like Integer.parseInt()). For instance, a<br />

percent format parse() method can interpret 57% as 0.57 instead of 57. A currency format<br />

can read (12.45) as -12.45.<br />

There are three parse() methods in the NumberFormat class. All do roughly the same thing:<br />

public Number parse(String text) throws ParseException<br />

public abstract Number parse(String text, ParsePosition parsePosition)<br />

public final Object parseObject(String source, ParsePosition parsePosition)<br />

The first parse() method attempts to parse a number from the given text. If the text<br />

represents an integer, it's returned as an instance of java.lang.Long. Otherwise, it's returned<br />

as an instance of java.lang.Double. If a string contains multiple numbers, only the first one<br />

is returned. For instance, if you parse "32 meters" you'll get the number 32 back. <strong>Java</strong> throws<br />

away everything after the number finishes. If the text cannot be interpreted as a number in the<br />

given format, a ParseException is thrown. The second parse() method specifies where in<br />

the text parsing starts. The position is given by a ParsePosition object. This is a little more<br />

complicated than using a simple int but does have the advantage of allowing one to read<br />

successive numbers from the same string. The third parse() method merely invokes the<br />

second. It's declared to return Object rather than Number so that it can override the method of<br />

the same signature in java.text.Format. If you know you're working with a NumberFormat<br />

rather than a DateFormat or some other nonnumeric format, there's no reason to use it.<br />

The java.text.ParsePosition class has one constructor and two public methods:<br />

public ParsePosition(int index)<br />

public int getIndex()<br />

public void setIndex(int index)<br />

412

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!