Java Programming Lecture 2

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Introduction to Objects and Input/Output
Chapter 2: Using Data.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Java Programming, 3e Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
COMP 14 Introduction to Programming Miguel A. Otaduy May 17, 2004.
COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 29, 2005.
Class Decimal Format ► Import package java.text ► Create DecimalFormat object and initialize ► Use method format ► Example: import java.text.DecimalFormat.
Introduction to Computers and Programming Lecture 4: Mathematical and Relational Operators.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Chapter 2 - Introduction to Java Applications
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Chapter 3b Standard Input and Output Sample Development.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
Chapter 3: Introduction to Objects and Input/Output.
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
Java Programming, 2E Introductory Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CIS 260: App Dev I. 2 Objects and Reference Variables n Predefined Java classes you have used:  String —for defining and manipulating strings  Integer.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
VARIABLES Introduction to Computer Science 1- COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Using Classes BCIS 3680 Enterprise Programming. Overview 2  Using Classes  Using premade classes for input and output  Display output: System, JOptionPane.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Chapter 2 Elementary Programming
Java Programming: From Problem Analysis to Program Design, 3e Chapter 3 Introduction to Objects and Input/Output.
Files. Advantages of files Can edit data, prepare ahead of time Can rerun file without reentering data Can examine output at leisure Can display output.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Chapter 2 – Continued Basic Elements of Java. Chapter Objectives Type Conversion String Class Commonly Used String Methods Parsing Numeric Strings Commonly.
File Input/Output. 2Java Programming: From Problem Analysis to Program Design, 3e File Input/Output File: area in secondary storage used to hold information.
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
Chapter 2: Java Fundamentals Type conversion,String.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Java Fundamentals 5. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Objectives ► Become familiar with the class String ► ► Learn how to use input and output dialog boxes in a program ► ► Learn how to tokenize the input.
Lecture 2 Objectives Learn about objects and reference variables.
Dialog Boxes.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 3 Introduction to Objects and Input/Output.
Java Fundamentals Part Integer Division Division can be tricky. In a Java program, what is the value of 1/2? You might think the answer is.
InterestRate Create an InterestRate class and InterestRateViewer client class to do the following: A person is purchasing an item with their credit card.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
JAVA 1.COMPARISON: JAVA AND C++ 2.KEYBOARD INPUT 3.OUTPUT 4.CONVERSION FROM STRING 5.OPERATORS AND EXPRESSIONS 6.DIALOG BOX – USER PROMPT January
Chapter 3: Introduction to Objects and Input/Output
Java Fundamentals 4.
File Input / Output.
Streams & File Input/Output (I/O)
Outline Creating Objects The String Class Packages Formatting Output
Objectives You should be able to describe: Interactive Keyboard Input
2.5 Another Java Application: Adding Integers
Java Programming: From Problem Analysis to Program Design, 4e
Message, Input, Confirm, and Specialized Dialogs
Chapter 3: Introduction to Objects and Input/Output
Chapter 2 - Introduction to Java Applications
Chapter 2: Basic Elements of Java
Chapter 3: Introduction to Objects and Input/Output
Message, Input, and Confirm Dialogs
Object Oriented Programming
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
JOptionPane class.
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

Java Programming Lecture 2 Input /Output

Input/Output Parsing Numeric Strings I/O System and GUI Read From and Write to Files

Parsing Numeric Strings A string consisting of only integers or decimal numbers is called a numeric string 1. To convert a string consisting of an integer to a value of the type int, we use the following expression: Integer.parseInt(strExpression) Integer.parseInt("6723") = 6723 Integer.parseInt("-823") = -823

Parsing Numeric Strings (continued) 2. To convert a string consisting of a decimal number to a value of the type float, we use the following expression: Float.parseFloat(strExpression) Float.parseFloat("34.56") = 34.56 Float.parseFloat("-542.97") = -542.97

Parsing Numeric Strings (continued) 3. To convert a string consisting of a decimal number to a value of the type double, we use the following expression: Double.parseDouble(strExpression) Double.parseDouble("345.78") = 345.78 Double.parseDouble("-782.873") = -782.873

Parsing Numeric Strings (continued) Integer, Float, and Double are classes designed to convert a numeric string into a number These classes are called wrapper classes parseInt is a method of the class Integer, which converts a numeric integer string into a value of the type int

