© A+ Computer Science - www.apluscompsci.com. import java.util.Scanner; Try to be as specific as possible when using an import.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
LAB 10.
Computer Programming Lab(5).
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
Starting Out with Java: From Control Structures through Objects
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 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.
Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner.
Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure.
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.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
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.
1 CSC 201: Computer Programming I Lecture 2 B. S. Afolabi.
Dialog Boxes.
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 4 – Scanner & Style. Console Output The console that starts a Java application is typically known as the standard output device. The standard.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Ihsan Ayyub Qazi Introduction to Computer Programming Recitation 3 Ihsan Ayyub Qazi.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
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.
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
© A+ Computer Science - Scanner frequently used methods NameUse nextInt()returns the next int value nextDouble()returns the next.
Introduction to Computer Science and Object-Oriented Programming
Lecture 4 – Scanner & Style
Input/Output.
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.
Chapter 2 More on Math More on Input
TemperatureConversion
Objectives You should be able to describe: Interactive Keyboard Input
Interactive Standard Input/output
Chapter 3 Java Input/Output.
Something about Java Introduction to Problem Solving and Programming 1.
INPUT STATEMENTS GC 201.
Program Style Console Input and Output
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Computers & Programming Languages
Introduction to Classes and Methods
A+ Computer Science INPUT.
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Chapter 3 Input/Output.
Chapter 2: Java Fundamentals
© A+ Computer Science - Classes And Objects © A+ Computer Science -
A+ Computer Science INPUT.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Data Types and Maths Programming Guides.
Math class services (functions)
Input, Variables, and Mathematical Expressions
Presentation transcript:

© A+ Computer Science -

import java.util.Scanner; Try to be as specific as possible when using an import.

© A+ Computer Science - Scanner keyboard = new Scanner(System.in); reference variable object instantiation

© A+ Computer Science -

Scanner frequently used methods NameUse nextInt()returns the next int value nextDouble()returns the next double value nextFloat()returns the next float value nextLong()returns the next long value nextByte()returns the next byte value nextShort()returns the next short value next()returns the next one word String nextLine()returns the next multi word String import java.util.Scanner;

© A+ Computer Science - Scanner keyboard = new Scanner(System.in); out.print("Enter an integer :: "); int num = keyboard.nextInt();

© A+ Computer Science - out.print("Enter an integer :: "); int num = keyboard.nextInt(); out.println(num); OUTPUT Enter an integer :: INPUT 931

© A+ Computer Science - int num = keyboard.nextInt(); reference variable method call

© A+ Computer Science - out.print("Enter an integer :: "); Prompts are used to tell the user what you want.

© A+ Computer Science -

Scanner keyboard = new Scanner(System.in); out.print("Enter a double :: "); double num = keyboard.nextDouble();

© A+ Computer Science - out.print("Enter a double :: "); double num = keyboard.nextDouble(); out.println(num); OUTPUT Enter a double :: INPUT 34.33

© A+ Computer Science - double num = keyboard.nextDouble(); reference variable method call

© A+ Computer Science -

Scanner keyboard = new Scanner(System.in); out.print("Enter a string :: "); String word = keyboard.next();

© A+ Computer Science - out.print("Enter a string :: "); String word = keyboard.next(); out.println(word); OUTPUT Enter a string :: I love java. I INPUT I love java.

© A+ Computer Science - Scanner keyboard = new Scanner(System.in); out.print("Enter a sentence :: "); String sentence = keyboard.nextLine();

© A+ Computer Science - out.print("Enter a line :: "); String line = keyboard.nextLine(); out.println(line); OUTPUT Enter a line :: I love java. I love java. INPUT I love java.

© A+ Computer Science -

out.print("Enter an integer :: "); int num = keyboard.nextInt(); out.print("Enter a sentence :: "); String sentence = keyboard.nextLine(); out.println(num + " "+sentence); OUTPUT Enter an integer :: 34 Enter a sentence :: 34 INPUT 34 picks up \n nextLine() picks up whitespace.

© A+ Computer Science - out.print("Enter an integer :: "); int num = keyboard.nextInt(); keyboard.nextLine(); //pick up whitespace out.print("Enter a sentence :: "); String sentence = keyboard.nextLine(); out.println(num + " "+sentence); OUTPUT Enter an integer :: 34 Enter a sentence :: picks up \n 34 picks up \n INPUT 34 picks up \n nextLine() picks up whitespace.

© A+ Computer Science -

Scanner keyboard = new Scanner(System.in); out.println(keyboard.nextInt()); OUTPUT INPUT

© A+ Computer Science -

BufferedReader keyboard = new BufferedReader( new InputStreamReader( System.in ) ); System.out.print("Enter a word :: "); String s = keyboard.readLine(); System.out.println(s + '\n' );

© A+ Computer Science - readLine() reads in all data as text / string data. The text you read in must be converted over to the appropriate type before it can be stored. System.out.print("Enter an integer :: "); one = Integer.parseInt(keyboard.readLine()); System.out.print("Enter a double :: "); two = Double.parseDouble(keyboard.readLine());

© A+ Computer Science -

//GUI INPUT BOX input= JOptionPane.showInputDialog("Enter an integer :: "); one = Integer.parseInt(input); //GUI OUTPUT BOX JOptionPane.showMessageDialog(null, "Integer value :: " + one);

© A+ Computer Science -