Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computer Science and Object-Oriented Programming

Similar presentations


Presentation on theme: "Introduction to Computer Science and Object-Oriented Programming"— Presentation transcript:

1 Introduction to Computer Science and Object-Oriented Programming
Week 6 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

2 Week 6 Topics Strings Reading Input

3 Strings A string is a series of characters such as “Hello, World!” Strings are delimited by (enclosed within) quotation marks Creating a string object does not require the new operator: String msg = “Hello, World!”; The length of the example string above is 13 and can be determined via a call to the length method: System.out.println(msg.length());

4 The + operator is used to concatenate strings (put them together):
Strings Cont. The + operator is used to concatenate strings (put them together): String a = “Agent”; int n = 7; String bond = a + n; Note that the integer value n is converted to a string Use concatenation to simplify expressions: System.out.println(“Total is ” + total);

5 Strings Cont. If a string contains digits of a number, you can use the Integer.parseInt or the Double.parseDouble method to obtain the number value int count = Integer.parseInt(input); double price = Double.parseDouble(input);

6 Strings Cont. Use the substring method to extract a part of a string: s.substring(start, pastEnd) You specify the position you want to start at, and the position one past the position you want to end at String msg = “Hello, World!”; String sub = msg.substring(0, 5); // sub is “Hello”

7 6.1.1 Strings Cont. String msg = “Hello, World!”;
String sub = msg.substring(7, 12); // sub is “World” String sub = msg.substring(7); // World! Above copies from start pos to end H e l o , W r d ! 1 2 3 4 5 6 7 8 9 10 11 12

8 Reading Input In Java version 5.0, a Scanner class was added that lets you read keyboard input in a convenient manner To construct a Scanner object, simply pass the System.in object to the Scanner constructor: Scanner in = new Scanner(System.in); Once you have a scanner, use nextInt or nextDouble method to read a whole number or a floating point number.

9 Reading Input Cont. Scanner in = new Scanner(System.in); System.out.print(“Enter Quantity: ”); int quantity = in.nextInt(); System.out.print(“Enter Price: ”); double price = in.nextDouble();

10 Reading Input Cont. nextLine() gets all text including spaces, next() gets a word (token) as soon as it comes to a white space (space, end of line, tab) System.out.print(“Enter City: ”); String city = in.nextLine(); System.out.print(“Enter state code: ”); String state = in.next(); import java.util.Scanner;

11 Reading Input Cont. An alternative to using a Scanner class and the console is to have a dialog box. String input = JOptionPane.showInputDialog(“First number: “); Convert the String to a number if necessary as previously discussed. Add this line to the end of your main method: System.exit(0); import javax.swing.JOptionPane;

12

13 Reference: Big Java 4th Edition by Cay Horstmann
Strings (section 4.5 in Big Java) Reading Input (section 4.6 in Big Java)


Download ppt "Introduction to Computer Science and Object-Oriented Programming"

Similar presentations


Ads by Google