Parsing Numeric Strings (continued) parseFloat is a method of the class Float and is used to convert a numeric decimal string into an equivalent value of the type float parseDouble is a method of the class Double, which is used to convert a numeric decimal string into an equivalent value of the type double

Using Dialog Boxes for Input/Output Use a graphical user interface (GUI) class JOptionPane Contained in package javax.swing Contains methods: showInputDialog and showMessageDialog Syntax: str = JOptionPane.showInputDialog(strExpression) Program must end with System.exit(0);

Getting Input from Input Dialog Boxes String string = JOptionPane.showInputDialog( null, “Prompt Message”, “Dialog Title”, JOptionPane.QUESTION_MESSAGE));

Convertting Strings to Integers The input returned from the input dialog box is a string. If you enter a numeric value such as 123, it returns “123”. To obtain the input as a number, you have to convert a string into a number.   To convert a string into an int value, you can use the static parseInt method in the Integer class as follows: int intValue = Integer.parseInt(intString); where intString is a numeric string such as “123”.

Convertting Strings to Doubles To convert a string into a double value, you can use the static parseDouble method in the Double class as follows:   double doubleValue =Double.parseDouble(doubleString); where doubleString is a numeric string such as “123.45”.

JOptionPane Example

Parameters for the Method showMessageDialog

JOptionPane Options for the Parameter messageType

File Input/Output File: area in secondary storage used to hold information You can also initialize a Scanner object to input sources other than the standard input device by passing an appropriate argument in place of the object System.in. We make use of the class FileReader.

File Input/Output (continued) Suppose that the input data is stored in a file, say prog.dat, and this file is on the floppy disk A The following statement creates the Scanner object inFile and initializes it to the file prog.dat Scanner inFile = new Scanner (new FileReader("prog.dat"));

File Input/Output (continued) Next, you use the object inFile to input data from the file prog.dat just the way you used the object console to input data from the standard input device using the methods next, nextInt, nextDouble, and so on

File Input/Output (continued)

File Input/Output (continued) Java file I/O process 1. Import necessary classes from the packages java.util and java.io into the program 2. Create and associate appropriate objects with the input/output sources 3. Use the appropriate methods associated with the variables created in Step 2 to input/output data 4. Close the files

File Input/Output (continued) Example Suppose an input file, say employeeData.txt, consists of the following data: Emily Johnson 45 13.50 Scanner inFile = new Scanner (new FileReader("employeeData.txt")); String firstName; String lastName; double hoursWorked; double payRate; double wages; firstName = inFile.next(); lastName = inFile.next(); hoursWorked = inFile.nextDouble(); payRate = inFile.nextDouble(); wages = hoursWorked * payRate; inFile.close(); //close the input file

Storing (Writing) Output to a File To store the output of a program in a file, you use the class PrintWriter Declare a PrintWriter variable and associate this variable with the destination Suppose the output is to be stored in the file prog.out on floppy disk A

Storing (Writing) Output to a File (continued) Consider the following statement: PrintWriter outFile = new PrintWriter("prog.out"); This statement creates the PrintWriter object outFile and associates it with the file prog.out You can now use the methods print, println, printf, and flush with outFile just the same way they have been used with the object System.out

Storing (Writing) Output to a File (continued) The statement: outFile.println("The paycheck is: $" + pay); stores the output—The paycheck is: $565.78—in the file prog.out -This statement assumes that the value of the variable pay is 565.78

Storing (Writing) Output to a File (continued) Step 4 requires closing the file; you close the input and output files by using the method close inFile.close(); outFile.close(); Closing the output file ensures that the buffer holding the output will be emptied, that is, the entire output generated by the program will be sent to the output file

throws clause During program execution, various things can happen; for example, division by zero or inputting a letter for a number In such cases, we say that an exception has occurred. If an exception occurs in a method, the method should either handle the exception or throw it for the calling environment to handle If an input file does not exist, the program throws a FileNotFoundException

throws clause (continued) If an output file cannot be created or accessed, the program throws a FileNotFoundException For the next few chapters, we will simply throw the exceptions Because we do not need the method main to handle the FileNotFoundException exception, we will include a command in the heading of the method main to throw the FileNotFoundException exception

Skeleton of I/O Program

Programming Example: Give the number of cents to define how many dollars quarters cents and dimes are there. Multiplication Tables