1 Simple Input Interactive Input - The Class Scanner n Command-Line Arguments.

Slides:



Advertisements
Similar presentations
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Advertisements

Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() –
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;
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
LAB 10.
Computer Programming Lab(4).
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
Computer Programming Lab(5).
The for-statement. Different loop-statements in Java Java provides 3 types of loop-statements: 1. The for-statement 2. The while-statement 3. The do-while-statement.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
CS1101: Programming Methodology Aaron Tan.
Integer Division What is x? int x = 1729 / 100; Answer: 17.
©Silberschatz, Korth and Sudarshan1 Primitive Types, Strings, and Console I/O n Variables and Expressions The Class String n Keyboard and Screen I/O n.
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
Java TA Session 1. Software Java Runtime Environment (JRE) java.exe Java Development Kit (JDK) java.exe, javac.exe Version 1.6 = Version 6, Version 1.7.
©Silberschatz, Korth and Sudarshan1 Methods Method Basics Parameters Void vs. Non-void Methods Recursion.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
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.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Mixing integer and floating point numbers in an arithmetic operation.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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.
Java Variables, Types, and Math Getting Started Complete and Turn in Address/Poem.
Catie Welsh February 2,  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Office hours: Thursday 1300 – 1400hrs.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Simple algorithms on an array - compute sum and min.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
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.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
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.
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.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
Introduction to programming in java
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
Reading Parameters CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D.
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Chapter 2 Clarifications
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
TemperatureConversion
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Compiling and Running a Java Program
Maha AlSaif Maryam AlQattan
Computer Programming Methodology Input and While Loop
Repetition.
Data types, Expressions and assignment, Input from User
TK1114 Computer Programming
Java 24th sep /19/2018 CFILT.
Something about Java Introduction to Problem Solving and Programming 1.
BIT115: Introduction to Programming
Introduction to Classes and Methods
Java Variables, Types, and Math Getting Started
Java so far Week 7.
class PrintOnetoTen { public static void main(String args[]) {
Introduction to Java Brief history of Java Sample Java Program
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Repetition Statements
Consider the following code:
Presentation transcript:

1 Simple Input Interactive Input - The Class Scanner n Command-Line Arguments

2 Simple Input n Sometimes data is needed and obtained from the user at run time. n Simple keyboard input requires: import java.util.Scanner; or import java.util.*; at the beginning of the file.

3 Simple Input, cont. A “Scanner” object must be initialized before inputting data: Scanner keyboard = new Scanner(System.in); n To input data: int eggsPerBasket; : eggsPerBasket = keyboard.nextInt(); which reads one int value from the keyboard and assigns it to the variable eggsPerBasket.

4 Simple Input, cont. import java.util.Scanner; public class Cows { public static void main (String[] args) { int barns, cowsPer, totalCows; Scanner kb = new Scanner(System.in); System.out.print(“Enter the number of barns: ”); barns = kb.nextInt(); System.out.print(“Enter the number of cows per barn: ”); cowsPer = kb.nextInt(); totalCows = barns * cowsPer; System.out.println(“Total number of cows is: “ + totalCows); System.out.println(“Suppose two cows per barn escape!”); cowsPer = cowsPer – 2; totalCows = barns * cowsPer; System.out.println(“Total number of cows is now: “ + totalCows); } > javac Cows.java > java Cows Enter the number of barns: 10 Enter the number of cows per barn: 6 Total number of cows is: 60 Suppose two cows per barn escape! Total number of cows is now: 40

5 Command-Line Arguments Frequently input is provided to a program at the command-line. public class UseArgument *Notice Scanner is not imported! { public static void main(String[] args) { System.out.print(“Hi, ”); System.out.print(args[0]); System.out.println(“. How are you?”); } } > javac UseArgument.java > java UseArgument Alice Hi, Alice. How are you? > java UseArgument Bob Hi, Bob. How are you?

6 Command-Line Arguments Frequently input is provided to a program at the command-line. public class UseArgument { public static void main(String[] args) { System.out.print(“Hi, ”); System.out.print(args[0]); System.out.println(“. How are you?”); } > javac UseArgument.java > java UseArgument Alice Hi, Alice. How are you? > java UseArgument Bob Hi, Bob. How are you?

7 Command-Line Arguments Frequently input is provided to a program at the command-line. public class UseArgument { public static void main(String[] args) { System.out.print(“Hi, ”); System.out.print(args[0]); System.out.println(“. How are you?”); } > javac UseArgument.java > java UseArgument Alice Hi, Alice. How are you? > java UseArgument Bob Hi, Bob. How are you?

8 Command-Line Arguments Frequently multiple values are provided at the command-line. public class Use3Arguments { public static void main(String[] args) { System.out.print(“The first word is ”); System.out.print(args[0]); System.out.print(“, the second is ”); System.out.print(args[1]); System.out.print(“, and the third is ”); System.out.println(args[2]); } > javac Use3Arguments.java > java Use3Arguments dog cat cow

9 Command-Line Arguments Frequently multiple values are provided at the command-line. public class Use3Arguments { public static void main(String[] args) { System.out.print(“The first word is ”); System.out.print(args[0]); System.out.print(“, the second is ”); System.out.print(args[1]); System.out.print(“, and the third is ”); System.out.println(args[2]); } > javac Use3Arguments.java > java Use3Arguments dog cat cow

10 Command-Line Arguments Frequently multiple values are provided at the command-line. public class Use3Arguments { public static void main(String[] args) { System.out.print(“The first word is ”); System.out.print(args[0]); System.out.print(“, the second is ”); System.out.print(args[1]); System.out.print(“, and the third is ”); System.out.println(args[2]); } > javac Use3Arguments.java > java Use3Arguments dog cat cow The first word is dog, the second is cat, and the third is cow

11 Command-Line Arguments What if you provide integers on the command-line? public class IntOps { public static void main(String[] args) { int sum, prod, quot, rem; sum = args[0] + args[1]; prod = args[0] * args[1]; quot = args[0] / args[1]; rem = args[0] % args[1]; System.out.println(“Sum = " + sum); System.out.println(“Product = " + prod); System.out.println(“Quotient = " + quot); Hoping to see this System.out.println(“Remainder = " + rem); } > javac IntOps.java > java IntOps Sum = 1333 Prod = Quotient = 12 Remainder = 46

12 Command-Line Arguments What if you provide integers on the command-line? public class IntOps { public static void main(String[] args) { int sum, prod, quot, rem; sum = args[0] + args[1]; prod = args[0] * args[1]; quot = args[0] / args[1]; rem = args[0] % args[1]; System.out.println(“Sum = " + sum); System.out.println(“Product = " + prod); System.out.println(“Quotient = " + quot); System.out.println(“Remainder = " + rem); } > javac IntOps.java *** ERROR ***

13 Command-Line Arguments For the same reason, this does NOT work: public class IntOps { public static void main(String[] args) { int a, b, sum, prod, quot, rem; a = args[0]; b = args[1]; sum = a + b; prod = a * b; quot = a / b; rem = a % b; System.out.println(“Sum = " + sum); System.out.println(“Product = " + prod); System.out.println(“Quotient = " + quot); System.out.println(“Remainder = " + rem); } > javac IntOps.java *** ERROR ***

14 Command-Line Arguments So, it looks ugly, but this is what we need to do: public class IntOps { public static void main(String[] args) { int a, b, sum, prod, quot, rem; a = Integer.parseInt(args[0]);// Notice the Integer.parseInt b = Integer.parseInt(args[1]);// Notice the comments…lol sum = a + b; prod = a * b; quot = a / b; rem = a % b; System.out.println(“Sum = " + sum); System.out.println(“Product = " + prod); System.out.println(“Quotient = " + quot); System.out.println(“Remainder = " + rem); } > javac IntOps.java > java IntOps Sum = 1333 Prod = Quotient = 12 Remainder = 46