Introduction to Java The String class Input and
















- Slides: 16

Introduction to Java The String class Input and Output

Classes • A class is a type used to produce objects. • An object is an entity that stores data and can take actions defined by methods. • For example: – An object of the String class stores data consisting of a sequence of characters. – example: length() method returns the number of characters in a particular String object. String solution = ″ten″ int how. Many = solution. length()

Objects, Methods, and Data • Objects within a class – have the same methods – have the same kind(s) of data but the data can have different values. • Remember, primitive types have values, but no methods.

Some String Methods • • s. length() s. equals(s 1) s. to. Lower. Case s. trim() s. char. At(n) s. substring(start, end) s. index. Of(substring, start) s. compare. To(s 1) – (+ if s > s 1; if s 1 > s; else 0) – see book p. 80 -81

String Objects vs. Variables • No methods allow you to change the value of a String object. • But you can change the value of a String variable. String pause = “ Hmm“; pause = pause. trim() pause = pause + “mmm!”; pause = “Ahhh”; value of pause Hmm Hmmmmm Ahhh

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. ”);

Escape Characters • Each escape sequence is a single character even though it is written with two symbols.

Keyboard and Screen I/O: Outline The two issues are • Screen Output – how to write to the screen • Keyboard Input – how to read from the keyboard.

Screen Output • System. out is an object that is part of Java. • println() is one of the methods available to the System. out object. System. out. println(“Hi!“);

print and println methods • Alternatively, use print() System. out. print(“When everything “); System. out. print(“does not fit on “); System. out. print(“one line, use the “); System. out. print(“”print” ”); System. out. println(“statement”); ending with a println(). • System. out. println(“c” + “o” + “c” +”a” );

Keyboard Input • Java 5. 0 keyboard input facilities are provided by the Scanner class • The Scanner class is stored in the java. util package. – remember: A package is a library of classes.

Using the Scanner Class • Near the beginning of your program, insert import java. util. * • Create an object of the Scanner class Scanner keyboard = new Scanner (System. in) • Read data (an int or a double, for example) int n 1 = keyboard. next. Int(); double d 1 = keyboard, next. Double();

Some Scanner Class Methods • syntax Int_Variable = Object_Name. next. Int(); Double_Variable = Object_Name. next. Double(); String_Variable = Object_Name. next. Line(); • see also p 93

next. Line()Method Use with Caution • The next. Line() method reads the remainder of the current line, even if it is empty. n = scanner. next. Int() s 1 = scanner. next. Line() s 2 = scanner. next. Line() • 42 is the answer and don’t you forget it • 42 and don’t you forget it

The Empty String • A string can have any number of characters, including zero. • The string with zero characters is called the empty string. • The empty string is useful and can be created in many ways including String s 3 = “”; • class Delimiters. Demo

Documentation and Style: Outline Points • Use Meaningful Names • Self-Documentation and Comments // this line is a comment /* this line and this line are comments*/ • Use Indentation • Use Named Constants public static final PI = 3. 142