Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming.

Similar presentations

Presentation on theme: "Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming."— Presentation transcript:

1 Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming

2 SE15: Strings and more I/O6–26–2 Todays Learning Objectives Learn about the Java data type used for strings of characters Learn how to do simple string manipulation Learn how to do window-based input and output

3 SE15: Strings and more I/O6–36–3 Lecture Outline The Class String Concatenation of Strings String Methods Scanner Class revisited JOptionPane

4 SE15: Strings and more I/O6–46–4 The Class String The class called String can be used to store and process strings of characters. A value of type String is a sequence of characters treated as a single item. You have already used constants of type String System.out.println( Enter a whole number); You can also declare variables of type String String greeting; greeting = Hi, how are you?;

5 SE15: Strings and more I/O6–56–5 Concatenation of Strings You can connect two strings together using the + operator This is known as concatenation String greeting = Hello; String sentence; Sentence = greeting + Andy; System.out.println(sentence); will write HelloAndy

6 SE15: Strings and more I/O6–66–6 Classes A class is a type whose values are objects. Objects are entities that store data and take actions Objects of type String store data consisting of a sequence of characters The actions that an object can take are called methods Most methods of the string class return or produce some value e.g. the method length() returns the number of characters in a String object String greeting = Hello; int n = greeting.length();

7 SE15: Strings and more I/O6–76–7 String Methods The String methods can be used to manipulate string values length() toUpperCase() Many of the methods depend on counting positions in the string indexOf(is) returns 5, the index of the substring is charAt(8) returns f SE15isfun 0 1 2 3 4 5 6 7 8 9 10 Savitch 80-82

8 SE15: Strings and more I/O6–86–8 Escape Characters How do you include quotation marks in a String? \Double quote \Single Quote \\Backslash \nNew line (go to start of the next line) \rCarriage return (go to start of current line) \tTab

9 SE15: Strings and more I/O6–96–9 Scanner Methods (The actions that Scanner can perform) nextInt() reads one int value typed in at the keyboard and returns its value Others exist for each other primitive type, such as: nextDouble() nextBoolean() To handle strings next() returns the String value consisting of the next keyboard characters up to, but including the first delimiter. nextLine() returns rest of the keyboard line (the \n is discarded)

10 SE15: Strings and more I/O6–10 JOptionPane JOptionPane provides a simple window interface to interact with users showInputDialog returns a String entered by the user showMessageDialog displays a String in a window String enteredString; enteredString = JOptionPane.showInputDialog( Enter a word); JOptionPane.showMessageDialog(null,Hello); Savitch 106-115

11 SE15: Strings and more I/O6–11 Summary Looked at the String Class and how strings of characters may be manipulated using the methods of the class Reviewed the use of the Scanner Class Met the JOptionPane for simple window I/O

12 SE15: Strings and more I/O6–12 Follow-up Work Exercise 3 Savitch chapter 2 Rewrite the change calculating program to use the JOptionPane and to allow uses to enter values in pounds and pence. In addition to the existing output, the program should output the numbers of £50, £20, £10, and £5 notes required and £1 and £2 coins.

Download ppt "Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming."

Similar presentations

Ads by Google