Download presentation
Presentation is loading. Please wait.
1
©2004 Brooks/Cole Chapter 3 Interactive Input
2
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Standard Input and Output System.in Is Used to Enter Data; System.out Is Used to Display Data
3
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 System.in System.in is an InputStream –InputStream has a method called read that returns a byte –Bytes need to be combined to get useful pieces of infromation There are several classes we can use to get input in a more convenient form –InputStreamReader and BufferedReader together allow us to read one line at a time into a String Need to do a conversion to get primitive values –Scanner allows us to read Strings and primitive values directly
4
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Scanner class New in Java 1.5 Located in the java.util package Create a Scanner from System.in to get input from the keyboard Scanner kbd; kbd = new Scanner(System.in); Now use the Scanner methods to read data from the keyboard
5
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Scanner Methods MethodReturns nextInt()int value nextLong()long value nextFloat()float value nextDouble()double value next()one word (whitespace separated) nextLine()remainder of current line
6
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Example int n = kbd.nextInt(); double d = kbd.nextDouble() String word = kbd.next(); String text = kbd.nextLine();
7
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Tokenizing Strings The String class has some methods that can be used to break long Strings into pieces. We'll look at substring and indexOf later StringTokenizer can be used to break up strings that are lists By default, StringTokenizer uses spaces to delimit the tokens You can specify a different delimiter
8
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Tokenizing Strings Sometimes you will have a single String that contains several pieces of data –from a JOptionPane.showInputDialog which asks for more than one number at a time –if the input to your program has different formats depending on how the line starts We call the pieces of the String tokens.
9
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Approaches The String class has some methods that can be used for tokenizing a String –substring, indexOf There is a class called StringTokenizer which makes the process easier –in the java.util package
10
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 StringTokenizer Create a StringTokenizer with a String argument StringTokenizer st = new StringTokenizer( textString) Methods –countTokens() returns the number of tokens –next() –hasNext() returns true if there is another token
11
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Parsing Tokens from a String Create a StringTokenizer object from the String Use the next() method of the StringTokenizer to get the pieces of the String Use the parse methods of the appropriate Wrapper class to convert the tokens to numeric values if necessary
12
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Conversions between Strings and Numbers Sometimes we have Strings that represent numeric values –The result of JOptionPane.showMessageDialog Sometimes, we want to insert a number into a String –for outputting results for example
13
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Wrapper Classes There is a Wrapper class associated with each primitive type –Used to create objects from primitive values –Class methods allow you to convert String to primitive type convert primitive type to String
14
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Wrapper Class Methods ClassString -> PrimitivePrimitive -> String Integerint Integer.parseInt( String)Integer.toString(int) Longlong Long.parseLong( String)Long.toString( long) Floatfloat Float.parseFloat( String)Float.toString( float) Doubledouble Double.parseDouble( String) Double.toString( double)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.