Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.

Slides:



Advertisements
Similar presentations
Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.
Advertisements

Computer Programming Lab 8.
More Java Syntax. Scanner class Revisited The Scanner class is a class in java.util, which allows the user to read values of various types. There are.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Today’s topics: I/O (Input/Output). Scribbler Inputs & Outputs  What are the Scribbler’s inputs and outputs?  reset button  motors/wheel  light sensor.
©2004 Brooks/Cole Chapter 3 Interactive Input. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Standard Input and Output System.in Is Used to.
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.
Chapter 2: Java Fundamentals Input and Output statements.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
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.
Using java’s Scanner class To read from input and from a file. (horstmann ch04 and ch 17)
Computer Programming Lab(5).
November 10, 2005Input and Output1 of 26 Input and Output String I/O Console I/O File I/O.
Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
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.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
File I/O (Input and Output). Why use files? Things stored in files are more permanent than things stored in variables in programs. Things stored in files.
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 Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
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.
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 
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.
Lecture 8: Bits and Pieces Tami Meredith. Roadmap Today's lecture has a bit of everything. Going back over previous chapters and pulling out little bits.
Lecture 2: 1226 Review Tami Meredith. Programming Process How do we fill in the yellow box? Text Editor Compiler (javac) Interpreter (JVM: java) User.
Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.6 Reading Input with java.io Produced by Harvey.
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[]
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
Simple Console Output CS 21a. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Introduction to programming in java
Streams & File Input/Output (I/O)
Input/Output.
Console Input and Output
TemperatureConversion
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Yanal Alahmad Java Workshop Yanal Alahmad
Maha AlSaif Maryam AlQattan
Interactive Standard Input/output
I/O Basics.
OUTPUT STATEMENTS GC 201.
INPUT STATEMENTS GC 201.
CSS161: Fundamentals of Computing
Program Style Console Input and Output
System.out.println for console output
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Introduction to Classes and Methods
MSIS 655 Advanced Business Applications Programming
Introduction to Computing Using Java
Chapter 2: Java Fundamentals
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
CS 1054 Introduction to Programming in Java
Presentation transcript:

Miscellaneous topics Chapter 2 Savitch

Miscellaneous topics Standard output using System.out Input using Scanner class

The standard System class System contains a number of interesting static methods. System.exit( 0 ); System.exit( 0 ); Terminates our program immediately long start = System.currentTimeMillis(); long start = System.currentTimeMillis(); Useful for timing programs System.out and System.in System.out and System.in The standard output and input streams See a/lang/System.html a/lang/System.html a/lang/System.html

Output and System.out

System.out Static member (not a method) Type is PrintStream See PrintStream.html See PrintStream.html PrintStream.html PrintStream.html Useful PrintStream methods: print() print() println() println() printf() printf()

print() and println() print() prints its argument println() prints its argument followed by a newline Ex. int i=12; String s = “sally”; System.out.println( i ); System.out.print( s + “ is “ + i + “ years old.” ); System.out.println();

printf and rounding What is the output of the following: class Test { public static void main ( String args[] ) { public static void main ( String args[] ) { double d2 = ; double d2 = ; System.out.printf( "%f %.2f \n\n", d2, d2 ); System.out.printf( "%f %.2f \n\n", d2, d2 ); }}

printf and rounding What is the output of the following: class Test { public static void main ( String args[] ) { public static void main ( String args[] ) { double d2 = ; double d2 = ; System.out.printf( "%f %.2f \n\n", d2, d2 ); System.out.printf( "%f %.2f \n\n", d2, d2 ); }}

printf() Format string examples: d = ; System.out.printf( “%010.2f %n”, d ); d = ; System.out.printf( “%010.2f %n”, d );

printf() Useful for formatting other data types as well. int i = 22; System.out.printf( "%d %n", i ); System.out.printf( "%6d %n", i ); System.out.printf( "%06d %n", i ); System.out.printf( "%-6d %n", i ); System.out.printf( "%x %n", i );

printf() Useful for Strings too. String s = "sally"; System.out.printf( "%s eats here.%n", s ); System.out.printf( "%12s eats here.%n", s ); //exception thrown: //System.out.printf( "%012s eats here.%n", s ); System.out.printf( "%-12s eats here.%n", s );

More OO alternatives to printf() NumberFormatDecimalFormat

Input, System.in, and the Scanner class

Scanner class Note that System.in is of type InputStream (see a/io/InputStream.html). a/io/InputStream.html a/io/InputStream.html And the InputStream can only read arrays of bytes! How can we read ints, floats, doubles, etc.? Use the Scanner class.

Scanner class import java.util.Scanner; class Tester { public static void main ( String params[] ) { Scanner kbd = new Scanner( System.in ); …}}

Scanner class Scanner kbd = new Scanner( System.in ); System.out.println( “Enter weight and age” ); int weight = kbd.nextInt(); int age = kbd.nextInt(); System.out.println( “Thanks.” );

Some useful Scanner class methods nextInt()nextLong()nextByte()nextShort()nextDouble()nextFloat()hasNextInt()hasNextLong()hasNextByte()hasNextShort()hasNextDouble()hasNextFloat()

More useful scanner class methods nextBoolean() (and hasNextBoolean()) Response should be either true or false Response should be either true or false next() (and hasNext()) Returns a string of the next characters up to but not including the next whitespace character Returns a string of the next characters up to but not including the next whitespace character nextLine() (and hasNextLine()) Returns a string of the next characters upt to but not including the next newline character Returns a string of the next characters upt to but not including the next newline character

Exercises on p. 91