Reading Information From the User Making your Programs Interactive.

Slides:



Advertisements
Similar presentations
Input review If review Common Errors in Selection Statement Logical Operators switch Statements Generating Random Numbers practice.
Advertisements

CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
1 Introduction to Java Programming Lecture 6 Program Input : BufferedReader & Scanner.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
Variable Scope Brackets, brackets…. brackets. Variable Scope /** * HelloWorld --- program that prints something to the screen. Blanca Polo */
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
***** SWTJC STEM ***** Chapter 3-1 cg 36 Java Class Libraries & API’s A class library is a set of classes that supports the development of programs. Java.
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
1 Scanner objects. 2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The.
Announcements Quiz 2 Grades Posted on blackboard.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
Lecture 10 Instructor: Craig Duckett Lecture 10 is in Lecture 11 Folder.
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
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.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
CSCI S-1 Section 6. Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday,
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Interactive Programs with Scanner. 2 Input and System.in interactive program: Reads input from the console. –While the program runs, it asks the user.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
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.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
Announcements Quiz 1 Next Monday. int : Integer Range of Typically –2,147,483,648 to 2,147,483,647 (machine and compiler dependent) float : Real Number.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
CSCI 1100/1202 January 23, Class Methods Some methods can be invoked through the class name, instead of through an object of the class These methods.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
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.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
Jeopardy $100 VariablesErrorsLoops Classes and Objects Program Structure $200 $300 $400 $500 $400 $300 $200 $100 $500 $400 $300 $200 $100 $500 $400 $300.
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.
© A+ Computer Science - import java.util.Scanner; Try to be as specific as possible when using an import.
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.
Reading Parameters CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D.
Chapter 2 Clarifications
File I/O CLI: File Input CLI: File Output GUI: File Chooser
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Strings and File I/O.
Maha AlSaif Maryam AlQattan
Interactive Standard Input/output
Introduction to Methods in java
Data types, Expressions and assignment, Input from User
TK1114 Computer Programming
Something about Java Introduction to Problem Solving and Programming 1.
I/O Streams- Basics Byte Streams and Character Streams
Operators Laboratory /11/16.
הרצאה 12: קבצים וחריגות (Exceptions)
Introduction to Classes and Methods
L3. Necessary Java Programming Techniques
L3. Necessary Java Programming Techniques
Know for Quiz Everything through Last Week and Lab 7
Differences between Java and JavaScript
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
CSE Module 1 A Programming Primer
CSC1401 Input and Output (with Files)
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Exception Handling Contents
EEC 484/584 Computer Networks
Validation We have covered one way to get user input, using Scanner to get it from the console We will soon cover at least one additional way to get.
Optional Topic: User Input with Scanner
Presentation transcript:

Reading Information From the User Making your Programs Interactive

Classes used for Reading Information Scanner (specific to JAVA 5 and beyond)  Import java.util.*; BufferedReader (has always been there)  Import java.io.*;

Scanner Class -The Methods Constructor  Scanner sc = new Scanner(System.in); Reading a String  String sc.nextLine( ); Reading an int  int sc.nextInt( ); Reading a double  double sc.nextDouble( );

java.util.Scanner -the Steps At the top must import java.util.Scanner; Declare a Scanner object within the variable declaration area:  Scanner scan = new Scanner(System.in); Now you can use it to read information from the user System.in refers to The users’ keyboard input

java.util.Scanner -more Steps Declare a variable to store the input String sName; System.out.print(“What is your name? “ ); sName = scan.nextLine( ); System.out.println(“Nice to meet you “ + sName); System.in refers to The users’ keyboard input

Reading integers with the Scanner class Reading an int number int iAge = 0; Int iYear = 2006; System.out.print(“How old are you? “); iAge = scan.nextInt( ); iYear = iYear - iAge; System.out.println(“\nSo you were born around “ + iYear);

Reading doubles with the Scanner Class Reading a double number final double dGoal = ; //constant double dEarnings = 0; double dMissing = 0; System.out.print(“How much do you make per hour? “); dEarnings = scan.nextDouble( ); dMissing = dGoal - dEarnings; System.out.println(“\nIn one hours you will only need “ + iMissing + “more to earn your goal”);

Scanner Problems See java/ReadInput/ReadWScanner.java

java.io.BufferedReader The BufferedReader class can only read Strings However we can transform those Strings into numbers of any kind. This one is always safe.

Two Objects and one Exception In the class:  public static void main(String args[ ]) throws IOException { InputStreamReader isr = new InputStreamReader (System.in); BufferedReader br = new BufferedReader(isr); The method:  String readLine( )

Reading a String with BufferedReader InputStreamReader isr = new InputStreamReader (System.in); BufferedReader br = new BufferedReader(isr); String sName; System.out.print(“What is your name? “); sName = br.readLine( ); System.out.println(“So your name is “ + sName);

Reading integers with BufferedReader String sPlaceHolder; int iAge =0; int iYear = 2006; System.out.print(“How old are you? “); sPlaceHolder = br.readLine( ); iAge = Integer.parseInt(sPlaceHolder); System.out.println(“So you are “ + iAge + “ years old”); System.out.println(“you were probably born in “+ (iYear - iage));

Reading doubles with BufferedReader Reading a double number final double dGoal = ; //constant double dEarnings = 0; double dMissing = 0; System.out.print(“How much do you make per hour? “); sPlaceHolder = br.readLine( ) dEarnings = Double.parseDouble(sPlaceHolder); dMissing = dGoal - dEarnings; System.out.println(“\nIn one hours you will only need “ + iMissing + “more to earn your goal”);

Questions? Java/ReadInput/ReadWBufReader.java Java/ReadInput/ReadWScanner.java