Comp103 - Java
Sentence App
SentenceApp.java
public class Sentence{
public int countEs(){
int count = 0;
int i = 0;
while (true) {
i = sentence.indexOf('e',i);
if (i == -1)
break;
i++;
count++;
}
return count;
}
String sentence;
int firstSpace;
int secondSpace;
int countS;
public Sentence(String input){
sentence = input;
}
public String returnString(){
return sentence;
}
public int countSpaces(){
int countS = 0;
int j = 0;
while (true) {
j = sentence.indexOf(' ',j);
if (j == -1)
break;
j++;
countS++;
}
return countS;
}
public void doStuff(){
if (countSpaces() >= 2){
System.out.println("The string is: " + sentence);
System.out.println("Lowercase: " + (sentence = sentence.toLowerCase()));
System.out.println("First character is: " + sentence.charAt(0));
System.out.println("First letter e is at position: " + sentence.indexOf('e'));
System.out.println("Last character is: " + sentence.charAt(sentence.length()-1));
System.out.println("Spaces replaced: " + (sentence = sentence.replace (' ', '$')));
System.out.println("The first word is: " + sentence.substring(0, (sentence.indexOf('$'))));
System.out.println("The first space is at position: " +(firstSpace = sentence.indexOf('$')));
System.out.println("The second space is at position: " + (secondSpace = sentence.indexOf('$',(sentence.indexOf('$')+1))));
System.out.println("The second word is: " + sentence.substring((firstSpace+1),secondSpace));
System.out.println("The number of times e appears in the string is: " + countEs());
}
else if(countSpaces() == 1){
System.out.println("The string is: " + sentence);
System.out.println("Lowercase: " + (sentence = sentence.toLowerCase()));
System.out.println("First character is: " + sentence.charAt(0));
System.out.println("First letter e is at position: " + sentence.indexOf('e'));
System.out.println("Last character is: " + sentence.charAt(sentence.length()-1));
System.out.println("Spaces replaced: " + (sentence = sentence.replace (' ', '$')));
System.out.println("The first word is: " + sentence.substring(0, (sentence.indexOf('$'))));
System.out.println("The space is at position: " +(firstSpace = sentence.indexOf('$')));
System.out.println("The number of times e appears in the string is: " + countEs());
}
else if((countSpaces() == 0) || (sentence != null)){
System.out.println("The string is: " + sentence);
System.out.println("Lowercase: " + (sentence = sentence.toLowerCase()));
System.out.println("First letter e is at position: " + sentence.indexOf('e'));
System.out.println("Spaces replaced: " + (sentence = sentence.replace (' ', '$')));
System.out.println("The space is at position: " +(firstSpace = sentence.indexOf('$')));
System.out.println("The number of times e appears in the string is: " + countEs());
}
}
}
Sentence.java
public class Sentence{
public int countEs(){
int count = 0;
int i = 0;
while (true) {
i = sentence.indexOf('e',i);
if (i == -1)
break;
i++;
count++;
}
return count;
}
String sentence;
int firstSpace;
int secondSpace;
int countS;
public Sentence(String input){
sentence = input;
}
public String returnString(){
return sentence;
}
public int countSpaces(){
int countS = 0;
int j = 0;
while (true) {
j = sentence.indexOf(' ',j);
if (j == -1)
break;
j++;
countS++;
}
return countS;
}
public void doStuff(){
if (countSpaces() >= 2){
System.out.println("The string is: " + sentence);
System.out.println("Lowercase: " + (sentence = sentence.toLowerCase()));
System.out.println("First character is: " + sentence.charAt(0));
System.out.println("First letter e is at position: " + sentence.indexOf('e'));
System.out.println("Last character is: " + sentence.charAt(sentence.length()-1));
System.out.println("Spaces replaced: " + (sentence = sentence.replace (' ', '$')));
System.out.println("The first word is: " + sentence.substring(0, (sentence.indexOf('$'))));
System.out.println("The first space is at position: " +(firstSpace = sentence.indexOf('$')));
System.out.println("The second space is at position: " + (secondSpace = sentence.indexOf('$',(sentence.indexOf('$')+1))));
System.out.println("The second word is: " + sentence.substring((firstSpace+1),secondSpace));
System.out.println("The number of times e appears in the string is: " + countEs());
}
else if(countSpaces() == 1){
System.out.println("The string is: " + sentence);
System.out.println("Lowercase: " + (sentence = sentence.toLowerCase()));
System.out.println("First character is: " + sentence.charAt(0));
System.out.println("First letter e is at position: " + sentence.indexOf('e'));
System.out.println("Last character is: " + sentence.charAt(sentence.length()-1));
System.out.println("Spaces replaced: " + (sentence = sentence.replace (' ', '$')));
System.out.println("The first word is: " + sentence.substring(0, (sentence.indexOf('$'))));
System.out.println("The space is at position: " +(firstSpace = sentence.indexOf('$')));
System.out.println("The number of times e appears in the string is: " + countEs());
}
else if((countSpaces() == 0) || (sentence != null)){
System.out.println("The string is: " + sentence);
System.out.println("Lowercase: " + (sentence = sentence.toLowerCase()));
System.out.println("First letter e is at position: " + sentence.indexOf('e'));
System.out.println("Spaces replaced: " + (sentence = sentence.replace (' ', '$')));
System.out.println("The space is at position: " +(firstSpace = sentence.indexOf('$')));
System.out.println("The number of times e appears in the string is: " + countEs());
}
}
}
public class Sentence{
public int countEs(){
int count = 0;
int i = 0;
while (true) {
i = sentence.indexOf('e',i);
if (i == -1)
break;
i++;
count++;
}
return count;
}
String sentence;
int firstSpace;
int secondSpace;
int countS;
public Sentence(String input){
sentence = input;
}
public String returnString(){
return sentence;
}
public int countSpaces(){
int countS = 0;
int j = 0;
while (true) {
j = sentence.indexOf(' ',j);
if (j == -1)
break;
j++;
countS++;
}
return countS;
}
public void doStuff(){
if (countSpaces() >= 2){
System.out.println("The string is: " + sentence);
System.out.println("Lowercase: " + (sentence = sentence.toLowerCase()));
System.out.println("First character is: " + sentence.charAt(0));
System.out.println("First letter e is at position: " + sentence.indexOf('e'));
System.out.println("Last character is: " + sentence.charAt(sentence.length()-1));
System.out.println("Spaces replaced: " + (sentence = sentence.replace (' ', '$')));
System.out.println("The first word is: " + sentence.substring(0, (sentence.indexOf('$'))));
System.out.println("The first space is at position: " +(firstSpace = sentence.indexOf('$')));
System.out.println("The second space is at position: " + (secondSpace = sentence.indexOf('$',(sentence.indexOf('$')+1))));
System.out.println("The second word is: " + sentence.substring((firstSpace+1),secondSpace));
System.out.println("The number of times e appears in the string is: " + countEs());
}
else if(countSpaces() == 1){
System.out.println("The string is: " + sentence);
System.out.println("Lowercase: " + (sentence = sentence.toLowerCase()));
System.out.println("First character is: " + sentence.charAt(0));
System.out.println("First letter e is at position: " + sentence.indexOf('e'));
System.out.println("Last character is: " + sentence.charAt(sentence.length()-1));
System.out.println("Spaces replaced: " + (sentence = sentence.replace (' ', '$')));
System.out.println("The first word is: " + sentence.substring(0, (sentence.indexOf('$'))));
System.out.println("The space is at position: " +(firstSpace = sentence.indexOf('$')));
System.out.println("The number of times e appears in the string is: " + countEs());
}
else if((countSpaces() == 0) || (sentence != null)){
System.out.println("The string is: " + sentence);
System.out.println("Lowercase: " + (sentence = sentence.toLowerCase()));
System.out.println("First letter e is at position: " + sentence.indexOf('e'));
System.out.println("Spaces replaced: " + (sentence = sentence.replace (' ', '$')));
System.out.println("The space is at position: " +(firstSpace = sentence.indexOf('$')));
System.out.println("The number of times e appears in the string is: " + countEs());
}
}
}
public class Sentence{
public int countEs(){
int count = 0;
int i = 0;
while (true) {
i = sentence.indexOf('e',i);
if (i == -1)
break;
i++;
count++;
}
return count;
}
String sentence;
int firstSpace;
int secondSpace;
int countS;
public Sentence(String input){
sentence = input;
}
public String returnString(){
return sentence;
}
public int countSpaces(){
int countS = 0;
int j = 0;
while (true) {
j = sentence.indexOf(' ',j);
if (j == -1)
break;
j++;
countS++;
}
return countS;
}
public void doStuff(){
if (countSpaces() >= 2){
System.out.println("The string is: " + sentence);
System.out.println("Lowercase: " + (sentence = sentence.toLowerCase()));
System.out.println("First character is: " + sentence.charAt(0));
System.out.println("First letter e is at position: " + sentence.indexOf('e'));
System.out.println("Last character is: " + sentence.charAt(sentence.length()-1));
System.out.println("Spaces replaced: " + (sentence = sentence.replace (' ', '$')));
System.out.println("The first word is: " + sentence.substring(0, (sentence.indexOf('$'))));
System.out.println("The first space is at position: " +(firstSpace = sentence.indexOf('$')));
System.out.println("The second space is at position: " + (secondSpace = sentence.indexOf('$',(sentence.indexOf('$')+1))));
System.out.println("The second word is: " + sentence.substring((firstSpace+1),secondSpace));
System.out.println("The number of times e appears in the string is: " + countEs());
}
else if(countSpaces() == 1){
System.out.println("The string is: " + sentence);
System.out.println("Lowercase: " + (sentence = sentence.toLowerCase()));
System.out.println("First character is: " + sentence.charAt(0));
System.out.println("First letter e is at position: " + sentence.indexOf('e'));
System.out.println("Last character is: " + sentence.charAt(sentence.length()-1));
System.out.println("Spaces replaced: " + (sentence = sentence.replace (' ', '$')));
System.out.println("The first word is: " + sentence.substring(0, (sentence.indexOf('$'))));
System.out.println("The space is at position: " +(firstSpace = sentence.indexOf('$')));
System.out.println("The number of times e appears in the string is: " + countEs());
}
else if((countSpaces() == 0) || (sentence != null)){
System.out.println("The string is: " + sentence);
System.out.println("Lowercase: " + (sentence = sentence.toLowerCase()));
System.out.println("First letter e is at position: " + sentence.indexOf('e'));
System.out.println("Spaces replaced: " + (sentence = sentence.replace (' ', '$')));
System.out.println("The space is at position: " +(firstSpace = sentence.indexOf('$')));
System.out.println("The number of times e appears in the string is: " + countEs());
}
}
}
