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.

Slides:



Advertisements
Similar presentations
Computer Programming Lab(7).
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter2: Introduction to Java Applications 1.
Dialogs. Displaying Text in a Dialog Box Windows and dialog boxes –Up to this our output has been to the screen –Many Java applications use these to display.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs Lecture 3-3: Interactive Programs w/
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
IC211 Lecture 8 I/O: Command Line And JOptionPane.
Begin Java having used Alice Pepper - Some slides from Alice in Action with Java.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
LAB 10.
Introduction to Java Java SE 8 for Programmers Paul Deitel Harvey Deitel Deitel Developer Series 2014.
Computer Programming Lab(5).
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
Integer Division What is x? int x = 1729 / 100; Answer: 17.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.
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.
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.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Java Variables, Types, and Math Getting Started Complete and Turn in Address/Poem.
Programming Fundamentals I Java Programming Spring 2009 Instructor: Xuan Tung Hoang TA: Tran Minh Trung Lab 03.
Review :chapters What is an algorithm? A step by step description of how to accomplish a task. For example, Adding 3 numbers N1, N2 and N3 Add.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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");
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
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,
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
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.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
AP Java 10/1/2015. public class Rolling { public static void main( String [] args) public static void main( String [] args) { int roll; int roll; for.
Introduction to programming in java
CompSci 230 S Programming Techniques
Chapter 2 Clarifications
CSC111 Quick Revision.
Java Programming Lecture 2
Input/Output.
CSC305: COMPUTER PROGRAMMING II (JAVA)
Yanal Alahmad Java Workshop Yanal Alahmad
Maha AlSaif Maryam AlQattan
Data types, Expressions and assignment, Input from User
MSIS 655 Advanced Business Applications Programming
AP Java Review If else.
AP Java 10/4/2016.
Introduction to Java Applications
Object Oriented Programming
AP Java Review If else.
Building Java Programs
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Optional Topic: User Input with Scanner
Presentation transcript:

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 using println: System.out.println(“One”); System.out.println(“Two”); System.out.println(“Buckle My Shoe”); or System.out.println(“One\nTwo\nBuckle My Shoe”);

Output Continued Examples using print & println System.out.print(“One “); System.out.print(“Two “); System.out.print(“Buckle my shoe”); System.out.println();

Output Continued Examples using + (the concatenate operator) int x = 10, y = 20; System.out.println (“x =“ + x + “\ny=“ + y + “\nx + y = “ + (x + y)); What would be output if the parenthesis around x + y were left off?

Using printf System.out.printf() is a method that receives multiple arguments. The first argument is a string that tells how to print the arguments that follow. Example: %s means print the argument as a string System.out.printf(“%s\n%s\n%s\n”, “One”, “Two”, “Buckle my shoe”);

printf continued int number1, number2, sum; number1=10; number1=30; sum = number1 + number2; System.out.printf(“The sum of %d + %d = %d”, number1, number2, sum);

Command line Input The Scanner class is used for input from the command line. The Scanner class is in the package java.util so any program using the Scanner class must first include an import statement that is used to help the compiler locate the class. All import statements appear first in a java program before the class definition or any other statement (other than comments).

import java.util.Scanner; public class AddTwoNumbers { public static void main(String[] str) { int x, y; //create a Scanner object to get input //System.in refers to standard input object Scanner input = new Scanner(System.in); //Prompt for and read the first integer //using the Scanner object’s nextInt method. //The program waits for the user to type in //an integer and press the ENTER key System.out.println(“Enter the first integer:”); x = input.nextInt();

System.out.println (“Enter the next integer:”); y = input.nextInt(); sum = x + y; System.out.println (“Sum is %d\n”, sum); } //end of main } //end of class AddTwoNumbers

Displaying output using JOptionPane JOptionPane is a class in the package javax.swing that allows output to be displayed in a dialog box. Example: JOptionPane.showMessageDialog(null, “Welcome\nto\nJava”); produces:

String name = JOptionPane.showInputDialog( “What is your name?”); String message = “Welcome, to Java Programming! “ + name; JOptionPane.showMessageDialog(null, message);