1 / 19

String, Input & Output, Graphics

String, Input & Output, Graphics. Outline. The Class String Keyboard and Screen I/O Documentation and Style Graphics Supplement. String Constants & Variables. We have used constants of type String already: "Enter a whole number from 1 to 99 .”

deliz
Download Presentation

String, Input & Output, Graphics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. String, Input & Output, Graphics

  2. Outline • The Class String • Keyboard and Screen I/O • Documentation and Style • Graphics Supplement

  3. String Constants &Variables • We have used constants of type Stringalready: "Enter a whole number from 1 to 99.” • Value of type String is a sequence of characters treated as a single item. • Declaring: String greeting; greeting = "Hello!"; or String greeting = "Hello!"; or String greeting = new String("Hello!"); • Printing: System.out.println(greeting);

  4. Concatenation of Strings • Two strings are concatenated using the +operator. String greeting = "Hello"; String sentence; sentence = greeting + " officer"; System.out.println(sentence); String solution; solution = "The answer is " + 42; System.out.println(solution); • Any number of strings can be concatenated by using + . Hello officer The answer is 42

  5. String Methods • An object of the String class stores data consisting of a sequence of characters. Objects have methods and data. • The length() method returns the number of characters in a particular String object. String greeting = "Hello”; intn = greeting.length(); • The method length() returns an int. You can use a call to method length() anywhere an int can be used, examples: int count = greeting.length(); System.out.println("Length is " + greeting.length()); count = greeting.length() + 3; n = 5

  6. String Indices • Positions start with 0, not 1. • The ‘J’ in “Java is fun.” is in position 0. • A position is referred to an index. • The ‘f ’ in "Java is fun." is at index 8.

  7. String Class Methods

  8. String Class Methods (Cont.)

  9. String Processing This returns 19: beginning of hard This is like (0, 19) It is a quotation mark

  10. Escape Characters • How would you print "Java" refers to a language.? • The compiler needs to be told that the quotation marks "do not signal the start or end of a string, but instead are to be printed. System.out.println("\"Java\" refers to a language."); • Each escape sequence is a single character even though it is written with two symbols, e.g.,System.out.println("new\nline"); new line

  11. Review: Output & Input • Screen output is provided by the System.outobject. This object has many useful methods such as println() & print(), as we have seen so far: System.out.print("Hello!"); • Keyboard input is provided by the Scanner class in the java.util package. import java.util.Scanner; Scanner keyboard = new Scanner (System.in); intn1 = keyboard.nextInt(); double d1 = keyboard.nextDouble();

  12. Scanner Class Methods Reads one word from the keyboard

  13. nextLine() Method Caution • The nextLine()method reads the remainder of the current line even if it is empty. Consider the following statements: intn; String s1;n = keyboard.nextInt();s1 = keyboard.nextLine(); • Consider the following input: So, nis set to 42 and but s1 is set to the emptystring"". As such, we need an extra keyboard.nextLine() to get rid of \n, which is at the end of the first line. 42\n and don't you\n forget it.\n

  14. Documentation and Style • Most programs are modified over time to respond to new requirements. • Programs which are easy to read and understand are easy to modify. • Even if it will be used only once, you have to read it in order to debugit. • The best programs are self-documenting, that is: • Clean style. • Well-chosen names.

  15. Comments • Comments are written into a program to explain the program. • They are useful to the programmer, but they are ignored by the compiler. • Comments can begin with // or begin with /**and end with */: • Everything after // is ignored by the compiler: double radius; // in centimeters • Everything between /**… */ is also ignored by the compiler: /** This program should only be used on MWF. */

  16. This ends the windowing program’s execution JOptionPane is a class in swing package The method showInputDialog is called through the class JOptionPaneand the input is stored in appleStringvariable Convert a string to an integer, e.g., “10” to 10 The method showMessageDialog is called through the class JOptionPaneand the output is printed by totalFruitCount

  17. Outcome of the Program

  18. Strings to Numbers

More Related