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.

Slides:



Advertisements
Similar presentations
Computer Programming Lab(7).
Advertisements

Building Java Programs Interactive Programs w/ Scanner What is a class? What is an object? What is the difference between primitive and object variables?
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.
Chapter 2: Java Fundamentals Input and Output statements.
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!");
© 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.
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology- George Koutsogiannakis.
COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
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;
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
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.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Chapter 2: Java Fundamentals
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Java 1.5 The New Java Mike Orsega Central Carolina CC.
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
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.
Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
Lecture 4 – Scanner & Style. Console Output The console that starts a Java application is typically known as the standard output device. The standard.
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
The Basics Input / Output, and Primitive Data Types.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
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.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
Introduction to programming in java
Lecture 4.1: More on Scanner, Methods, and Codingbat Michael Hsu CSULA.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Lecture 4 – Scanner & Style
CompSci 230 S Programming Techniques
Building Java Programs
Chapter 2 Clarifications
Input/Output.
TemperatureConversion
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Yanal Alahmad Java Workshop Yanal Alahmad
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
CSC 1051 – Data Structures and Algorithms I
BIT115: Introduction to Programming
Introduction to Classes and Methods
Java so far Week 7.
Building Java Programs
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.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Review of Previous Lesson
Input and Formatted Output (printf)
Optional Topic: User Input with Scanner
Presentation transcript:

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 things we can do here other than outputting straight text.

Using println will have standard output put in a carriage return after the text while print will not. This code: System.out.println(“AP CS”); System.out.println(“ is the best ever!”); Produces: AP CS is the best ever! This code: System.out.print(“AP CS”); System.out.print(“ is the best ever!”); Produces: AP CS is the best ever!

We can output variables the same way that we can output text. This code: int numStudents = 13; System.out.println(numStudents); Produces: 13 Usually though we want to output some text and we can do that with the + operator. This code: int numStudents = 13; System.out.println(“Number of AP CS students: ” + numStudents); Produces: Number of AP CS students: 13

Sometimes we want to print something that is a little different from normal text. For that we have the escape key which is the backslash. Here is a listing of the more useful ones: 1. \n – insert a newline 2. \r – insert a carriage return 3. \t – insert a tab 4. \’ – insert a single quote 5. \” – insert a double quote 6. \\ - allows you to output a backslash

This code: System.out.println(“I want sharks\n\t with \”laser\” beams”); Produces: I want sharks with “laser” beams

Standard Input is a bit more complicated than Standard Output but again JAVA provides us a fairly straightforward way to do this. The sample we will use is as follows: import java.util.*; public class Interrogator { public static void main(String [] arg){ Scanner input = new Scanner(System.in); System.out.println("What is your name?"); String name = input.nextLine(); System.out.println("Hello " + name + ", I hope you doing well today."); }

When programing in JAVA, we are provided a library of other code that we can make use of. But to do this, you must include an import statement to import the corresponding library. In this program we are using the Scanner class which is why we need: import java.util.*; After that we have a class and a main method declaration just like in Hello World

To use a Scanner we must declare it just like we would a primitive variable. But, because the Scanner is a class, we must also create an instance of it using the new operator. In this case, we named our Scanner “input” When we created it, we passed in System.in to the constructor method so that it scans input from the computer’s Standard input Scanner input = new Scanner(System.in);

In this program we were interested in finding out the person’s name so we used the method nextLine() to get the next line of text from Standard input. Once that line of text is returned from the method, we then use the assignment operator (=) to assign it to our variable name. String name = input.nextLine();

next() – returns the next token (word) from the scanner nextLine() – returns the next line of text from the scanner nextBoolean() – reutrns the next boolean from the scanner nextInt() – returns the next int from the scanner nextDouble() – returns the next double from the scanner