Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Can we talk?

2 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.

3 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!

4 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

5 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

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

7 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."); }

8 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

9 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);

10 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();

11 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


Download ppt "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."

Similar presentations


Ads by Google