CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.

Slides:



Advertisements
Similar presentations
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Advertisements

Hand Crafting your own program By Eric Davis for CS103.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
Introduction to Computer Programming Stringing Along – Using Character and String Data.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
***** SWTJC STEM ***** Chapter 3-1 cg 36 Java Class Libraries & API’s A class library is a set of classes that supports the development of programs. Java.
Computer Programming Lab(4).
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
Introduction to Information and Computer Science Computer Programming Lecture c This material (Comp4_Unit5c), was developed by Oregon Health and Science.
Lecture 2: Classes and Objects, using Scanner and String.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
EXAM 1 REVIEW. days until the AP Computer Science test.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
CSC 1051 M.A. Papalaskari, Villanova University Algorithms Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Using Objects Chapter 3 Spring 2006 CS 101 Aaron Bloomfield.
Building java programs, chapter 3 Parameters, Methods and Objects.
Cumulative algorithms. 2 Adding many numbers How would you find the sum of all integers from ? // This may require a lot of typing int sum = 1 +
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
CSCI S-1 Section 8. Coming Soon Problem Set Four (72 + 5/15 points) – Tuesday, July 21, 17:00 EST.
C OMMON M ISTAKES CSC Java Program Structure  String Methods.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Introduction to programming in java
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
(Dreaded) Quiz 2 Next Monday.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Lecture 4 – Scanner & Style
Chapter 2 Clarifications
CSC111 Quick Revision.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Chapter 2 Elementary Programming
CSC 1051 – Data Structures and Algorithms I
Maha AlSaif Maryam AlQattan
Chapter 5: Control Structures II
Introduction to Methods in java
Repetition.
Data types, Expressions and assignment, Input from User
Something about Java Introduction to Problem Solving and Programming 1.
The for-loop and Nested loops
Introduction to Classes and Methods
Java Language Basics.
Know for Quiz Everything through Last Week and Lab 7
Chapter 3 Spring 2006 CS 101 Aaron Bloomfield
CSC1401 Input and Output (with Files)
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Building Java Programs
Building Java Programs
Random Numbers while loop
Consider the following code:
More on iterations using
Optional Topic: User Input with Scanner
Presentation transcript:

CSC1401 Strings (text)

Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations

Strings Anything within double quotes Examples: System.out.println(“Hi”); System.out.println(“The amount is “ + money); String name = “Steve”;

Strings as a Java class Note that we do not need an import statement Formally, we should write: String name; name = new String (“Steve”); But it’s ok to write: String name; name = “Steve”;

Input of Strings in Java (with Scanner ) import java.util.*; // needed for scanner class IOTesting { public static void main(String [] args) { Scanner scan = new Scanner(System.in); String course; course = scan.nextLine(); …

Problem Solving with Strings We typically wish to Parse strings Change strings Java has lots of built-in String methods (check the Javadocs) We’ll just look at a few of them

Entering your name import java.util.*; // needed for scanner class IOTesting { public static void main(String [] args) { Scanner scan = new Scanner(System.in); String name; name = scan.nextLine(); // if the person types in Steve Cooper // how can we get the first // and last names??? …

What do we need to be able to do? Finding a string within a string (in this case a blank) indexOf (String searchString) indexOf (String searchString, int startPosition) Getting a part of a String substring (int start, int onecharafterend) substring (int start)

In code System.out.println("Enter your name"); String name = sc.nextLine(); int blank = name.indexOf(" "); String first = name.substring(0,blank); String last = name.substring(blank+1); System.out.println("Your first name is " + first + " and your last is " + last);

Another problem How many e ’s are in your name? Two solutions: Use indexOf (starting from the position after the last e was found) Use the charAt (int location) method Note that charAt returns a character, not a String. Characters may be tested for equality, ==, but not Strings

Other string functions Assume that we have 2 string variables, first and last first.equals (last) Cannot use == with String s! first.replace (“e”, “a”) Replaces all e’s with a’s last.replaceFirst (“o”, “ww”) Replaces the first o with ww

One last problem Determining if a string is an anagram How can we solve this?

Summary Strings are how text gets represented in Java String is a built-in class, with lots of methods associated with it.

Reading Assignment Media Computation, Chapter 12, Section 2, and pp