Know for Quiz Everything through Last Week and Lab 7 Loops: while, for, do-while switch-case Statement
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 Compare Strings
Objects Objects: Types of Variables Made from Built-In Types Variables Can be Declared as an Object Type Objects Usually Have 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 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 length(): return int length of 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
System.out.println("Length of string is: " + myString.length()); Output: Length of string is: 12
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 hasNextInt(), hasNextDouble() If not, Notify User, and Get Rid of Line Once hasNotXXX() returns true, safe to read in value
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."); } numE = scan.nextInt(); System.out.println("You entered: " + numE);
Arrays Syntax: type variableName[] = new type [size]; Memory Is Set Aside for size Items of type Each Variable Location in Array Is Accessed by Offsetting into the Array with an Integer Expression Legitimate Offsets Are 0 to size-1
Array Example int [] values = new int[15]; values[0] = 150; System.out.println( values[0]); values[3] = values[0] + 6;
Array Example final int ARRAY_SIZE = 100; int offset; int [] numArray = new int [ARRAY_SIZE]; for(offset = 0; offset < ARRAY_SIZE; offset++) { numArray[offset] = 0; } for(offset = 0; offset < numArray.length; offset++) numArray[offset] = offset;
Files Data in Main Memory is “volatile” File: Place for “permanent” data storage C: drive, A: drive, Flash drive, etc. Main Memory File main() int num; string firstname; Disk
Output File Streams import java.io.*; class FileOutput { public static void main(String [ ] args) throws IOException int i; FileOutputStream ofile = new FileOutputStream("data2.txt",false); //true:APP PrintWriter pw = new PrintWriter(ofile); for (i=0;i < 100; i = i + 2) pw.println(i); } pw.close(); // Writes data to file on disk
Input Files import java.io.*; import java.util.Scanner; class FileInput { public static void main(String [ ] args) throws IOException File ifile = new File("data2.txt"); Scanner scan = new Scanner(ifile); while (scan.hasNextInt()) i = scan.nextInt(); }
mydata.txt file 5 8 9.3 Jon 6 14.335 Bill 0 35.67e9 Mary import java.util.Scanner; import java.io.*; class FormatFileData { public static void main(String [ ] args) throws IOException int loops, integer, i; float decimal; String name; File ifile = new File("mydata.txt"); Scanner scan = new Scanner(ifile); loops = scan.nextInt(); for(i= 0 ; i < loops; i++) integer = scan.nextInt(); decimal = scan.nextFloat(); name= scan.next(); System.out.print(integer + " "); System.out.print(decimal + " "); System.out.print(name + " "); System.out.println(); } mydata.txt file 5 8 9.3 Jon 6 14.335 Bill 0 35.67e9 Mary -23 -4.55 Smith -3 -4e3 xyz Output: 8 9.3 Jon 6 14.335 Bill 0 3.567E10 Mary -23 -4.55 Smith -3 –4000.0 xyz
Know for Quiz 2 Everything through Last Week and Lab 7 Loops: while, for, do-while switch-case Statement