190 likes | 383 Views
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 .”
E N D
Outline • The Class String • Keyboard and Screen I/O • Documentation and Style • Graphics Supplement
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);
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
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
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.
String Processing This returns 19: beginning of hard This is like (0, 19) It is a quotation mark
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
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();
Scanner Class Methods Reads one word from the keyboard
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
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.
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. */
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