Download lect08 Strings and Input Failure

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
(Dreaded) Quiz 2
• Next Monday
Strings Revisited
• String Is a Type (Object) in JAVA
• Automatically imported in java.lang
• May Initialize or Assign with Double Quoted
Values
• Can Add String to String with ‘+’ Operator
• CANNOT Compare using Equality Operators
(uses .equals() method instead)
• Methods Available to
–
–
–
–
Access single characters
Get length of String
Find a character or substring in String
Convert to lower or upper case
Objects
• Objects: Types of Variables Made from Built-In
Types
• Variables Can be Declared as an Object Type
• Objects Usually Have Methods/Functions Tied to
Them
–
–
–
–
Member functions
Public functions
Interface functions
Methods
• String Is an Example of an Object Type
• StringVariable.length() Returns the
Number of Characters in the String
Methods
• Also called Procedures, Functions
• Have Parenthesis (e.g., method() )
• May be Passed Arguments, or
Parameters Inside of Parenthesis
• Parameters are values to initialize new
variables in method
• Parameters used to pass information to
method
String Methods
• charAt(int index): return char at named
index (starting at 0)
• equals(String str): return boolean
• indexOf(char): return int of first occurrence
of char (index starts at 0)
• indexOf(String substr): return int of first
occurrence of String substr
• length(): return int length of String
• substring(int start, int end): return String
between start and end
• toLowerCase(), toUpperCase(): convert
String
String myString = "My Dog Sandy",compareString;
System.out.println("The fourth character is: " +
myString.charAt(3));
if (myString.equals("my dog sandy"))
System.out.println("Strings are equal");
else
System.out.println("Strings are not equal");
compareString = myString + myString;
System.out.println("Added strings are: " +
compareString);
Output:
The fourth character is: D
Strings are not equal
Added strings are: My Dog SandyMy Dog Sandy
compareString = myString.toLowerCase();
System.out.println("String is: " + compareString);
System.out.println("Char S is at location: " +
myString.indexOf('S'));
System.out.println("substring og is at location: " +
myString.indexOf("og"));
System.out.println("Length of string is: " +
myString.length());
System.out.println("substring between location 3 and 5 is: " +
myString.substring(3,5));
Output:
String is: my dog sandy
Char S is at location: 7
Substring og is at location: 4
Length of string is: 12
Substring between location 3 and 5 is: Do
import java.util.Scanner;
public class MyProgram
{
public static void main( String [ ] args)
{
Scanner scan = new Scanner(System.in);
String lastname;
boolean instructor = false;
System.out.print(“Enter last name:”);
lastname = scan.next();
if(lastname.equals(“hanrath”))
instructor = true; // privileged
else
instructor = false; // not privileged
}
}
More on Input
•
•
•
•
•
•
Input Failure Common
*MUST* Be Part of Test Plan
Pages 310-313 in Anderson
Check Input for Type Desired
If not, Notify User, and Get Rid of Line
Use scan.hasNextInt(),
scan.hasNextDouble(),
scan.hasNextFloat()
Input
Failure
Example
import java.util.*;
class InputFailureExample
{
public static void main(String[] args)
{
int numE;
String garbage;
Scanner scan = new Scanner(System.in);
System.out.print("Enter Integer: ");
while (!scan.hasNextInt())
{
garbage = scan.nextLine();
System.out.println("Please try again.");
System.out.print("Enter Integer: ");
}
numE = scan.nextInt();
System.out.println("You entered: " + numE);
}
}
Know for Quiz 2
• Everything through Exam 1
• Loops: while, for, do-while
• switch-case Statement
• String Methods
• Input Failure Methods