Chapter 3 Java Input/Output.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Formatting Output For a pleasant interaction with the user.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Evan Korth Scanner class Evan Korth NYU. Evan Korth Java 1.5 (5.0)’s Scanner class Prior to Java 1.5 getting input from the console involved multiple.
Scanner Pepper With credits to Dr. Siegfried. The Scanner Class Most programs will need some form of input. At the beginning, all of our input will come.
Chapter 2: Java Fundamentals Input and Output statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
Using Classes BCIS 3680 Enterprise Programming. Overview 2  Using Classes  Using premade classes for input and output  Display output: System, JOptionPane.
Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
Chapter 2 Elementary Programming
Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
Chapter 3 Java Input/Output.
Using Java Class Library
Basics and arrays Operators:  Arithmetic: +, -, *, /, %  Relational:, >=, ==, !=  Logical: &&, ||, ! Primitive data types Byte(8), short(16), int(32),
Dialog Boxes.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
CSC 142 H 1 CSC 142 Standard input/output. CSC 142 H 2 Console Ouput  Use the static PrintStream object out in System System.out.println("Hello, world");
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Data Types – Reference Types Objective To understand what reference types are The need to study reference types To understand Java standard packages To.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
Keyboard and Screen I/O (Input/Output). Screen Output  System.out is an object that is part of the Java language. (Don’t worry yet about why there is.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Console Input and Output
TemperatureConversion
Outline Creating Objects The String Class Packages Formatting Output
Data Types – Reference Types
Unit 2 Elementary Programming
Elementary Programming
Lecture Notes – Basics (Ch1-6)
Interactive Standard Input/output
Dialogues and Wrapper Classes
Console Input and Output
Chapter 2 Elementary Programming
INPUT STATEMENTS GC 201.
Chapter 2 Elementary Programming
Program Style Console Input and Output
الكلية الجامعية للعلوم التطبيقية
Introduction to Classes and Methods
A+ Computer Science INPUT.
Unit 1: Intro Lesson 3: Input.
Chapter 3 Input/Output.
Chapter 2: Java Fundamentals
A+ Computer Science INPUT.
Object Oriented Programming
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Math class services (functions)
JOptionPane class.
Unit 1: Intro Lesson 4: Output.
Presentation transcript:

Chapter 3 Java Input/Output

Objectives Apply formats (currency and percentages) to output. Use keyboard input in Java program to create interactivity. Use dialog box input/output in Java program to create interactivity. Associate import statements with corresponding input/output/format classes/packages.

Scanner Class Input data from the Java console: Note: In order to use this class, you must use the following import statement: import java.util.Scanner; Value Returned Method Returns… byte nextByte() Input as a byte. short nextShort() Input as a short. int nextInt() Input as an integer. long nextLong() Input as a long. float nextFloat() Input as a float. double nextDouble() Input as a double. boolean nextBoolean() Input as a boolean. String next() Next word as a String. nextLine() Input line as a String.

Scanner Age Example:

Scanner Addition Example:

Input Dialog Boxes An input dialog box asks a question and uses a box for entering a response. String response = JOptionPane.showInputDialog(null, "Enter Fahrenheit"); If this data is to be used in a calculation, it will need to be converted from String data to double data or integer data with one of the following statements: double fahrenheit = Double.parseDouble(response); Note: In order to use this class, you must use the following import statement: import javax.swing.JOptionPane;

Message Dialog Boxes Uses a simple window to display (output) information. JOptionPane.showMessageDialog(null, "Fahrenheit: " + fahrenheit + "\nCelsius: " + celsius); Note: In order to use this class, you must use the following import statement: import javax.swing.JOptionPane;

JOptionPane Addition Example

JOptionPane Addition Output

NumberFormat Class Used to format output as currency.   Currency Example:  NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance( ); System.out.println(currencyFormatter.format(20.5) ); Output: $20.50 Note: In order to use this NumberFormat class, you must use the import statement: import java.text.NumberFormat;

DecimalFormat Class Used to format output with a specific number of digits before and after the decimal point.   double fahrenheit = 212.5; DecimalFormat patternFormatter = new DecimalFormat ("#,###.00"); System.out.println(“The temperature is “ + patternFormatter.format(fahrenheit));  Output: The temperature is 212.50 Note: In order to use this NumberFormat class, you must use the following import statement: import java.text.DecimalFormat; Character Meaning Required digit. Print 0 if there isn’t a digit in this place. # Optional digit. Leave blank if there isn’t a digit in this place. . Decimal point separator. , Comma separator. % Multiplies the result by 100 and displays a percent sign.