// 11/25/2019 /** the String class introduces us to the data type used for text. */ /* def. of a string (in computer science) is a sequence of characters */ // methods of String class: // equals, charAt, startsWith, endsWith, length, compareTo import java.util.Scanner; public class Strings { public static void main(String[] args) { String name1 = "John Doe"; /* you may also use new to create a String object, in a similar fashion to the creation of other objects. String nm = new String("John Doe"); // this would also work Scanner sc = new Scanner(...) File infile = new File(...) PrintWriter outfile = new PrintWriter(); int[] arr = new int... */ System.out.println("name1 is " + name1); String name2 = "Alice"; System.out.println("name2 is " + name2); if (name1.length() != name2.length()) System.out.println("length of name2 is: " + name2.length()); char firstInit = name2.charAt(0); System.out.println("name2's first initial is: " + firstInit); // read in a String Scanner sc = new Scanner(System.in); System.out.println("enter a name: "); name1 = sc.next(); // test if name1 equals to name2 // name2 = name1; if (name1 == name2) // NOT the way to do it System.out.println("name1 is == name2 " + name1); else System.out.println("name1 is: " + name1 + " name2 is: " + name2); if (name1.equals(name2)) System.out.println("name1 is .equal to name2 " + name1); else System.out.println("name1 is: " + name1 + " name2 is: " + name2);; // char ch = 'a'; // if (ch >= 'A' && ch <= 'Z') // is ch an uppercase letter name2 = "Z"; if (name1.compareTo(name2) < 0) System.out.println("in alphabetical order: \n" + name1 + "\n" + name2); else System.out.println("in alphabetical order: \n" + name2 + "\n" + name1); } } /** discussion about test question on exam 1: String ans = sc.next(); if (ans == 'y') fix1: if (ans.charAt(0) == 'y') fix2: if (ans.equals("yes")) fix3: if (ans.startsWith("y")) *******/ /** Declare and read in 2 String objects. * Then, you are going to compare these 2 Strings without calling * .equals * hint: you can use for loop, you can loop up until the length of * a string. Really, you should first make sure that they have the * same length. you can call charAt to retreive each character that * needs to be compared. */