Input/Output

advertisement
Today’s topics:
I/O (Input/Output)
Scribbler Inputs & Outputs
 What are the
Scribbler’s inputs and
outputs?







reset button
motors/wheel
light sensor
speaker
LEDs
stall sensor
obstacle sensor =
emitter + detector
 line sensor
Scribbler Inputs
Scribbler Outputs
Is the serial port an input, an
output, or both?
What outputs does our
computer have?
What outputs does our
computer have?
1. write / print / draw to
monitor / screen /
display
2. speaker for sound
3. write information to
disks (hard, CD, and
DVD)
4. write information to
network interface
card (NIC)
What outputs does our
computer have?
How do we perform
output to the display
in Java?
What outputs does our
computer have?
How do we perform
output to the display
in Java?
 System.out.print
 System.out.println
What inputs does our computer
have?
What inputs does our computer
have?
1. mouse (movement
and buttons)
2. keyboard
3. read information
from disks (hard,
CD, and DVD)
4. read information
from network
interface card (NIC)
What inputs does our computer
have?
We will now focus on
reading information
from the keyboard.
Input/Output
 Input from the keyboard
import java.util.Scanner;
…
Scanner in = new Scanner( System.in );
//wait until the user types something and
// presses the return/enter key.
in.nextLine();
Input/Output
 An I/O example
//declare variable(s)
Scanner in = new Scanner( System.in );
//perform some i/o
System.out.println( "hello. Press 'return' to continue." );
in.nextLine();
System.out.println( "That’s all. Press 'return' again." );
in.nextLine();
//finish up
in.close();
Input/Output
 Another I/O example
//this program echoes what you type
//declare variables
Scanner in = new Scanner( System.in );
//perform some i/o
System.out.print( "Enter something & press 'return' to continue." );
String what = in.nextLine();
System.out.print( "You typed: " + what + ". Press 'return' again." );
in.nextLine();
//finish up
in.close();
String concatenation. Example of operator
overloading.
String input
 We’ve already discussed nextLine().
 Reads an entire line (including blanks and/or
tabs) up to but not including return/enter.
 Additionally, we have next().
 Skips leading whitespace (blanks) including
return.
 In effect, next() reads words including
punctuation.
 Skips consecutive blanks (if any).
 Note: “blanks” include space, tab, and
return/newline.
String input
String s;
Scanner in = new Scanner( System.in );
System.out.print( "Enter input: " );
s = in.nextLine();
You enter:
<sp>Oh!<tab>That is terrible!<sp><return>
What is the value of s?
String input
String s;
Scanner in = new Scanner( System.in );
System.out.print( "Enter input: " );
s = in.nextLine();
You enter:
<sp>Oh!<tab>That is terrible!<sp><return>
What is the value of s?
s = “<sp>Oh!<tab>That is terrible!<sp>”
String input
String s1, s2, s3;
Scanner in = new Scanner( System.in );
System.out.print( "Enter input: " );
s1 = in.next ();
s2 = in.next();
s3 = in.nextLine();
You enter:
<sp><sp>Oh!<tab>That is terrible!<sp><sp><return>
What are the values of s1, s2, and s3?
String input
String s1, s2, s3;
Scanner in = new Scanner( System.in );
System.out.print( "Enter input: " );
s1 = in.next ();
s2 = in.next();
s3 = in.nextLine();
You enter:
<sp><sp>Oh!<tab>That is terrible!<sp><sp><return>
What are the values of s1, s2, and s3?
s1 = “Oh!”
s2 = “That” s3 = “<sp>is terrible!<sp><sp>”
Reading Strings
Note:
When reading from the keyboard (not from a
file), you must type in your string (or
strings) and then you must hit <enter>.
String input summary
2 methods (associated w/ string input):
1. next()
–
–
Skips leading blanks, tabs, and newlines.
Reads a string of letters, digits, and underscores.
2. nextLine()
–
–
Doesn’t skip leading blanks and tabs.
reads an entire line up to but not including \n
(newline)
Input/Output
 Can we read other things (ints, doubles)
besides strings?
Input/Output
 Can we read other things (ints, doubles) besides
strings? Yes.
//calculate F = M A
//get values
System.out.print( "enter mass: " );
double mass = in.nextDouble();
System.out.print( "enter acceleration: " );
double acc = in.nextDouble();
//calculate result
double force = mass * acc;
System.out.println( "force = " + force );
Input/Output
 Can we read other things (ints, doubles) besides
strings? Yes.
//calculate F = M A
Can you convert this
//get values
System.out.print( "enter mass: " ); to use ints instead of
double mass = in.nextDouble(); doubles?
System.out.print( "enter acceleration: " );
double acc = in.nextDouble();
//calculate result
double force = mass * acc;
System.out.println( "force = " + force );
Input/Output
 Can we read other things (ints, doubles) besides
strings? Yes.
//calculate F=MA
Can you convert this
//get values
System.out.print( "enter mass: " ); to use ints instead of
doubles?
int mass = in.nextInt();
System.out.print( "enter acceleration: " );
int acc = in.nextInt();
//calculate result
int force = mass * acc;
System.out.println( "force = " + force );
Complete program “skeleton” or
“template.”
Java is an object oriented programming language. All code must be
associated with an object. We declare an object as follows:
/*
introductory comment(s)
*/
import java.util.Scanner;
// comment(s) about this class.
public class Assignment5 {
…
//comment(s) about this method (function).
public static void main ( String args[] ) {
Scanner in = new Scanner( System.in );
System.out.println( "Computer is ready." );
…
} //end main
…
} //end class Assignment5 definition
Download