Created
October 23, 2017 15:46
-
-
Save mcao/3ad660bc81be3ad87a9dd1304ed9bf3d to your computer and use it in GitHub Desktop.
StringProject.java created by mcao - https://repl.it/NEj7/38
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Class: StringProject | |
* Author: Michael Cao | |
* Tutorial for the Advanced Computer Science StringProject Lab. Not the actual answers! | |
**/ | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
// First we need to initialize the classes we need. In this class we only need the Scanner class to get input from the console. | |
Scanner input = new Scanner(System.in); //This statement initializes the Scanner class and sets it up to recieve input from the console. | |
// This is where you get the data you need to work with, with a simple console prompt and retriving input to a String variable. | |
System.out.print("Please input your location in the format 'City, State Zip':"); //This prints a statement prompting you for your location. | |
String location = input.nextLine(); //This retrives the next line you type into the console and stores it into a new String variable. | |
// Ok, it's time to get into the nitty gritty String processing. Hold on to your seats and ask me for any questions you have! | |
int comma = location.indexOf(","); //Finds the location of the first comma and stores the location in a variable. This will be important for the next thing. | |
String stateAndZip = location.substring(comma + 2, location.length()); //This cuts out everything before the first space for easier String processing. | |
//The +1 in the comma is essential because we want to start retrieving | |
//at the first character of the state instead of at the comma before the state! | |
//If we used the location of the first space, it would break when we put in locations | |
//with more than one space in the name. | |
int secondSpace = stateAndZip.indexOf(" "); //Gets the location of the second space in the original string, but in relation to the stateAndZip variable we made. | |
//That means that the number in the secondSpace variable is the location of the first space in the stateAndZip | |
//variable, a.k.a. the space between the state and zip code! | |
// Now it's time to actually get the stuff that the lab wants you to get! | |
String city = location.substring(0, comma); //This substring method gets the section of the inputted string that contains the city. | |
//It basically slices the string into the part from the beginning to the location of the comma. | |
//0 is where the string starts, and comma indicates that we want the String up until the location of the comma. | |
System.out.println("City:\t" + city); //Print out the first thing the lab wants you to get: the city! | |
String state = stateAndZip.substring(0, secondSpace); //This substring method gets the section of the stateAndZip string we created that contains the state. | |
//It works because when we initialized the secondSpace method, we got the location of the second space | |
//of the entered string but using the stateAndZip string so it will work with the stateAndZip variable. | |
System.out.println("State:\t" + state); //Print out the second thing the lab wants you to get: the state! | |
String zipCode = stateAndZip.substring(secondSpace + 1, stateAndZip.length()); //This substring method gets the last thing we need. The zipCode! | |
//It basically uses the location of the space between the state and zip code, | |
//and goes one after it to the beginning of the zip code, then slices that part out. | |
System.out.println("Zip:\t" + zipCode); //Print out the last thing we need: the zip code! We're done! \ | |
/** | |
* Please only use this code for educational purposes and don't copy it directly. I don't want Mrs. Garda breathing down my neck for providing answers! | |
* I'll contine providing these tutorials for lab exercises in the future, but less and less as the year goes on. Don't hesistate to text me or send me a | |
* message on Snapchat if you have any questions about the code! I'm here to help you all get amazing grades in Java and hopefully make it so you can enjoy | |
* the class and programming in general. Programming is fun if you know how to do it and like a good challenge! | |
**/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment