/*
* Class: CSC-123
*
* Project 1 part 1: Methods of String class
*
* Student:
*
*/
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Project1Part1
{
public static void createAndShowGUI()
{
Object[] possibleValues =
{
"1: charAt & concat", "2: Equals To ?",
"3: Equals To and Ignores Case", "4: Format Output - Date",
"5: Tell Integer Length", "6: Boolean Matches",
"7: Set Substring Length - Select Start", "8: Set Substring - From Start - End",
"9: Change Input to all LowerCase", "10: Change Input to all UpperCase",
"11: Trim and Clean Out White Space"
};
Object selectedValue = JOptionPane.showInputDialog(null,
"Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues[0]);
if (selectedValue == "1: charAt & concat")
{
doCharAt();
}
else if(selectedValue == "2: Equals To ?")
{
doEqualsTo();
}
else if(selectedValue == "3: Equals To and Ignores Case")
{
doEqualsIgnoreCase();
}
else if(selectedValue == "4: Format Output - Date")
{
doFormat();
}
else if(selectedValue == "5: Tell Integer Length")
{
doLength();
}
else if(selectedValue == "6: Boolean Matches")
{
doBooleanMatches();
}
else if(selectedValue == "7: Set Substring Length - Select Start")
{
doStartChar();
}
else if(selectedValue == "8: Set Substring - From Start - End")
{
doStartEnd();
}
else if(selectedValue == "9: Change Input to all LowerCase")
{
doConvertToLower();
}
else if(selectedValue == "10: Change Input to all UpperCase")
{
doConvertToUpper();
}
else if(selectedValue == "11: Trim and Clean Out White Space")
{
doTrim();
}
}
public static void doCharAt() //Doing charAt and concat
{
char bob;
String bill = input("Please enter a long string for some concatting and charAtting"); //accept input from user;;
String sval = input("Please input an INTEGER for charAt, remember count starts at 0"); //accept input from user;
int count = Integer.parseInt(sval);
bob = bill.charAt(count);
String sue = " Sue";
String tom = " ";
JFrame myWindow;
myWindow = new JFrame();
JOptionPane.showMessageDialog(myWindow, "Lets create an empty string for tom");
System.out.println("Lets create an empty string for tom");
tom = bill.concat(sue);
JOptionPane.showMessageDialog(myWindow, "Lets set string for bill, it is: " + bill);
System.out.println("Lets set string for bill, it is: " + bill);
JOptionPane.showMessageDialog(myWindow, "Lets charAt(count) bill and the result is: " + bob);
System.out.println("Lets charAt(count) bill and the result is: " + bob);
JOptionPane.showMessageDialog(myWindow, "Lets create string sue = Sue");
System.out.println("Lets create string sue = Sue");
JOptionPane.showMessageDialog(myWindow, "Lets make tom = bill.concat(sue), tom's value now is: " + tom);
System.out.println("Lets make tom = bill.concat(sue), tom's value now is: " + tom);
myWindow.setTitle("Tom's value is " + tom);
createAndShowGUI();
}
public static void doEqualsTo() //Doing equal to, boolean, case sensitive
{
boolean hill;
String inputValueEqualsTo1 = input("Please input case sensitive value for comparison"); //accept input from user
String inputValueEqualsTo2 = input("Please input another case sensitive value for comparison"); //accept input from user
String hay = inputValueEqualsTo1;
String hoe = inputValueEqualsTo2;
hill = hay.equals(hoe);
JFrame myWindow;
myWindow = new JFrame();
JOptionPane.showMessageDialog(myWindow, "Are both inputs equal? " + hill);
JOptionPane.showMessageDialog(myWindow, "Line in question: hill = hay.equals(hoe)");
System.out.println("Are both inputs equal? " + hill);
System.out.println("Line in question: hill = hay.equals(hoe)");
createAndShowGUI();
}
public static void doEqualsIgnoreCase() //Doing equal to, boolean, case insensitive
{
boolean joe;
String inputValueEqualsTo3 = input("Please input value for comparison"); //accept input from user
String inputValueEqualsTo4 = input("Please input another value for comparison"); //accept input from user
String zill = inputValueEqualsTo3;
String jill = inputValueEqualsTo4;
joe = zill.equalsIgnoreCase(jill);
JFrame myWindow;
myWindow = new JFrame();
JOptionPane.showMessageDialog(myWindow, "Are both inputs equal(case insensitive)? " + joe);
JOptionPane.showMessageDialog(myWindow, "Line in question: joe = zill.equalsIgnoreCase(jill)");
System.out.println("Are both inputs equal(case insensitive)? " + joe);
System.out.println("joe = zill.equalsIgnoreCase(jill)");
createAndShowGUI();
}
public static void doFormat() //Doing format, using regular expressions
{
Calendar rightNow = Calendar.getInstance();
String dateTime = String.format("The Date is: %1$tm/%1$te/%1$tY", rightNow);
JFrame myWindow;
myWindow = new JFrame();
System.out.println("Lets see the date before the String.format: " + rightNow);
JOptionPane.showMessageDialog(myWindow, "Lets show the current date in MM/DD/YYYY format: " + dateTime);
JOptionPane.showMessageDialog(myWindow, "Line in question: String.format(%1$tm/%1$te/%1$tY, rightNow)");
System.out.println(dateTime);
System.out.println("Line in question: String.format(%1$tm/%1$te/%1$tY, rightNow)");
createAndShowGUI();
}
public static void doLength() //Doing length of string
{
int size;
String sb = input("Please input string for length calculation"); //accept input from user
size = sb.length();
JFrame myWindow;
myWindow = new JFrame();
JOptionPane.showMessageDialog(myWindow, "Length of String: " + size);
JOptionPane.showMessageDialog(myWindow, "Line in question: size = sb.length()");
System.out.println ("Length of String " + size);
System.out.println ("Line in question: size = sb.length()");
createAndShowGUI();
}
public static void doBooleanMatches() //Doing boolean matches with Regular Expressions
{
boolean flag;
String zoe = input("Please enter your telephone number (XXX-XXX-XXXX)"); //accept input from user
flag = zoe.matches("[0-9]{3}-[0-9]{3}-[0-9]{4}");
JFrame myWindow;
myWindow = new JFrame();
JOptionPane.showMessageDialog(myWindow, "Correct Format ?: " + flag);
JOptionPane.showMessageDialog(myWindow, "Line in question: flag = zoe.matches(\"[0-9]{3}-[0-9]{3}-[0-9]{4}\")");
System.out.println("Correct Format ?: " + flag);
System.out.println("Line in question: flag = zoe.matches(\"[0-9]{3}-[0-9]{3}-[0-9]{4}\")");
createAndShowGUI();
}
public static void doStartChar() //Doing start from character number in string
{
String result;
String stChange = input("Please input String for substring start at 2"); //accept input from user
result = stChange.substring(2);
JFrame myWindow;
myWindow = new JFrame();
JOptionPane.showMessageDialog(myWindow, "Result from substring start at 2: " + result);
JOptionPane.showMessageDialog(myWindow, "Line in question: result = stChange.substring(2)");
System.out.println("Result from substring start at 2: " + result);
System.out.println("Line in question: result = stChange.substring(2)");
createAndShowGUI();
}
public static void doStartEnd() //Doing start and end from character string
{
String startend;
String stChangeStEnd = input("Please input string for substring start at 2 and ending at 35");
startend = stChangeStEnd.substring(2, 35);
JFrame myWindow;
myWindow = new JFrame();
JOptionPane.showMessageDialog(myWindow, "Result from substring start at 2 & ending at 35: " + startend);
JOptionPane.showMessageDialog(myWindow, "Line in question: startend = stChangeStEnd.substring(2, 35)");
System.out.println("Result from substring start at 2 & ending at 35: " + startend);
System.out.println("Line in question: startend = stChangeStEnd.substring(2, 35)");
createAndShowGUI();
}
public static void doConvertToLower() //Doing convert to lowercase
{
String convertlower;
String lowerCase = input("Please input a uppercase string");
convertlower = lowerCase.toLowerCase();
JFrame myWindow;
myWindow = new JFrame();
JOptionPane.showMessageDialog(myWindow, "Result from convert to lowercase: " + convertlower );
JOptionPane.showMessageDialog(myWindow, "Line in question: convertlower = lowerCase.toLowerCase()");
System.out.println("Result from convert to lowercase: " + convertlower);
System.out.println("Line in question: convertlower = lowerCase.toLowerCase()");
createAndShowGUI();
}
public static void doConvertToUpper() //Doing convert to uppercase
{
String convertupper;
String upperCase = input("Please input a lowercase string");
convertupper = upperCase.toUpperCase();
JFrame myWindow;
myWindow = new JFrame();
JOptionPane.showMessageDialog(myWindow, "Result from convert to uppercase: " + convertupper );
JOptionPane.showMessageDialog(myWindow, "Line in question: convertupper = upperCase.toUpperCase()");
System.out.println("Result from convert to uppercase: " + convertupper );
System.out.println("Line in question: convertupper = upperCase.toUpperCase()");
createAndShowGUI();
}
public static void doTrim() //Doing trim which cleans spaces and tabs
{
String clean;
String spacedOut = input("Please input sentence starting with many spaces");
clean = spacedOut.trim();
JFrame myWindow;
myWindow = new JFrame();
JOptionPane.showMessageDialog(myWindow, "Result from trim: " + clean );
JOptionPane.showMessageDialog(myWindow, "Line in question: clean = spacedOut.trim()");
System.out.println("Result from trim: " + clean );
System.out.println("Line in question: clean = spacedOut.trim()");
createAndShowGUI();
}
public static String input(String text)
{
return JOptionPane.showInputDialog(text);
}
public static void main(String[] args) //Main running portion
{
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}