StringPractice Class

 

import java.util.Scanner;

public class StringPractice {

/** Your Name Here
* String Practice
* Two things to remember about Strings
* 1. Strings are Objects, they are NOT primitives like int, double or boolean
* 2. Strings are IMMUTABLE - you cannot modify a String object,
* instead a new String object is created
*/

public static void main(String[] args) {
/*
// Part 1
System.out.println("Part 1");
Scanner reader= new Scanner(System.in); // create a scanner object to get user input.
String name, name2; // declare two String objects
System.out.println("Enter nbc ");
name=reader.next(); // assign a value of nbc to name
System.out.println("Enter nbc");
name2=reader.next(); // assign a value of nbc to name2
System.out.println("Compare name (" + name + ") to name2 (" + name2 + ") using == and the result is: ");// what do you think the result will be?
System.out.println("And the answer is: " + name2 == name);
*/

/*
// Part 2 Concatenating strings
System.out.println("Part 2");
String s1 = "This is a ";
System.out.println(s1);
String s2 = "string";
System.out.println(s2);
String s3 = s1 + s2; // Create a new string object by joining the first two string objects.
// What will the next println print? Write your answer here.....
System.out.println(s3);
String s4 = "This is a " + s2; // another way to combine Strings
System.out.println(s4);
String s5 = "First ";
s5 += s2; // what will the result of this line be????? Notice the += operator.
// What will printout when s5 is printed - write your answer here.....
System.out.println(s5);
*/

/*
//Part 3 Escape Characters
// a forward slash \ is an escape character, it tells Java to treat the character after it as a character
System.out.println("Part 3");
String s11 = "This is an example of an escape character. \n And" +
" now we're on a new line. \n \t This is a tab.";
String s22 = "\"This is a quote.\""; // the only way to print " is to use the \ escape character
System.out.println(s11);
System.out.println(s22);
*/

/*
// Part 4 Useful String Object methods
// If == cannot be used to compare Strings then how do you compare them?
// You use the String object's compareTo method.
System.out.println("Part 4");
String firstString = new String("abc"); // two different ways of creating string objects
s2 = "abc";
System.out.println(firstString == s2); // remember this does NOT work.
System.out.println(firstString.compareTo(s2)); // but this does, or does it?
System.out.println(firstString.equals(s2)); // this is better, that is what we expect.
System.out.println("a".compareTo("A")); // what happens if you reverse the letters?
// so if the compareTo method returns 0 then the two strings are equal.
// otherwise it returns the difference between the strings.

s3 = "String.";
s4 = "This is a " + s3;
System.out.println(s4.length());
System.out.println(s3.indexOf('i')); //
System.out.println(s3.charAt(2));
// you must remember that the index for characters in a string always starts at ZERO!
// so 0 = S 1 = t , 2 = r so charAt(2) returns r
*/

}// end of main method

}// end of class