Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.

Slides:



Advertisements
Similar presentations
Introduction to Programming Overview of Week 2 25 January Ping Brennan
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Class attributes Chapter 3: Introduction to Classes and Objects.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data.
Chapter 2: Java Fundamentals Input and Output statements.
Chapter 2: Java Fundamentals Java Program Structure.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
JAVA Control Statement.
Computer Programming Lab(4).
CS0007: Introduction to Computer Programming Scope, Comments, Code Style, Keyboard Input.
Computer Programming Lab(5).
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)
Input & Output In Java. Input & Output It is very complicated for a computer to show how information is processed. Although a computer is very good at.
Chapter 2 Elementary Programming
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 3: Numerical Data Manipulating Numbers Variables.
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.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
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.
Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Introduction to Programming
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Using Java Class Library
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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.
Java Variables, Types, and Math Getting Started Complete and Turn in Address/Poem.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Strings and I/O. Lotsa String Stuff… There are close to 50 methods defined in the String class. We will introduce several of them here: charAt, substring,
Classes and Objects. Object Software objects are modeled after real-world objects in that they, too, have state and behavior. A software object maintains.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
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.
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
Classes and Objects. Object vs. Class Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2  A class could be considered as a set of objects having.
Introduction to programming in java
M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.
User Input ICS2O.
Chapter 2 Clarifications
CSC111 Quick Revision.
Input/Output.
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Elementary Programming
Maha AlSaif Maryam AlQattan
Chapter 3 Java Input/Output.
Data types, Expressions and assignment, Input from User
INPUT STATEMENTS GC 201.
Scope, Comments, Code Style, Keyboard Input
Program Style Console Input and Output
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Introduction to Classes and Methods
A+ Computer Science INPUT.
Java Variables, Types, and Math Getting Started
Fundamentals 2.
Chapter 3 Input/Output.
Chapter 2: Java Fundamentals
A+ Computer Science INPUT.
CS2011 Introduction to Programming I Elementary Programming
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

Java Fundamentals 3 Input and Output statements

Standard Output Window Using System.out, we can output multiple lines of text to the standard output window. Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2 The exact style of standard output window depends on the Java tool you use.

The println Method We use println instead of print to skip a line. Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 3 int x = 123, y = x + x; System.out.print( " x = “ ); System.out.println( x ); System.out.print( " x + x = “ ); System.out.println( y ); System.out.println( " THE END“ ); x = 123 x + x = 246 THE END

Standard Input To input primitive data values, we use the Scanner class. 4 steps are needed to be able to use input primitive: Step 1: import the Scanner class: import Java.util.Scanner; Step 2 : declaring a reference variable of a Scanner Scanner read ; //we named the object read Step 3: creating an instance of the Scanner read = new Scanner (System.in); Step 4: use specific methods to enter data int x = read.nextInt(); Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 4

Example Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 5 import java.util.Scanner; public class TestInput { public static void main(String[] args) { Scanner input ; int area,length, width; input = new Scanner (System.in); // creating an instance System.out.println("enter the length "); length = input.nextInt(); //reading the length from the keyboard System.out.println("Enter the Width "); width = input.nextInt(); //reading the width from the keyboard area = length * width ; System.out.println("the length is "+ length); System.out.println("the width is "+ width); System.out.println("the area is "+ area); }

Output enter the length 2 Enter the Width 3 the length is 2 the width is 3 the area is 6 Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 6

Common Scanner Methods MethodExample Scanner input = new Scanner (System.in); nextByte( )byte b = input.nextByte( ); nextDouble( )double d = input.nextDouble( ); nextFloat( )float f = input.nextFloat( ); nextInt( )int i = input.nextInt( ); nextLong( )long l = input.nextLong( ); nextShort( )short s = input.nextShort( ); next() String str = input.next(); Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 7