import java.util.Scanner; // R6 Assignment // Author: Justin Daniels // Date: Feb 7, 2017 // Class: CS163 // E-Mail: jrindnls@cs.colostate.edu public class R6 { private static boolean debug = false; public static void main(String[] args) { // TODO Auto-generated method stub /* * * Testing the Scanner methods of nextInt(), nextDouble(), next(), * nextLine(), hasNext(), and hasNextDouble() * */ // In order to move through each piece incrementally, simply uncomment the debug variable // To prevent too much text, comment out the previous instance of debug //debug = true; if (debug) { System.out.println("To open a Scanner reading from the system input: "); System.out.println("Syntax: Scanner VARIABLE_NAME = new Scanner(System.in);"); Scanner input = new Scanner(System.in); System.out.print("Capturing input.nextInt(): "); int nextInt = input.nextInt(); System.out.println("Your number was: " + nextInt); System.out.println(); System.out.print("Capturing input.nextDouble(): "); double nextDouble = input.nextDouble(); System.out.println("Your number was: " + nextDouble); System.out.println(); System.out.print("Capturing input.next(): "); String next = input.next(); System.out.println("Your token was: " + next); System.out.println("Remember to include an input.nextLine() to discard the remainder of the line!"); input.nextLine(); System.out.println(); System.out.print("Capturing input.nextLine(): "); String nextLine = input.nextLine(); System.out.println("Your line was: " + nextLine); System.out.println("Closing Scanner(System.in)"); input.close(); } debug = false; //debug = true; if (debug) { System.out.println("To open a Scanner reading from a String: "); System.out.println("Syntax: Scanner VARIABLE_NAME = new Scanner(STRING_VARIABLE_NAME);"); System.out.println(); String myFirstTestString = "This is a test of hasNext()"; System.out.println("Initializing Scanner: Scanner stringReader = new Scanner(myFirstTestString);"); Scanner stringReader = new Scanner(myFirstTestString); System.out.println("Reading from the following String: " + myFirstTestString); System.out.println("Testing stringReader.hasNext()"); if (stringReader.hasNext()) { System.out.println("There was something for me to read, and it was: " + stringReader.next()); } else { System.out.println("I failed to see if there was something here."); } System.out.println("Closing Scanner."); stringReader.close(); System.out.println(); String mySecondTestString = "1 1.2232"; System.out.println("Opening Scanner to read the next String: stringReader = new Scanner(mySecondTestString);"); stringReader = new Scanner(mySecondTestString); System.out.println("Reading from the following String: " + mySecondTestString); System.out.println("Testing stringReader.hasNextDouble()"); if (stringReader.hasNextDouble()) { System.out.println("I saw a double, and its value is: " + stringReader.nextDouble()); } else { System.out.println("I failed to see a number within this String."); } System.out.println("Closing Scanner."); stringReader.close(); } /* * * Creating a new String, String methods of length(), indexOf(), * charAt(), substring(), toUpperCase(); toLowerCase(), and * concatenation +. * */ debug = false; //debug = true; if (debug) { System.out.println("Declaring a new String without \"new\""); System.out.println("Syntax: String VARIABLE_NAME = \"Your text here\";"); String noNew = "Declared String without the \"new\" keyword"; System.out.println("My String was created without the \"new\" keyword"); System.out.println(noNew); System.out.println(); System.out.println("Declaring a new String WITH \"new\""); System.out.println("Syntax: String VARIABLE_NAME = new String(\"Your text here\");"); String withNew = new String("Declared String with the \"new\" keyword"); System.out.println("My String was created with the \"new\" keyword"); System.out.println(withNew); } debug = false; //debug = true; if (debug) { // .length() System.out.println("The .length() method counts how many characters are in a String"); System.out.println("from 1 ~ however long it might be."); System.out.println(); System.out.println("Syntax: STRING_VARIABLE.length()"); System.out.println(); String smallString = "Hello"; System.out.println("Checking the .length() of \"Hello\""); System.out.printf("The word \"Hello\" has %d characters\n", smallString.length()); System.out.println("Checking the .length() of a String counts spaces."); String lotsOfSpaces = "So many spaces"; System.out.println("Checking the length of \"" + lotsOfSpaces + "\""); System.out.println("The .length() of that String was: " + lotsOfSpaces.length() + " characters."); } debug = false; //debug = true; if (debug) { // .indexOf() System.out.println("Methods used to manipulate a String"); System.out.println("are index based - 0 is the first position, 1 the next"); System.out.println("all the way up until .length() - 1."); System.out.println(); System.out.println("The .indexOf() method takes a character as a parameter"); System.out.println("and returns the index position of the **FIRST** occurrence"); System.out.println("If a character is not found, it will return -1"); System.out.println(); System.out.println("Syntax: STRING_VARIABLE.indexOf('character to search for')"); System.out.println(); String indexOfFound = "Hello"; System.out.println("The .indexOf('l') in \"Hello\" is: " + indexOfFound.indexOf('l')); System.out.println(); String noIndexFound = "Random"; System.out.println("The .indexOf('x') in \"Random\" is: " + noIndexFound.indexOf('x')); } debug = false; //debug = true; if(debug){ //.charAt() System.out.println("The .charAt() method takes an index parameter"); System.out.println("and returns the character at that position."); System.out.println("If an index is out of bounds (-1 OR STRING.length())"); System.out.println("you will receive an \"IndexOutOfBoundsException\""); System.out.println("and the program ends at that point."); //String outOfBounds = "This example will be out of bounds."; //System.out.println(outOfBounds.charAt(outOfBounds.length())); System.out.println(); System.out.println("Syntax: STRING_VARIABLE.charAt(#)"); System.out.println(); String inBounds = "Testing an index value in bounds"; System.out.println("The .charAt(6) in \"" + inBounds + "\" is: '" + inBounds.charAt(6) + "'"); System.out.println(); System.out.println("However, it can also pick up special characters as well."); System.out.println("The .charAt(7) in \"" + inBounds + "\" is: '" + inBounds.charAt(5) + "'"); } debug = false; //debug = true; if(debug){ //.substring() System.out.println("The .substring() method can be used to chop"); System.out.println("up a String and return portions of it back to you."); System.out.println("There are 2 different ways to invoke this method:"); System.out.println(".substring(startingIndex)"); System.out.println("and"); System.out.println(".substring(startingIndex, endingIndex - 1)"); System.out.println(); System.out.println("Syntax: STRING_VARIABLE.substring(startingIndex, endingIndex - 1)"); String subString = "Break it up into smaller pieces"; System.out.println("String to be broken up: " + subString); System.out.println("Testing using .substring(6)"); System.out.println(subString.substring(6)); System.out.println(); System.out.println("The original String is unchanged: " + subString); System.out.println(); System.out.println("Testing using .substring(0, 4)"); System.out.println(subString.substring(0, 4)); System.out.println("Testing using .substring(0, 5)"); System.out.println(subString.substring(0, 5)); } debug = false; //debug = true; if(debug){ //.toUpperCase() and .toLowerCase() System.out.println("String methods never change the base String UNLESS you reassign the String itself."); System.out.println("We can use this to our advantage if we want"); System.out.println("to check a String at a specified case without"); System.out.println("altering the contents."); System.out.println(); System.out.println("Syntax: STRING_VARIABLE.toUpperCase() OR STRING_VARIABLE.toLowerCase()"); String mixedCase = "ThIS is PAinFul FOR mE To WRIte"; System.out.println("Testing the mixedCase String: " + mixedCase); System.out.println(); System.out.println("Printing it out in all uppercase using .toUpperCase()"); System.out.println(mixedCase.toUpperCase()); System.out.println("Has the original changed?: " + mixedCase); System.out.println(); System.out.println("Printing it out in all lowercase using .toLowerCase()"); System.out.println(mixedCase.toLowerCase()); System.out.println("Has the original changed?: " + mixedCase); System.out.println(); System.out.println("Not until I reassign it..."); mixedCase = mixedCase.toLowerCase(); System.out.println("Reassigned: " + mixedCase); } debug = false; //debug = true; if(debug){ // + and .concat() System.out.println("Strings can be combined in 2 different ways:"); System.out.println("The '+' symbol and the .concat(STRING_VAR) method."); System.out.println("It is often easier and faster to use the + to combine Strings"); System.out.println("Concatenation is the term for combining two String into one."); String firstString = "First String "; String secondString = "attaches to the second String."; System.out.println(); System.out.println("First String: " + firstString); System.out.println("Second String: " + secondString); System.out.println(); System.out.println("Syntax: STRING_VAR_1 + STRING_VAR_2"); System.out.println("Combining two Strings using the + symbol"); System.out.println("System.out.println(firstString + secondString);"); System.out.println(firstString + secondString); System.out.println(); System.out.println("Syntax: STRING_VAR_1.concat(STRING_VAR_2)"); System.out.println("Combining two Strings using the .concat() method"); System.out.println("System.out.println(firstString.concat(secondString);"); System.out.println(firstString.concat(secondString)); System.out.println(); System.out.println("We can also use the String literals to concatenate: "); System.out.println("System.out.println(\"My first String\" + \" attaches to my second String\");"); System.out.println("My first String" + " attaches to my second String"); } debug = false; //debug = true; if(debug){ // Character class System.out.println("Characters are a primitive data type that can"); System.out.println("only hold a single character - 'a', 'b', '&', etc."); System.out.println(); System.out.println("Declaring a character (char) type:"); System.out.println("char variableName;"); char myChar; System.out.println(); System.out.println("Initializing a character type:"); System.out.println("char variableName = 'single character';"); char my2dChar = '3'; System.out.println("My character is: '" + my2dChar + "'"); System.out.println(); System.out.println("Converting a character type to another integer type:"); my2dChar = 93; System.out.println("My character is now '" + my2dChar + "'"); System.out.println(); System.out.println("The Character class contains methods that we will be able to use"); System.out.println("to check a boolean condition using a character"); System.out.println("The Character.isDigit() method will return true if a character is 0 - 9"); System.out.println(); System.out.println("Syntax: Character.isDigit(character_variable)"); System.out.println("Is '" + my2dChar + "' a digit?: " + Character.isDigit(my2dChar)); my2dChar = ' '; System.out.println(); System.out.println("My character is: '" + my2dChar + "'"); System.out.println("The Character.isWhitespace() method checks to see if a character is a space"); System.out.println(); System.out.println("Syntax: Character.isWhitespace(character_variable)"); System.out.println("Is '" + my2dChar + "' a space?: " + Character.isWhitespace(my2dChar)); System.out.println(); my2dChar = 'B'; System.out.println("Syntax: Character.isLowerCase(character_variable)"); System.out.println("Is '" + my2dChar + "' a lowercase character?: " + Character.isLowerCase(my2dChar)); System.out.println(); System.out.println("Syntax: Character.isUpperCase(character_variable)"); System.out.println("Is '" + my2dChar + "' an uppercase character?: " + Character.isUpperCase(my2dChar)); System.out.println(); System.out.println("Syntax: Character.isLetter(character_variable)"); System.out.println("Is '" + my2dChar + "' a letter?: " + Character.isLetter(my2dChar)); System.out.println(); System.out.println("Typecasting converts one variable type to another"); System.out.println("Syntax: (variable to convert to) variableToConvert"); System.out.println(); System.out.println("Converting '" + my2dChar + "' to its ASCII (integer) value"); System.out.println("Syntax: (variable type to convert to) variableToConvert"); System.out.println("(int) my2dChar"); System.out.println((int)my2dChar); } } }