Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.

Similar presentations


Presentation on theme: "COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal."— Presentation transcript:

1 COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal Numbers *Wrapper Classes *Standard Input *GregorianCalendar

2 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Standard Output *The showMessageDialog method is intended for displaying short one-line messages, not for a general-purpose output mechanism. *Using System.out, we can output multiple lines of text to the standard output window - the console.

3 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. System.out *System.out is a PrintStream object. *Two useful methods the print method takes a String as its argument and prints it to the console the println method takes a String as its argument and prints it to the console with a newline character appended to the end *Both methods will continue printing from the end of the currently displayed output. *Both methods will do the necessary type conversion if we pass numerical data.

4 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Overloaded + Operator *The + operator can be used in 2 ways adding numbers concatenating Strings *What happens if we mix its arguments int x = 1; int y = 2; String output = “test” + x + y; String output = x + y + “test”;

5 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Output Example Printing a prompt on same line as input will be typed: System.out.print( "Enter radius: "); Echo printing the input using print and println: System.out.print( "The radius is "); System.out.println( radius); Printing results (using +): System.out.println( "Area : " + area);

6 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. DecimalFormat *Since floating point numbers can't be represented exactly in computer memory, they often print out with lots of digits after the decimal point. This can make the output look messy *The DecimalFormat class lets you print numbers with a fixed number of digits after the decimal place. *Use a "picture" string to show what you want the number to look like "0.000" means 3 digits after the decimal point *Use the format method with the number you want formatted as an argument

7 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. DecimalFormat Example *Create the DecimalFormat with a String DecimalFormat df = new DecimalFormat("0.000"); *Call the format method with the number as an argument double fp; /* assign a value to fp */ System.out.print( df.format(fp));

8 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Getting Numerical Input Values *Java provides primitive data types for working with numbers. *Wrapper classes exist that allow you to make an object out of a number. There is a wrapper class for each primitive type: Integer, Double, … *These classes provide methods we can use to convert an appropriate String object to a numerical value. Integer.parseInt() converts a String like "23" to an int Double.parseDouble() converts a String like "2.3" to a double

9 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Wrapper classes *Allow you to create an object from a primitive value *Are used to perform necessary type conversions Converting a String object to a numerical value Converting a numeric value to a String. *There is a wrapper class for each of the java primitive types

10 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Getting Numeric Input Data Data from the keyboard often comes into the program as a String String radiusStr = JOptionPane.showInputDialog(null, "Enter radius:"); A string containing numeric data needs to be converted before it can be used in calculations radius = Double.parseDouble(radiusStr);

11 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Standard Input *Using input dialogs is tedious for large amounts of input. And, not very user-friendly *The technique of using System.in to input data is called standard input. *System.in is an InputStream which handles byte data. *Using the intermediate InputStreamReader object allows us to read a single character at a time. *The BufferedReader class allows us to read one line at a time (in String form) We could read numbers with BufferedReader and wrapper classes

12 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. The Scanner Class *Java1.5 has added a new input class that makes numeric input simpler. java.util.Scanner *Use System.in to create a Scanner Scanner kbd = new Scanner( System.in); Note: All the Scanner examples in the text need to be corrected.

13 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Scanner input methods nextInt gets an int from the input int i = kbd.nextInt(); nextDouble double d = kbd.nextDouble(); nextByte, nextShort, nextLong and nextFloat work similarly next() gets a String reads characters until the next whitespace

14 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Reading line-by-line with Scanner *By default, Scanner uses white space to delimit (separate) Strings *To read an entire line need to replace the delimiter Ask System class what it is for your OS String delim = System.getProperties( "line.separator"); Tell the Scanner to use it kbd.useDelimiter( delim); Then read the line using next String line = kbd.next();

15 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. The GregorianCalendar Class *The GregorianCalendar class is useful in manipulating calendar information such as year, month, and day. *You can create a GregorianCalendar for any date by giving the year, month and day cal = new GregorianCalendar(2001, 8, 11); Caution: the first month of the year, January, is represented by 0.

16 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. GregorianCalendar *getTime method returns a Date *get(Calendar.property) allows you to determine year, month, date, day of week, hours, minutes, … *Use SimpleDateFormat or DateFormat to output the date in the desired format *DateFormat has a parse method that takes a String in the proper format and returns a Date

17 COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Extra Material *BufferedReader is covered later in your text

18 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Input classes *How the sequence of I/O objects adds greater capabilities. InputStream has the capability to read bytes InputStreamReader has the capablity to read characters (2 bytes) BufferedReader can read an entire line

19 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Using BufferedReader *Declaration BufferedReader kbd *Creation kbd = new BufferedReader( new InputStreamReader( System.in)); *Use String input = kbd.readLine();

20 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Handling Input Errors *Calling the readLine method of a BufferedReader can result in an error condition called an exception. *The programmer is required to handle this potential error condition. *For now we choose to ignore these problems by adding the clause throws IOException to the method declaration whenever a method includes a call to the readLine method. public static void main(String [] args) throws IOException {... String input = bufReader.readLine();... }


Download ppt "COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal."

Similar presentations


Ads by Google