app1 - APPENDIX 1 More on the Scanner Class and Output...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
APPENDIX 1 More on the Scanner Class and Output Formatting This appendix provides further details on the Scanner class, especially on the useDelimiter method. There is also a discussion of the printf method for output formatting. The useDelimiter Method in the Scanner class In the following example, a scanner retrieves each word in a line, and the word is converted to lower-case and printed. The scanner is declared in a method whose only parameter is a line to be parsed into words: public void run() { split ("Here today gone tomorrow"); } // method run public void split (String line) { Scanner sc = new Scanner (line); while (sc.hasNext()) System.out.println (sc.next().toLowerCase()); } // method split The output will be here today gone tomorrow Unfortunately, if the input contains any non-alphabetic, non-whitespace characters, those characters will be included in the tokens. For example, if the call is split (“Here today, gone tomorrow.”);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The output will be here today, gone tomorrow. We can override the default delimiter of whitespace with the
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This test prep was uploaded on 04/21/2008 for the course CS 103 taught by Professor Billcollins during the Spring '08 term at Lafayette.

Page1 / 4

app1 - APPENDIX 1 More on the Scanner Class and Output...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online