Standard I/O - Loudoun County Public Schools

Standard I/O - Loudoun County Public Schools

Standard I/O Can we talk? Standard Output 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. print vs println

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! Output of Variables 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

\Escape Sequence 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

\Escape Sequence Example This code: System.out.println(I want sharks\n\t with \laser\ beams); Produces: I want sharks with laser beams Standard Input 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."); } }

Import the important 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

Scanner Class 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 computers Standard input Scanner input = new Scanner(System.in);

Reading in information In this program we were interested in finding out the persons 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(); Methods of the Scanner Class

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

Recently Viewed Presentations

  • Periodic Table Families Project

    Periodic Table Families Project

    Requirements For Each Slide. Each element should have its own slide that includes the following: Number of protons, neutrons & electrons - A description of the "personality of the element"and "its outward appearance"- include physical properties of the element, uses...
  • RCRE Presentation Template

    RCRE Presentation Template

    Serious infestations have occurred in New Jersey in the past 1957, 1967,1985, 1988 and 1989 (Ghidiu and Rabin - 'The Grower', 1991) There was a light infestation at Centerton Research Farm and a local farm in unsprayed pepper plots in...
  • History of Sports - taylor.k12.ky.us

    History of Sports - taylor.k12.ky.us

    History of Sports Mr. Young Sociology Ancient Sports These are some sports est. to be over 2500 years old Hurling in Ireland Harpastum In Rome Cuju in China Polo in Persia Also Wrestling Ancient Egyptian Sports Helped to start many...
  • Healthy Lives Fairer Futures Josie Murray @PublicHealthStR For

    Healthy Lives Fairer Futures Josie Murray @PublicHealthStR For

    Consultation - Q1. We launched a consultation at the FPH Scotland conference Nov 2016 - January 2017. The advocacy subgroup longlisted priorities based on the feedback from the consultation in February 2017
  • Sexual Harassment Prevention Reviewed, DIR-T USCGAUX Expectations Recognize:

    Sexual Harassment Prevention Reviewed, DIR-T USCGAUX Expectations Recognize:

    Welcome to the US Coast Guard Auxiliary SHP Training. Independent Learner Instructions. This document is derived from the mandatory training of the same name. It contains both images and narratives regarding the training topic and is provided in a "presenter...
  • Vocabulary Acrostics - Woodbridge Township School District

    Vocabulary Acrostics - Woodbridge Township School District

    Vocabulary Acrostics. After your test, come to me and pick a number. Write an acrostic using your assigned vocabulary word: satire, genre, articulate, paraphrase, script . The acrostic should not only use the letters in the word, but also relate...
  • From Emerson to Thoreau

    From Emerson to Thoreau

    From Emerson to Thoreau American Literature I 15/11/2004 Cecilia H.C. Liu Info. Provided by Dr. Murphy Thoreau's Walden (1854) Thoreau's Walden is a book about nature in the woods at Concord and a book on how to live.
  • IRA vs 401K - California State Polytechnic University, Pomona

    IRA vs 401K - California State Polytechnic University, Pomona

    IRA vs 401K By James Traylor Michael Pan Melissa Sy James Ayoub Team 6 Objective Evaluate 401k vs IRA Compare Pros & Cons Maximize returns in the Long run Find most useful techniques to solve our dilemma Scenario We will...