lecture 6 strings and more i/o comp1681 / se15 introduction to programming

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

Upload: ashley-pope

Post on 28-Mar-2015

214 views

Category:

Documents


2 download

TRANSCRIPT

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

Lecture 6

Strings and more I/O

COMP1681 / SE15

Introductionto Programming

Page 2: Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming

SE15: Strings and more I/O 6–2

Today’s 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

Page 3: Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming

SE15: Strings and more I/O 6–3

Lecture Outline

The Class String Concatenation of Strings String Methods

Scanner Class revisited JOptionPane

Page 4: Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming

SE15: Strings and more I/O 6–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 StringSystem.out.println( “Enter a whole number”);

You can also declare variables of type StringString greeting;greeting = “Hi, how are you?”;

Page 5: Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming

SE15: Strings and more I/O 6–5

Concatenation of Strings

You can connect two strings together using the + operator

This is known as concatenationString greeting = “Hello”;

String sentence;

Sentence = greeting + “Andy”;

System.out.println(sentence);

will write HelloAndy

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

SE15: Strings and more I/O 6–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 objectString greeting = “Hello”;

int n = greeting.length();

Page 7: Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming

SE15: Strings and more I/O 6–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’

S E 1 5 i s f u n

0 1 2 3 4 5 6 7 8 9 10

Savitch 80-82

Page 8: Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming

SE15: Strings and more I/O 6–8

Escape Characters

How do you include quotation marks in a String?

\” Double quote

\’ Single Quote

\\ Backslash

\n New line (go to start of the next line)

\r Carriage return (go to start of current line)

\t Tab

Page 9: Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming

SE15: Strings and more I/O 6–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)

Page 10: Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming

SE15: Strings and more I/O 6–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

Page 11: Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming

SE15: Strings and more I/O 6–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

Page 12: Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming

SE15: Strings and more I/O 6–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.