| @@ -0,0 +1,74 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| public class Sandwich_Pt1 { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println(Arrays.toString("I really really like really red apples".split("really"))); | ||
| System.out.println(Arrays.toString("I like red apples".split(" "))); | ||
| //test for Part 1 | ||
| System.out.println(sandwich_Pt1("breadmeatleetuceeadmayoketchuead")); | ||
| System.out.println(sandwich_Pt1("breadbread")); | ||
| System.out.println(sandwich_Pt1("bread")); | ||
| System.out.println(sandwich_Pt1("breadsomethingbreadmeatbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbreadotherstuff")); | ||
| System.out.println(sandwich_Pt1("breadbreadsomethingbread")); | ||
| //test for Part 2 | ||
| System.out.println(sandwich_Pt2("bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("something bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("breadbreadbread")); | ||
| //String.split(); | ||
| //It's a method that acts on a string, <StringName>.split(<String sp>); | ||
| //Where sp is the string where the string splits | ||
| //And it returns an array | ||
| // Example: "I like apples!".split(" "); | ||
| // it will split at spaces and return an array of ["I","like","apples!"] | ||
| // Example 2: "I really like really red apples"split("really") | ||
| // it will split at the word "really" and return an array of ["I "," like "," apples!"] | ||
|
|
||
| //play around with String.split! what happens if you "I reallyreally like apples".split("really") ? | ||
|
|
||
|
|
||
| //Your task: | ||
| /*Write a method that take in a string like "applespineapplesbreadlettustomatobaconmayohambreadcheese" describing a sandwich | ||
| * use String.split to split up the sandwich by the word "bread" and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * What if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
| //Your task pt 2: | ||
| /*Write a method that take in a string like "apples pineapples bread lettus tomato bacon mayo ham bread cheese" describing a sandwich | ||
| * use String.split to split up the sandwich at the spaces, " ", and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * Again, what if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| } | ||
| public static String sandwich_Pt1(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
| public static String sandwich_Pt2(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split(" "); | ||
| String answer=""; | ||
| for(int i=0;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().equals("breadbread")||answer.trim().equals("breadbreadbread")) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,72 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| public class Sandwich_Pt1 { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println(Arrays.toString("I really really like really red apples".split("really"))); | ||
| System.out.println(Arrays.toString("I like red apples".split(" "))); | ||
| //test for Part 1 | ||
| System.out.println(sandwich_Pt1("breadmeatleetuceeadmayoketchuead")); | ||
| System.out.println(sandwich_Pt1("breadbread")); | ||
| System.out.println(sandwich_Pt1("bread")); | ||
| System.out.println(sandwich_Pt1("breadsomethingbreadmeatbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbreadotherstuff")); | ||
| System.out.println(sandwich_Pt1("breadbreadsomethingbread")); | ||
| //test for Part 2 | ||
| System.out.println(sandwich_Pt2("bread mayo bread mayo bread")); | ||
| //String.split(); | ||
| //It's a method that acts on a string, <StringName>.split(<String sp>); | ||
| //Where sp is the string where the string splits | ||
| //And it returns an array | ||
| // Example: "I like apples!".split(" "); | ||
| // it will split at spaces and return an array of ["I","like","apples!"] | ||
| // Example 2: "I really like really red apples"split("really") | ||
| // it will split at the word "really" and return an array of ["I "," like "," apples!"] | ||
|
|
||
| //play around with String.split! what happens if you "I reallyreally like apples".split("really") ? | ||
|
|
||
|
|
||
| //Your task: | ||
| /*Write a method that take in a string like "applespineapplesbreadlettustomatobaconmayohambreadcheese" describing a sandwich | ||
| * use String.split to split up the sandwich by the word "bread" and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * What if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
| //Your task pt 2: | ||
| /*Write a method that take in a string like "apples pineapples bread lettus tomato bacon mayo ham bread cheese" describing a sandwich | ||
| * use String.split to split up the sandwich at the spaces, " ", and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * Again, what if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| } | ||
| public static String sandwich_Pt1(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
| public static String sandwich_Pt2(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf(" "), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=0;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().equals("breadbread")||answer.trim().equals("breadbreadbread")) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,73 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| public class Sandwich_Pt1 { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println(Arrays.toString("I really really like really red apples".split("really"))); | ||
| System.out.println(Arrays.toString("I like red apples".split(" "))); | ||
| //test for Part 1 | ||
| System.out.println(sandwich_Pt1("breadmeatleetuceeadmayoketchuead")); | ||
| System.out.println(sandwich_Pt1("breadbread")); | ||
| System.out.println(sandwich_Pt1("bread")); | ||
| System.out.println(sandwich_Pt1("breadsomethingbreadmeatbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbreadotherstuff")); | ||
| System.out.println(sandwich_Pt1("breadbreadsomethingbread")); | ||
| //test for Part 2 | ||
| System.out.println(sandwich_Pt2("bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("something bread mayo bread mayo bread")); | ||
| //String.split(); | ||
| //It's a method that acts on a string, <StringName>.split(<String sp>); | ||
| //Where sp is the string where the string splits | ||
| //And it returns an array | ||
| // Example: "I like apples!".split(" "); | ||
| // it will split at spaces and return an array of ["I","like","apples!"] | ||
| // Example 2: "I really like really red apples"split("really") | ||
| // it will split at the word "really" and return an array of ["I "," like "," apples!"] | ||
|
|
||
| //play around with String.split! what happens if you "I reallyreally like apples".split("really") ? | ||
|
|
||
|
|
||
| //Your task: | ||
| /*Write a method that take in a string like "applespineapplesbreadlettustomatobaconmayohambreadcheese" describing a sandwich | ||
| * use String.split to split up the sandwich by the word "bread" and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * What if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
| //Your task pt 2: | ||
| /*Write a method that take in a string like "apples pineapples bread lettus tomato bacon mayo ham bread cheese" describing a sandwich | ||
| * use String.split to split up the sandwich at the spaces, " ", and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * Again, what if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| } | ||
| public static String sandwich_Pt1(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
| public static String sandwich_Pt2(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf(" "), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=0;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().equals("breadbread")||answer.trim().equals("breadbreadbread")) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,77 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| public class Sandwich_Pt1 { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println(Arrays.toString("I really really like really red apples".split("really"))); | ||
| System.out.println(Arrays.toString("I like red apples".split(" "))); | ||
| //test for Part 1 | ||
| System.out.println(sandwich_Pt1("breadmeatleetuceeadmayoketchuead")); | ||
| System.out.println(sandwich_Pt1("breadbread")); | ||
| System.out.println(sandwich_Pt1("bread")); | ||
| System.out.println(sandwich_Pt1("breadsomethingbreadmeatbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbreadotherstuff")); | ||
| System.out.println(sandwich_Pt1("breadbreadsomethingbread")); | ||
| //test for Part 2 | ||
| System.out.println(sandwich_Pt2("bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("something bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("breadbreadbread")); | ||
| System.out.println(sandwich_Pt2("bread something bread bread")); | ||
| System.out.println(sandwich_Pt2("something bread bread bread")); | ||
| //String.split(); | ||
| //It's a method that acts on a string, <StringName>.split(<String sp>); | ||
| //Where sp is the string where the string splits | ||
| //And it returns an array | ||
| // Example: "I like apples!".split(" "); | ||
| // it will split at spaces and return an array of ["I","like","apples!"] | ||
| // Example 2: "I really like really red apples"split("really") | ||
| // it will split at the word "really" and return an array of ["I "," like "," apples!"] | ||
|
|
||
| //play around with String.split! what happens if you "I reallyreally like apples".split("really") ? | ||
|
|
||
|
|
||
| //Your task: | ||
| /*Write a method that take in a string like "applespineapplesbreadlettustomatobaconmayohambreadcheese" describing a sandwich | ||
| * use String.split to split up the sandwich by the word "bread" and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * What if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
| //Your task pt 2: | ||
| /*Write a method that take in a string like "apples pineapples bread lettus tomato bacon mayo ham bread cheese" describing a sandwich | ||
| * use String.split to split up the sandwich at the spaces, " ", and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * Again, what if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| } | ||
| public static String sandwich_Pt1(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
| public static String sandwich_Pt2(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split(" "); | ||
| String answer=""; | ||
| for(int i=0;i<split.length;i++) | ||
| if(!split[i].equals("bread")) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().equals("breadbread")||answer.trim().equals("breadbreadbread")) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,58 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| public class Sandwich_Pt1 { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println(Arrays.toString("I really really like really red apples".split("really"))); | ||
| System.out.println(Arrays.toString("I like red apples".split(" "))); | ||
| String test="breadmeatleetuceeadmayoketchuead"; | ||
| System.out.println(sandwich(test)); | ||
| System.out.println(sandwich("breadbread")); | ||
| System.out.println(sandwich("bread")); | ||
| System.out.println(sandwich("breadsomethingbreadmeatbread")); | ||
| System.out.println(sandwich("cheesebreadsomethingbread")); | ||
| System.out.println(sandwich("cheesebreadsomethingbreadotherstuff")); | ||
| System.out.println(sandwich("breadbreadsomethingbread")); | ||
| //String.split(); | ||
| //It's a method that acts on a string, <StringName>.split(<String sp>); | ||
| //Where sp is the string where the string splits | ||
| //And it returns an array | ||
| // Example: "I like apples!".split(" "); | ||
| // it will split at spaces and return an array of ["I","like","apples!"] | ||
| // Example 2: "I really like really red apples"split("really") | ||
| // it will split at the word "really" and return an array of ["I "," like "," apples!"] | ||
|
|
||
| //play around with String.split! what happens if you "I reallyreally like apples".split("really") ? | ||
|
|
||
|
|
||
| //Your task: | ||
| /*Write a method that take in a string like "applespineapplesbreadlettustomatobaconmayohambreadcheese" describing a sandwich | ||
| * use String.split to split up the sandwich by the word "bread" and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * What if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
| //Your task pt 2: | ||
| /*Write a method that take in a string like "apples pineapples bread lettus tomato bacon mayo ham bread cheese" describing a sandwich | ||
| * use String.split to split up the sandwich at the spaces, " ", and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * Again, what if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| } | ||
| public static String sandwich(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,72 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| public class Sandwich_Pt1 { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println(Arrays.toString("I really really like really red apples".split("really"))); | ||
| System.out.println(Arrays.toString("I like red apples".split(" "))); | ||
| //test for Part 1 | ||
| System.out.println(sandwich_Pt1("breadmeatleetuceeadmayoketchuead")); | ||
| System.out.println(sandwich_Pt1("breadbread")); | ||
| System.out.println(sandwich_Pt1("bread")); | ||
| System.out.println(sandwich_Pt1("breadsomethingbreadmeatbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbreadotherstuff")); | ||
| System.out.println(sandwich_Pt1("breadbreadsomethingbread")); | ||
| //test for Part 2 | ||
| System.out.println(sandwich_Pt2("bread mayo bread mayo bread")); | ||
| //String.split(); | ||
| //It's a method that acts on a string, <StringName>.split(<String sp>); | ||
| //Where sp is the string where the string splits | ||
| //And it returns an array | ||
| // Example: "I like apples!".split(" "); | ||
| // it will split at spaces and return an array of ["I","like","apples!"] | ||
| // Example 2: "I really like really red apples"split("really") | ||
| // it will split at the word "really" and return an array of ["I "," like "," apples!"] | ||
|
|
||
| //play around with String.split! what happens if you "I reallyreally like apples".split("really") ? | ||
|
|
||
|
|
||
| //Your task: | ||
| /*Write a method that take in a string like "applespineapplesbreadlettustomatobaconmayohambreadcheese" describing a sandwich | ||
| * use String.split to split up the sandwich by the word "bread" and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * What if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
| //Your task pt 2: | ||
| /*Write a method that take in a string like "apples pineapples bread lettus tomato bacon mayo ham bread cheese" describing a sandwich | ||
| * use String.split to split up the sandwich at the spaces, " ", and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * Again, what if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| } | ||
| public static String sandwich_Pt1(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
| public static String sandwich_Pt2(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf(" "), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().equals("breadbread")||answer.trim().equals("breadbreadbread")) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,77 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| public class Sandwich_Pt1 { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println(Arrays.toString("I really really like really red apples".split("really"))); | ||
| System.out.println(Arrays.toString("I like red apples".split(" "))); | ||
| //test for Part 1 | ||
| System.out.println(sandwich_Pt1("breadmeatleetuceeadmayoketchuead")); | ||
| System.out.println(sandwich_Pt1("breadbread")); | ||
| System.out.println(sandwich_Pt1("bread")); | ||
| System.out.println(sandwich_Pt1("breadsomethingbreadmeatbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbreadotherstuff")); | ||
| System.out.println(sandwich_Pt1("breadbreadsomethingbread")); | ||
| //test for Part 2 | ||
| System.out.println(sandwich_Pt2("bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("something bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("breadbreadbread")); | ||
| System.out.println(sandwich_Pt2("bread something bread bread")); | ||
| System.out.println(sandwich_Pt2("somethingbread bread bread")); | ||
| //String.split(); | ||
| //It's a method that acts on a string, <StringName>.split(<String sp>); | ||
| //Where sp is the string where the string splits | ||
| //And it returns an array | ||
| // Example: "I like apples!".split(" "); | ||
| // it will split at spaces and return an array of ["I","like","apples!"] | ||
| // Example 2: "I really like really red apples"split("really") | ||
| // it will split at the word "really" and return an array of ["I "," like "," apples!"] | ||
|
|
||
| //play around with String.split! what happens if you "I reallyreally like apples".split("really") ? | ||
|
|
||
|
|
||
| //Your task: | ||
| /*Write a method that take in a string like "applespineapplesbreadlettustomatobaconmayohambreadcheese" describing a sandwich | ||
| * use String.split to split up the sandwich by the word "bread" and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * What if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
| //Your task pt 2: | ||
| /*Write a method that take in a string like "apples pineapples bread lettus tomato bacon mayo ham bread cheese" describing a sandwich | ||
| * use String.split to split up the sandwich at the spaces, " ", and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * Again, what if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| } | ||
| public static String sandwich_Pt1(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
| public static String sandwich_Pt2(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split(" "); | ||
| String answer=""; | ||
| for(int i=0;i<split.length;i++) | ||
| if(!split[i].equals("bread")) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().equals("breadbread")||answer.trim().equals("breadbreadbread")) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,78 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| public class Sandwich_Pt1 { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println(Arrays.toString("I really really like really red apples".split("really"))); | ||
| System.out.println(Arrays.toString("I like red apples".split(" "))); | ||
| //test for Part 1 | ||
| System.out.println(sandwich_Pt1("breadmeatleetuceeadmayoketchuead")); | ||
| System.out.println(sandwich_Pt1("breadbread")); | ||
| System.out.println(sandwich_Pt1("bread")); | ||
| System.out.println(sandwich_Pt1("breadsomethingbreadmeatbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbreadotherstuff")); | ||
| System.out.println(sandwich_Pt1("breadbreadsomethingbread")); | ||
| System.out.println(); | ||
| //test for Part 2 | ||
| System.out.println(sandwich_Pt2("bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("something bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("bread bread bread")); | ||
| System.out.println(sandwich_Pt2("bread something bread bread")); | ||
| System.out.println(sandwich_Pt2("something bread bread bread")); | ||
| //String.split(); | ||
| //It's a method that acts on a string, <StringName>.split(<String sp>); | ||
| //Where sp is the string where the string splits | ||
| //And it returns an array | ||
| // Example: "I like apples!".split(" "); | ||
| // it will split at spaces and return an array of ["I","like","apples!"] | ||
| // Example 2: "I really like really red apples"split("really") | ||
| // it will split at the word "really" and return an array of ["I "," like "," apples!"] | ||
|
|
||
| //play around with String.split! what happens if you "I reallyreally like apples".split("really") ? | ||
|
|
||
|
|
||
| //Your task: | ||
| /*Write a method that take in a string like "applespineapplesbreadlettustomatobaconmayohambreadcheese" describing a sandwich | ||
| * use String.split to split up the sandwich by the word "bread" and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * What if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
| //Your task pt 2: | ||
| /*Write a method that take in a string like "apples pineapples bread lettus tomato bacon mayo ham bread cheese" describing a sandwich | ||
| * use String.split to split up the sandwich at the spaces, " ", and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * Again, what if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| } | ||
| public static String sandwich_Pt1(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
| public static String sandwich_Pt2(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split(" "); | ||
| String answer=""; | ||
| for(int i=0;i<split.length;i++) | ||
| if(!split[i].equals("bread")) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,77 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| public class Sandwich_Pt1 { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println(Arrays.toString("I really really like really red apples".split("really"))); | ||
| System.out.println(Arrays.toString("I like red apples".split(" "))); | ||
| //test for Part 1 | ||
| System.out.println(sandwich_Pt1("breadmeatleetuceeadmayoketchuead")); | ||
| System.out.println(sandwich_Pt1("breadbread")); | ||
| System.out.println(sandwich_Pt1("bread")); | ||
| System.out.println(sandwich_Pt1("breadsomethingbreadmeatbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbreadotherstuff")); | ||
| System.out.println(sandwich_Pt1("breadbreadsomethingbread")); | ||
| //test for Part 2 | ||
| System.out.println(sandwich_Pt2("bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("something bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("breadbreadbread")); | ||
| System.out.println(sandwich_Pt2("bread something bread bread")); | ||
| System.out.println(sandwich_Pt2(" something bread bread bread")); | ||
| //String.split(); | ||
| //It's a method that acts on a string, <StringName>.split(<String sp>); | ||
| //Where sp is the string where the string splits | ||
| //And it returns an array | ||
| // Example: "I like apples!".split(" "); | ||
| // it will split at spaces and return an array of ["I","like","apples!"] | ||
| // Example 2: "I really like really red apples"split("really") | ||
| // it will split at the word "really" and return an array of ["I "," like "," apples!"] | ||
|
|
||
| //play around with String.split! what happens if you "I reallyreally like apples".split("really") ? | ||
|
|
||
|
|
||
| //Your task: | ||
| /*Write a method that take in a string like "applespineapplesbreadlettustomatobaconmayohambreadcheese" describing a sandwich | ||
| * use String.split to split up the sandwich by the word "bread" and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * What if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
| //Your task pt 2: | ||
| /*Write a method that take in a string like "apples pineapples bread lettus tomato bacon mayo ham bread cheese" describing a sandwich | ||
| * use String.split to split up the sandwich at the spaces, " ", and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * Again, what if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| } | ||
| public static String sandwich_Pt1(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
| public static String sandwich_Pt2(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split(" "); | ||
| String answer=""; | ||
| for(int i=0;i<split.length;i++) | ||
| if(!split[i].equals("bread")) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().equals("breadbread")||answer.trim().equals("breadbreadbread")) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,79 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| public class Sandwich_Pt1 { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println(Arrays.toString("I really really like really red apples".split("really"))); | ||
| System.out.println(Arrays.toString("I like red apples".split(" "))); | ||
| //test for Part 1 | ||
| System.out.println(sandwich_Pt1("breadmeatleetuceeadmayoketchuead")); | ||
| System.out.println(sandwich_Pt1("breadbread")); | ||
| System.out.println(sandwich_Pt1("bread")); | ||
| System.out.println(sandwich_Pt1("breadsomethingbreadmeatbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbreadotherstuff")); | ||
| System.out.println(sandwich_Pt1("breadbreadsomethingbread")); | ||
| System.out.println(); | ||
| //test for Part 2 | ||
| System.out.println(sandwich_Pt2("bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("something bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("bread bread bread")); | ||
| System.out.println(sandwich_Pt2("bread something bread bread")); | ||
| System.out.println(sandwich_Pt2("something bread bread bread")); | ||
| System.out.println(sandwich_Pt2("rottenstuff bread cheese bread bread")); | ||
| //String.split(); | ||
| //It's a method that acts on a string, <StringName>.split(<String sp>); | ||
| //Where sp is the string where the string splits | ||
| //And it returns an array | ||
| // Example: "I like apples!".split(" "); | ||
| // it will split at spaces and return an array of ["I","like","apples!"] | ||
| // Example 2: "I really like really red apples"split("really") | ||
| // it will split at the word "really" and return an array of ["I "," like "," apples!"] | ||
|
|
||
| //play around with String.split! what happens if you "I reallyreally like apples".split("really") ? | ||
|
|
||
|
|
||
| //Your task: | ||
| /*Write a method that take in a string like "applespineapplesbreadlettustomatobaconmayohambreadcheese" describing a sandwich | ||
| * use String.split to split up the sandwich by the word "bread" and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * What if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
| //Your task pt 2: | ||
| /*Write a method that take in a string like "apples pineapples bread lettus tomato bacon mayo ham bread cheese" describing a sandwich | ||
| * use String.split to split up the sandwich at the spaces, " ", and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * Again, what if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| } | ||
| public static String sandwich_Pt1(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
| public static String sandwich_Pt2(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split(" "); | ||
| String answer=""; | ||
| for(int i=0;i<split.length;i++) | ||
| if(!split[i].equals("bread")) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,78 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| public class Sandwich_Pt1 { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println(Arrays.toString("I really really like really red apples".split("really"))); | ||
| System.out.println(Arrays.toString("I like red apples".split(" "))); | ||
| //test for Part 1 | ||
| System.out.println(sandwich_Pt1("breadmeatleetuceeadmayoketchuead")); | ||
| System.out.println(sandwich_Pt1("breadbread")); | ||
| System.out.println(sandwich_Pt1("bread")); | ||
| System.out.println(sandwich_Pt1("breadsomethingbreadmeatbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbreadotherstuff")); | ||
| System.out.println(sandwich_Pt1("breadbreadsomethingbread")); | ||
| System.out.println(); | ||
| //test for Part 2 | ||
| System.out.println(sandwich_Pt2("bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("something bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("breadbreadbread")); | ||
| System.out.println(sandwich_Pt2("bread something bread bread")); | ||
| System.out.println(sandwich_Pt2("something bread bread bread")); | ||
| //String.split(); | ||
| //It's a method that acts on a string, <StringName>.split(<String sp>); | ||
| //Where sp is the string where the string splits | ||
| //And it returns an array | ||
| // Example: "I like apples!".split(" "); | ||
| // it will split at spaces and return an array of ["I","like","apples!"] | ||
| // Example 2: "I really like really red apples"split("really") | ||
| // it will split at the word "really" and return an array of ["I "," like "," apples!"] | ||
|
|
||
| //play around with String.split! what happens if you "I reallyreally like apples".split("really") ? | ||
|
|
||
|
|
||
| //Your task: | ||
| /*Write a method that take in a string like "applespineapplesbreadlettustomatobaconmayohambreadcheese" describing a sandwich | ||
| * use String.split to split up the sandwich by the word "bread" and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * What if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
| //Your task pt 2: | ||
| /*Write a method that take in a string like "apples pineapples bread lettus tomato bacon mayo ham bread cheese" describing a sandwich | ||
| * use String.split to split up the sandwich at the spaces, " ", and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * Again, what if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| } | ||
| public static String sandwich_Pt1(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
| public static String sandwich_Pt2(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split(" "); | ||
| String answer=""; | ||
| for(int i=0;i<split.length;i++) | ||
| if(!split[i].equals("bread")) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().equals("breadbread")||answer.trim().equals("breadbreadbread")) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,53 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| public class Sandwich_Pt1 { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println(Arrays.toString("I really really like really red apples".split("really"))); | ||
| System.out.println(Arrays.toString("I like red apples".split(" "))); | ||
| String test="breadmeatleetuceeadmayoketchuead"; | ||
| String answer=sandwich(test); | ||
| System.out.println((answer)); | ||
| //String.split(); | ||
| //It's a method that acts on a string, <StringName>.split(<String sp>); | ||
| //Where sp is the string where the string splits | ||
| //And it returns an array | ||
| // Example: "I like apples!".split(" "); | ||
| // it will split at spaces and return an array of ["I","like","apples!"] | ||
| // Example 2: "I really like really red apples"split("really") | ||
| // it will split at the word "really" and return an array of ["I "," like "," apples!"] | ||
|
|
||
| //play around with String.split! what happens if you "I reallyreally like apples".split("really") ? | ||
|
|
||
|
|
||
| //Your task: | ||
| /*Write a method that take in a string like "applespineapplesbreadlettustomatobaconmayohambreadcheese" describing a sandwich | ||
| * use String.split to split up the sandwich by the word "bread" and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * What if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
| //Your task pt 2: | ||
| /*Write a method that take in a string like "apples pineapples bread lettus tomato bacon mayo ham bread cheese" describing a sandwich | ||
| * use String.split to split up the sandwich at the spaces, " ", and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * Again, what if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| } | ||
| public static String sandwich(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,76 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| public class Sandwich_Pt1 { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println(Arrays.toString("I really really like really red apples".split("really"))); | ||
| System.out.println(Arrays.toString("I like red apples".split(" "))); | ||
| //test for Part 1 | ||
| System.out.println(sandwich_Pt1("breadmeatleetuceeadmayoketchuead")); | ||
| System.out.println(sandwich_Pt1("breadbread")); | ||
| System.out.println(sandwich_Pt1("bread")); | ||
| System.out.println(sandwich_Pt1("breadsomethingbreadmeatbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbreadotherstuff")); | ||
| System.out.println(sandwich_Pt1("breadbreadsomethingbread")); | ||
| //test for Part 2 | ||
| System.out.println(sandwich_Pt2("bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("something bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("breadbreadbread")); | ||
| System.out.println(sandwich_Pt2("bread something bread bread")); | ||
| //String.split(); | ||
| //It's a method that acts on a string, <StringName>.split(<String sp>); | ||
| //Where sp is the string where the string splits | ||
| //And it returns an array | ||
| // Example: "I like apples!".split(" "); | ||
| // it will split at spaces and return an array of ["I","like","apples!"] | ||
| // Example 2: "I really like really red apples"split("really") | ||
| // it will split at the word "really" and return an array of ["I "," like "," apples!"] | ||
|
|
||
| //play around with String.split! what happens if you "I reallyreally like apples".split("really") ? | ||
|
|
||
|
|
||
| //Your task: | ||
| /*Write a method that take in a string like "applespineapplesbreadlettustomatobaconmayohambreadcheese" describing a sandwich | ||
| * use String.split to split up the sandwich by the word "bread" and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * What if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
| //Your task pt 2: | ||
| /*Write a method that take in a string like "apples pineapples bread lettus tomato bacon mayo ham bread cheese" describing a sandwich | ||
| * use String.split to split up the sandwich at the spaces, " ", and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * Again, what if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| } | ||
| public static String sandwich_Pt1(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
| public static String sandwich_Pt2(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split(" "); | ||
| String answer=""; | ||
| for(int i=0;i<split.length;i++) | ||
| if(!split[i].equals("bread")) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().equals("breadbread")||answer.trim().equals("breadbreadbread")) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,81 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| public class Sandwich_Pt1 { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println(Arrays.toString("I really really like really red apples".split("really"))); | ||
| System.out.println(Arrays.toString("I like red apples".split(" "))); | ||
| //test for Part 1 | ||
| System.out.println(sandwich_Pt1("breadmeatleetuceeadmayoketchuead")); | ||
| System.out.println(sandwich_Pt1("breadbread")); | ||
| System.out.println(sandwich_Pt1("bread")); | ||
| System.out.println(sandwich_Pt1("breadsomethingbreadmeatbreadmeat")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbreadotherstuff")); | ||
| System.out.println(sandwich_Pt1("breadbreadsomethingbread")); | ||
| System.out.println(); | ||
| //test for Part 2 | ||
| System.out.println(sandwich_Pt2("bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("something bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("bread bread bread")); | ||
| System.out.println(sandwich_Pt2("bread something bread bread")); | ||
| System.out.println(sandwich_Pt2("something bread bread bread")); | ||
| System.out.println(sandwich_Pt2("rottenstuff bread cheese bread bread meat")); | ||
| System.out.println(sandwich_Pt2("bread bread")); | ||
| System.out.println(sandwich_Pt2("bread")); | ||
| //String.split(); | ||
| //It's a method that acts on a string, <StringName>.split(<String sp>); | ||
| //Where sp is the string where the string splits | ||
| //And it returns an array | ||
| // Example: "I like apples!".split(" "); | ||
| // it will split at spaces and return an array of ["I","like","apples!"] | ||
| // Example 2: "I really like really red apples"split("really") | ||
| // it will split at the word "really" and return an array of ["I "," like "," apples!"] | ||
|
|
||
| //play around with String.split! what happens if you "I reallyreally like apples".split("really") ? | ||
|
|
||
|
|
||
| //Your task: | ||
| /*Write a method that take in a string like "applespineapplesbreadlettustomatobaconmayohambreadcheese" describing a sandwich | ||
| * use String.split to split up the sandwich by the word "bread" and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * What if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
| //Your task pt 2: | ||
| /*Write a method that take in a string like "apples pineapples bread lettus tomato bacon mayo ham bread cheese" describing a sandwich | ||
| * use String.split to split up the sandwich at the spaces, " ", and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * Again, what if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| } | ||
| public static String sandwich_Pt1(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
| public static String sandwich_Pt2(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split(" "); | ||
| String answer=""; | ||
| for(int i=0;i<split.length;i++) | ||
| if(!split[i].equals("bread")) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,52 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| public class Sandwich_Pt1 { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println(Arrays.toString("I really really like really red apples".split("really"))); | ||
| String test="breadmeatleetuceeadmayoketchuead"; | ||
| String answer=sandwich(test); | ||
| System.out.println((answer)); | ||
| //String.split(); | ||
| //It's a method that acts on a string, <StringName>.split(<String sp>); | ||
| //Where sp is the string where the string splits | ||
| //And it returns an array | ||
| // Example: "I like apples!".split(" "); | ||
| // it will split at spaces and return an array of ["I","like","apples!"] | ||
| // Example 2: "I really like really red apples"split("really") | ||
| // it will split at the word "really" and return an array of ["I "," like "," apples!"] | ||
|
|
||
| //play around with String.split! what happens if you "I reallyreally like apples".split("really") ? | ||
|
|
||
|
|
||
| //Your task: | ||
| /*Write a method that take in a string like "applespineapplesbreadlettustomatobaconmayohambreadcheese" describing a sandwich | ||
| * use String.split to split up the sandwich by the word "bread" and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * What if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
| //Your task pt 2: | ||
| /*Write a method that take in a string like "apples pineapples bread lettus tomato bacon mayo ham bread cheese" describing a sandwich | ||
| * use String.split to split up the sandwich at the spaces, " ", and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * Again, what if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| } | ||
| public static String sandwich(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,199 @@ | ||
| public class Magpie4 { | ||
|
|
||
| //Get a default greeting and return a greeting | ||
| public String getGreeting() { | ||
| return "Hello, let's talk."; | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| * 1) Copy and paste the code you wrote in Magpie 3 into the getResponse method. There is new code within the method | ||
| * but below where you will paste your code. Figure out what it does. | ||
| * | ||
| * 2) Copy and paste Part 3's getRandomResponse() code into the method below. | ||
| * | ||
| * 3) Alter the class as described in the handout. | ||
| * | ||
| */ | ||
|
|
||
|
|
||
| /** | ||
| * Gives a response to a user statement | ||
| * takes in user statement | ||
| * response based on the rules given | ||
| */ | ||
| public String getResponse(String statement) { | ||
| String response = ""; | ||
| if (findKeyword(statement, "no", 0) >= 0) { | ||
| response = "Why so negative?"; | ||
| } | ||
| else if (findKeyword(statement, "mother") >= 0 | ||
| || findKeyword(statement,"father") >= 0 | ||
| || findKeyword(statement,"sister") >= 0 | ||
| || findKeyword(statement,"brother") >= 0) { | ||
| response = "Tell me more about your family."; | ||
| } | ||
| else if (findKeyword(statement,"Dreyer")>=0){ | ||
| response=("Ms. Dreyer is great at teaching!!!!"); | ||
| } | ||
| else if (findKeyword(statement,"math")>=0 | ||
| ||findKeyword(statement,"physics")>=0){ | ||
| response="Yes, I love the subject!"; | ||
| } | ||
| else if (statement.trim().length()==0){ | ||
| response="Say something please."; | ||
| } | ||
| else if(findKeyword(statement,"Nathan")>=0){ | ||
| response="He is a cool person."; | ||
| } | ||
| else if(findKeyword(statement,"sport")>=0){ | ||
| response="Tennis is a fun sport to play."; | ||
| } | ||
| // Responses which require transformations | ||
| else if (findKeyword(statement, "I want to", 0) >= 0) { | ||
| response = transformIWantToStatement(statement); | ||
| } | ||
|
|
||
| else { | ||
| // Look for a two word (you <something> me) | ||
| // pattern | ||
| int psn = findKeyword(statement, "you", 0); | ||
|
|
||
| if (psn >= 0 && findKeyword(statement, "me", psn) >= 0) { | ||
| response = transformYouMeStatement(statement); | ||
| } else { | ||
| response = getRandomResponse(); | ||
| } | ||
| } | ||
| return response; | ||
| } | ||
| private int findKeyword(String statement, String goal, int startPos) { | ||
| String phrase = statement.trim(); | ||
| // The only change to incorporate the startPos is in | ||
| // the line below | ||
| int psn = phrase.toLowerCase().indexOf(goal.toLowerCase(), startPos); | ||
|
|
||
| // Refinement--make sure the goal isn't part of a | ||
| // word | ||
| while (psn >= 0) { | ||
| // Find the string of length 1 before and after | ||
| // the word | ||
| String before = " ", after = " "; | ||
| if (psn > 0) { | ||
| before = phrase.substring(psn - 1, psn).toLowerCase(); | ||
| } | ||
| if (psn + goal.length() < phrase.length()) { | ||
| after = phrase.substring(psn + goal.length(), | ||
| psn + goal.length() + 1).toLowerCase(); | ||
| } | ||
|
|
||
| // If before and after aren't letters, we've | ||
| // found the word | ||
| if (before.equals(" ") && after.equals(" ")) { | ||
| return psn; | ||
| } | ||
|
|
||
| // The last position didn't work, so let's find | ||
| // the next, if there is one. | ||
| psn = phrase.indexOf(goal.toLowerCase(), psn + 1); | ||
|
|
||
| } | ||
|
|
||
| return -1; | ||
| } | ||
| private int findKeyword(String statement, String goal) { | ||
| return findKeyword(statement, goal, 0); | ||
| } | ||
|
|
||
|
|
||
| // Paste Part 3 code here. The method has new pieces that continue below and should flow from your previous code. | ||
|
|
||
|
|
||
| // Responses which require transformations | ||
|
|
||
| /** | ||
| * Take a statement with "I want to <something>." and transform it into | ||
| * "What would it mean to <something>?" | ||
| * | ||
| * @param statement | ||
| * the user statement, assumed to contain "I want to" | ||
| * @return the transformed statement | ||
| */ | ||
| private String transformIWantToStatement(String statement) { | ||
| // Remove the final period, if there is one | ||
| statement = statement.trim(); | ||
| String lastChar = statement.substring(statement.length() - 1); | ||
| if (lastChar.equals(".")) { | ||
| statement = statement.substring(0, statement.length() - 1); | ||
| } | ||
| int psn = findKeyword(statement, "I want to", 0); | ||
| String restOfStatement = statement.substring(psn + 9).trim(); | ||
| return "What would it mean to " + restOfStatement + "?"; | ||
| } | ||
|
|
||
| /** | ||
| * Take a statement with "you <something> me" and transform it into | ||
| * "What makes you think that I <something> you?" | ||
| * | ||
| * @param statement | ||
| * the user statement, assumed to contain "you" followed by "me" | ||
| * @return the transformed statement | ||
| */ | ||
| private String transformYouMeStatement(String statement) { | ||
| // Remove the final period, if there is one | ||
| statement = statement.trim(); | ||
| String lastChar = statement.substring(statement.length() - 1); | ||
| if (lastChar.equals(".")) { | ||
| statement = statement.substring(0, statement.length() - 1); | ||
| } | ||
|
|
||
| int psnOfYou = findKeyword(statement, "you", 0); | ||
| int psnOfMe = findKeyword(statement, "me", psnOfYou + 3); | ||
|
|
||
| String restOfStatement = statement.substring(psnOfYou + 3, psnOfMe) | ||
| .trim(); | ||
| return "What makes you think that I " + restOfStatement + " you?"; | ||
| } | ||
|
|
||
| /** | ||
| * Search for one word in phrase. The search is not case sensitive. This | ||
| * method will check that the given goal is not a substring of a longer | ||
| * string (so, for example, "I know" does not contain "no"). | ||
| * | ||
| * parameter: statement | ||
| * the string to search | ||
| * parameter: goal | ||
| * the string to search for | ||
| * parameter: startPos | ||
| * the character of the string to begin the search at | ||
| * return the index of the first occurrence of goal in statement or -1 if | ||
| * it's not found | ||
| */ | ||
|
|
||
| /** | ||
| * Pick a default response to use if nothing else fits. | ||
| * | ||
| * @return a non-committal string | ||
| */ | ||
| private String getRandomResponse() { | ||
| final int NUMBER_OF_RESPONSES = 6; | ||
| double r = Math.random(); | ||
| int whichResponse = (int) (r * NUMBER_OF_RESPONSES); | ||
| String response = ""; | ||
|
|
||
| if (whichResponse == 0) { | ||
| response = "Interesting, tell me more."; | ||
| } else if (whichResponse == 1) { | ||
| response = "Hmmm."; | ||
| } else if (whichResponse == 2) { | ||
| response = "Do you really think so?"; | ||
| } else if (whichResponse == 3) { | ||
| response = "You don't say."; | ||
| } else if (whichResponse == 4) { | ||
| response = "Where are you from?"; | ||
| } else if (whichResponse == 5) { | ||
| response = "What are you saying?"; | ||
| } | ||
| return response; | ||
| } | ||
| } |
| @@ -0,0 +1,74 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| public class Sandwich_Pt1 { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println(Arrays.toString("I really really like really red apples".split("really"))); | ||
| System.out.println(Arrays.toString("I like red apples".split(" "))); | ||
| //test for Part 1 | ||
| System.out.println(sandwich_Pt1("breadmeatleetuceeadmayoketchuead")); | ||
| System.out.println(sandwich_Pt1("breadbread")); | ||
| System.out.println(sandwich_Pt1("bread")); | ||
| System.out.println(sandwich_Pt1("breadsomethingbreadmeatbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbreadotherstuff")); | ||
| System.out.println(sandwich_Pt1("breadbreadsomethingbread")); | ||
| //test for Part 2 | ||
| System.out.println(sandwich_Pt2("bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("something bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("breadbreadbread")); | ||
| //String.split(); | ||
| //It's a method that acts on a string, <StringName>.split(<String sp>); | ||
| //Where sp is the string where the string splits | ||
| //And it returns an array | ||
| // Example: "I like apples!".split(" "); | ||
| // it will split at spaces and return an array of ["I","like","apples!"] | ||
| // Example 2: "I really like really red apples"split("really") | ||
| // it will split at the word "really" and return an array of ["I "," like "," apples!"] | ||
|
|
||
| //play around with String.split! what happens if you "I reallyreally like apples".split("really") ? | ||
|
|
||
|
|
||
| //Your task: | ||
| /*Write a method that take in a string like "applespineapplesbreadlettustomatobaconmayohambreadcheese" describing a sandwich | ||
| * use String.split to split up the sandwich by the word "bread" and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * What if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
| //Your task pt 2: | ||
| /*Write a method that take in a string like "apples pineapples bread lettus tomato bacon mayo ham bread cheese" describing a sandwich | ||
| * use String.split to split up the sandwich at the spaces, " ", and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * Again, what if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| } | ||
| public static String sandwich_Pt1(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
| public static String sandwich_Pt2(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split(" "); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().equals("breadbread")||answer.trim().equals("breadbreadbread")) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,58 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| public class Sandwich_Pt1 { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println(Arrays.toString("I really really like really red apples".split("really"))); | ||
| System.out.println(Arrays.toString("I like red apples".split(" "))); | ||
| String test="breadmeatleetuceeadmayoketchuead"; | ||
| System.out.println(sandwich(test)); | ||
| System.out.println(sandwich("breadbread")); | ||
| System.out.println(sandwich("bread")); | ||
| System.out.println(sandwich("breadsomethingbreadmeatbread")); | ||
| System.out.println(sandwich("cheesebreadsomethingbread")); | ||
| System.out.println(sandwich("cheesebreadsomethingbreadotherstuff")); | ||
| System.out.println("breadbreadsomethingbread"); | ||
| //String.split(); | ||
| //It's a method that acts on a string, <StringName>.split(<String sp>); | ||
| //Where sp is the string where the string splits | ||
| //And it returns an array | ||
| // Example: "I like apples!".split(" "); | ||
| // it will split at spaces and return an array of ["I","like","apples!"] | ||
| // Example 2: "I really like really red apples"split("really") | ||
| // it will split at the word "really" and return an array of ["I "," like "," apples!"] | ||
|
|
||
| //play around with String.split! what happens if you "I reallyreally like apples".split("really") ? | ||
|
|
||
|
|
||
| //Your task: | ||
| /*Write a method that take in a string like "applespineapplesbreadlettustomatobaconmayohambreadcheese" describing a sandwich | ||
| * use String.split to split up the sandwich by the word "bread" and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * What if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
| //Your task pt 2: | ||
| /*Write a method that take in a string like "apples pineapples bread lettus tomato bacon mayo ham bread cheese" describing a sandwich | ||
| * use String.split to split up the sandwich at the spaces, " ", and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * Again, what if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| } | ||
| public static String sandwich(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,50 @@ | ||
|
|
||
| public class Sandwich_Pt1 { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| String test="breadmeatleetuceeadmayoketchuead"; | ||
| String answer=sandwich(test); | ||
| System.out.println((answer)); | ||
| //String.split(); | ||
| //It's a method that acts on a string, <StringName>.split(<String sp>); | ||
| //Where sp is the string where the string splits | ||
| //And it returns an array | ||
| // Example: "I like apples!".split(" "); | ||
| // it will split at spaces and return an array of ["I","like","apples!"] | ||
| // Example 2: "I really like really red apples"split("really") | ||
| // it will split at the word "really" and return an array of ["I "," like "," apples!"] | ||
|
|
||
| //play around with String.split! what happens if you "I reallyreally like apples".split("really") ? | ||
|
|
||
|
|
||
| //Your task: | ||
| /*Write a method that take in a string like "applespineapplesbreadlettustomatobaconmayohambreadcheese" describing a sandwich | ||
| * use String.split to split up the sandwich by the word "bread" and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * What if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
| //Your task pt 2: | ||
| /*Write a method that take in a string like "apples pineapples bread lettus tomato bacon mayo ham bread cheese" describing a sandwich | ||
| * use String.split to split up the sandwich at the spaces, " ", and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * Again, what if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| } | ||
| public static String sandwich(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,72 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| public class Sandwich_Pt1 { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println(Arrays.toString("I really really like really red apples".split("really"))); | ||
| System.out.println(Arrays.toString("I like red apples".split(" "))); | ||
| //test for Part 1 | ||
| System.out.println(sandwich_Pt1("breadmeatleetuceeadmayoketchuead")); | ||
| System.out.println(sandwich_Pt1("breadbread")); | ||
| System.out.println(sandwich_Pt1("bread")); | ||
| System.out.println(sandwich_Pt1("breadsomethingbreadmeatbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbreadotherstuff")); | ||
| System.out.println(sandwich_Pt1("breadbreadsomethingbread")); | ||
| //test for Part 2 | ||
| System.out.println(sandwich_Pt2("bread mayo bread mayo bread")); | ||
| //String.split(); | ||
| //It's a method that acts on a string, <StringName>.split(<String sp>); | ||
| //Where sp is the string where the string splits | ||
| //And it returns an array | ||
| // Example: "I like apples!".split(" "); | ||
| // it will split at spaces and return an array of ["I","like","apples!"] | ||
| // Example 2: "I really like really red apples"split("really") | ||
| // it will split at the word "really" and return an array of ["I "," like "," apples!"] | ||
|
|
||
| //play around with String.split! what happens if you "I reallyreally like apples".split("really") ? | ||
|
|
||
|
|
||
| //Your task: | ||
| /*Write a method that take in a string like "applespineapplesbreadlettustomatobaconmayohambreadcheese" describing a sandwich | ||
| * use String.split to split up the sandwich by the word "bread" and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * What if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
| //Your task pt 2: | ||
| /*Write a method that take in a string like "apples pineapples bread lettus tomato bacon mayo ham bread cheese" describing a sandwich | ||
| * use String.split to split up the sandwich at the spaces, " ", and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * Again, what if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| } | ||
| public static String sandwich_Pt1(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
| public static String sandwich_Pt2(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf(" "), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=2;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().equals("breadbread")||answer.trim().equals("breadbreadbread")) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,76 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| public class Sandwich_Pt1 { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println(Arrays.toString("I really really like really red apples".split("really"))); | ||
| System.out.println(Arrays.toString("I like red apples".split(" "))); | ||
| //test for Part 1 | ||
| System.out.println(sandwich_Pt1("breadmeatleetuceeadmayoketchuead")); | ||
| System.out.println(sandwich_Pt1("breadbread")); | ||
| System.out.println(sandwich_Pt1("bread")); | ||
| System.out.println(sandwich_Pt1("breadsomethingbreadmeatbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbread")); | ||
| System.out.println(sandwich_Pt1("cheesebreadsomethingbreadotherstuff")); | ||
| System.out.println(sandwich_Pt1("breadbreadsomethingbread")); | ||
| //test for Part 2 | ||
| System.out.println(sandwich_Pt2("bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("something bread mayo bread mayo bread")); | ||
| System.out.println(sandwich_Pt2("breadbreadbread")); | ||
| System.out.println(sandwich_Pt2("breadsomethingbreadbread")); | ||
| //String.split(); | ||
| //It's a method that acts on a string, <StringName>.split(<String sp>); | ||
| //Where sp is the string where the string splits | ||
| //And it returns an array | ||
| // Example: "I like apples!".split(" "); | ||
| // it will split at spaces and return an array of ["I","like","apples!"] | ||
| // Example 2: "I really like really red apples"split("really") | ||
| // it will split at the word "really" and return an array of ["I "," like "," apples!"] | ||
|
|
||
| //play around with String.split! what happens if you "I reallyreally like apples".split("really") ? | ||
|
|
||
|
|
||
| //Your task: | ||
| /*Write a method that take in a string like "applespineapplesbreadlettustomatobaconmayohambreadcheese" describing a sandwich | ||
| * use String.split to split up the sandwich by the word "bread" and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * What if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
| //Your task pt 2: | ||
| /*Write a method that take in a string like "apples pineapples bread lettus tomato bacon mayo ham bread cheese" describing a sandwich | ||
| * use String.split to split up the sandwich at the spaces, " ", and return what's in the middle of the sandwich and ignores what's on the outside | ||
| * Again, what if it's a fancy sandwich with multiple pieces of bread? | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| } | ||
| public static String sandwich_Pt1(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split("bread"); | ||
| String answer=""; | ||
| for(int i=1;i<split.length;i++) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().length()==0) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
| public static String sandwich_Pt2(String messedUpSandwich){ | ||
| if(messedUpSandwich.indexOf("bread")<0||messedUpSandwich.indexOf("bread")==messedUpSandwich.lastIndexOf("bread")) | ||
| return "Not a sandwich!"; | ||
| String orderedSandwich=messedUpSandwich.substring(messedUpSandwich.indexOf("bread"), messedUpSandwich.lastIndexOf("bread")); | ||
| String split[] =orderedSandwich.split(" "); | ||
| String answer=""; | ||
| for(int i=0;i<split.length;i++) | ||
| if(!split[i].equals("bread")) | ||
| answer=answer+split[i]; | ||
| if (answer.trim().equals("breadbread")||answer.trim().equals("breadbreadbread")) | ||
| return "Not a sandwich!"; | ||
| return answer; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1 @@ | ||
| ����fingerprintactionbugIdbugUrlmessage needinfos |
| @@ -0,0 +1 @@ | ||
| NRM�j|x|h]jao|||||vphypk|b|||s|nr|g||\md|lpii|chjbrhl|befhg||||th||p||lgcyxji|sk|||yntmd|[|lrippy|pv|q|r|hyn|||xildy|xocq|uyjjxtmpc|kjhhx|upuuuuuuuuuuuuuu|tiu|y||ntpr|l|t|u|x||tw||t|mmo||r|s|vv||||p|||t|x||t||p|nqaxa||ldvddvh|`lpqqs|tgte||tqxtr||rx|||uq||u|lyudc\|k\[|dj\uqw|su||hlotxwxvtx||r||t|v|w|wvv||v|||||tf\|chtsqg|gnvxxwvx|pttttyy|ramaaaamxlylqioppnqs|s|pjpolpp||dnpppppojpeevnpiru|w|cuttjy|rrrw||t||i|lmfi||i|geir|yl||||||u|ig||lkvke||||||ml|||elrxm|x||n|y||||||||bk||j||vjh|mv||r|w|h|w|h|pw|y||m`y|lp|px|jfu|v||yv||xyux||n||y||x|||c|h||ylpl|n|u|h|||guxcchy|u|s||||ho||v|v|te||||s||h|xxdsvu|u|o|u||||oi||||qcd|idnv||q`t||x|n||lqf|j|k||g||||t|rkqw|||||gp|tu|||t|||h|y|j||c|||a|||||||h|y|v|u|hu|y|||s|||m|mpy|||x|||cfxy||||||||||kx|||nl||k||v||||||||||||||pkwxyw||||||w||p|||||w|||qu|my||||htq|||l||mnx||||lny|||v|n||||||||xyrx|||l||u|||l|||||y|y|p|||||||||ppv|u||||p|u|rt|l||t|x||||||y|kx|||ony||r|qr|vwy|||||x||t||ryxy|||q|||qf|t||x|vxhyp|y||hl|||fu|k|vrk|yfh||i|||v|px|o||||xnin|p|||p|q|||x|||rl|q||||||luv|twp|yxtw|||nxm|||v|x|w|h|v|vv|||x|toxp||y|||||y||m||y|||q||pr|yw|r||wd|||||||||||q||n||||||||j||n||y||px|r|||l|||||||||kp|puv||v|||||nvgir||||i|ftrt||axxldp|||g|y|xc|j|||w|lxopf||||||yfv|||xi|||||cf||r||||tuerymxp|wy||||u|n|l|tyyxxvxsr|x||||nwy|p||||||t||||u|||y|||o|y|rqyl|||||rm|||w|tw|xx||t||y||vx|vt|||||ham||pq|o|x|mx||u|||yn|n|||y|xxxsay|||rjh|jtnowyx|qsx|||yt||qryv|ty|c|||||s||ty|os||st||e||`|||cxw|lr||r|x|m|cup|lxy|tqu|x|||||v|||||u||||t||qwpyyw||uyv||t|x|ytexy|k|sxsxnyyt|yy|yn||w|j||ufyxy||||yr||||xk||||up|mxyyvwyv|yxy||y|||y||xxxxyx|xxx||gxuYpxy|wtqu|pymvxom||yy||rm||whvqktvyxt|l|xn||oyylx||v||xtix||yvtyyv|x||ty|xxv|rt`|qkert||yy||wx|w||wuxyy|yyvvyyoq||||rtsy|noqur||yy|t|rvyuuu||y|||yrsuruymxy|tuytt|xtx||x||w|||x|vtywvyy||xr|||imt||xt|vx||wuyy|yx|v|u|x|||yn|xuh|y||||||x|||y|yvyx||x|y|xr||u||mf|||afordicdbefdhfddafoplm`e|qatmoklihhrgcqfkghnhmlcghkq`ikhffhlioefjpkkg`i|hablaliinbihnl|fgiqgp|lkhktsih|itioqjoqeki|ekexj|vyhlkimpd|mqpb|||iihigmmdjwxtm|lh|||l||kvm||j|kkagng||gm|jgpcc|bvgsu||`x|n||m||f||ixoyxty|m|vx|httnvlf|||ll|jjm|lnvp|umnxosmx|||||rtx|s|x||jjxy||||ps|n|v|s|uojjhgj|esiu|lg|txuqyyxxv||rjy|uukym|||||||t|uvstxp||sw|ww|yxt||ov|wqdlsmxqrvx|ywtl||y|xx||y|hyyyyyr|||q|yyy|w|y|yy|||xyy|x|||ytx|y|y|x|y|y|||||q|r||xjj||i||||lcl|j|hdsumhbe||jwy|n|utikl||^gutamxm|jioou|jp|mkpqi|g|p|sfj|cxehneiikn|inylfympponrooxtri||ghmkgh|e|n\t||pnhh|hmxbs|njhq`|imrgu|pkqy|yvnptixsxp|jtv||oudpg|urjp||iqkdsdd|w||pnjtlj|u|jp|mmmxjihkh|q|bd|f|n|l|kafphohnl|`pfqkftij||nhipmlkf|opn||sp||uhigk||unnpso|wrsssjfdqm||qh|n|||||vvbv|||p|myppn|ed`wjmp_hhgomxscfcifgwqhjjlejqljhkgtmknmhijnekglqlmlnrnmpg|krwqqop|wuymtyuqlo|xxxxupxmpttu|srqrgvpxqmyvweprqmtkmfnoomgtphutwtmpmssipoljqnxrtrvpaqvvt|ympklppxpptksyu|tvnusmkmtxrtsoppxup|vsbiqvqqtvptruwuwkpswy|yuxuqrvurnusutqqrx||struspurpxwrutrvuvnl|qxqetx|w|qosvutustuilxwtvsrlvs|xrwivspspry||ptrroxkpnnxqrtovwrt`nrttwtrr|sytvvsrtutvxpqtpxvurrxx|yilxqqtsupvqmvjtusrqruwoyotxq|qoyplittxxsn|rquv|seutsrswtlspv|ytxxxvt||yhhut|xxtuvrtv|yvvy|y|\u||yvx|qtuwvx|xvqvvtrrv|xrutyxyyvvsvuur|||y|tr|xxxxwyoq|kuyyxxtxyv|qprxwxxxx||mnqsxqvuty|||xtxwwutvrtvxvwqouyxvuxsrx|wtttxttxwxmxyxx|tyy|vxxyyyxyx||uyxvpxytstytsqtvuxyxyvv||||e|yynrx|x|u|yyxtttu||l|yyyyxxxw|vty|qxxu||yyxu|||rt|vywyv|wxvyyvxxx||xrxv|uswvyysq|||uxwuyyyy||xxyyyy|x|||yyp|||yuvuus|tyy|x|xix|xyxxxyyxxvwo|wywuyyt||niw|y|||||u|||ywy|yyxgxx|y||yyy|kqt||up||||u|sq||yy|xmmmp|qyx|yy|yy||nn|||nmqqxy|yy|vv|vuvxyxyxxxyh|q|vy|yvy||xx|x||kmy||xxyxtyy|xxxww|x|||xyx|vx|x|y|x|||xpxx|wx||||xs||lvxxyvxyw|yy|x|q|ty|v|x|wv|yxr|y|vxwxy|r|xhyx|||x|vwxw||||yyyy||||||||||t|xy||||wypy||ww|xx||xxh||fjqgeng||abpnxiu||xpyx|y||||yy|||||xwuvu|xvvuwyy|v|||||y|yy||||x||rl|||xwuyyy|yyrlq||yyy||wur||x|x||yyx||uuwxt||u|xy|ttyy||||xyty|wr||||pyurxw|vy|||||uyvy|w|||pyyy||yxyyyyx|wx||xyx|||||q|xyx|||xyxx|y||y||yfijec`d|klobaitj|xf|k||f|||i||md||ehk^|||m|iojcmyhl|hkoesvq||m|`pf||h||rbyn|d`srwo`mg|i|||||qjke|cgohmpdtiej|fwqp|_igr|xnoqknlejf|qidaet|shfvkqhrhhtgrvhnkddi|hilnf|hmlo|n|i|vnhiwh|sihmtjgmkg|gphmiijdeoowdhei|qhhuegnnwixikn|gdp|||mn|tgkldmf|tk|iyuxi|s|qthrfosmtmumvdphoitvm|jxmr||xtsyfmml||lq|wu|pp|fanqisfnnk|tkkp|^pm`feqqx|ntp|mhwliqgdhfnkrqfchjgeigiag|rxjhftf|it|a||ehjiwehsukinmhvsgjx||xtvxuou||svvynrglcfxhs|iuty|yhriuuam|hrlqidm||ehhou|x|jpnkqdh||xrymwa|v|ho`tydhqiuxd|uihd|pi||yv||||h|nq|||j|p|||vj|||tbpnwxt|sbl|tt|p|||||h||w|t||n|m|hh|u|jy||qsdl|vxu||is`p|p`i|oovusq||n|p|rphwolow||tr|rml|||qhox|nllpeyqp|vt|tptjs|pp`|tx|m|t||lo|tp||ruotrfw|rurrnxrywp|ro||||t|w||uql|tov|y||qho|f||lv||i||xwm|t|mjlrrpmluuptngq|ni|q|u||glsssjmp||q||y|||||vp|xqxy||y|||xtyexyxl|ewsxtjtfux|vy|tm||p|y||t|bukhwqxuuqvlvxv|y|xhmdquf|mvp|st||uwq|wxyxp|yyxryuyxujxx|llyx|ut|bx||y||qlhw||jxttu||t||wfy|t|||wtqivtprwoyx|ptqxxxxtypr|tywy|pypyyq|ywx|vxx|||wyx||yy||||xxx||||xtxi|r||opvppm|||y|l|strr|||wwx|||||| |
| @@ -1,3 +1,3 @@ | ||
| #Tue Nov 01 22:26:37 PDT 2016 | ||
| org.eclipse.core.runtime=2 | ||
| org.eclipse.platform=4.6.0.v20160606-1100 |
| @@ -1 +1 @@ | ||
| [[{"location":"C:\\Program Files\\Java\\jre1.8.0_101","type":"JRE","hints":{"EXECUTION_ENVIRONMENT":"JavaSE-1.8"}},"jre:jre:1.8.0"]] |