Skip to content

Instantly share code, notes, and snippets.

@sumtyme
Forked from anonymous/LineInfo.java
Created November 20, 2012 03:46
Show Gist options
  • Save sumtyme/4115811 to your computer and use it in GitHub Desktop.
Save sumtyme/4115811 to your computer and use it in GitHub Desktop.
LineInfo help!
/**
* LineInfo class
*
* @author sinister
*
*/
public class LineInfo {
private String myLine;
public LineInfo(String line) {
myLine = line;
}
/**
* sees the number of words
*
* @Author sinister
* @return the number of words
*/
public int numWords() {
int size = 0;
String line = myLine.trim();
if (line.length() > 0) {
size = 1;
for (int i = 0; i < line.length(); i++) {
if (line.charAt(i) == ' ') {
size++;
}
}
}
return size;
}// end numWords()
/**
* sees how many occurances of str there are in myLine
*
* @author sinister
* @param str
* the string that is used
* @return the number of occurrences of str in myLine
*/
public int numOccurances(String str) {
int occ = 0;
String line = myLine.trim();
int ind = 0;
while (true) {
if (line.indexOf(str, ind) >= 0 && ind != -1) {
occ++;
ind = line.indexOf(str, ind + 1);
} else {
return occ;
}
}
}
/**
* Compares whether myLine or LineInfo otherLine has more occurances of str
*
* @author sinister
* @param str
* string that is used for comparison
* @param otherLine
* the word that is compared along with myLine
* @return the string with more occurances of str
*/
public String moreOccurances(String str, LineInfo otherLine) {
/*
* What this method should do: precondition: str is not null
* postcondition: Prints the LineInfo that has more occurances of string
* - myLine or otherLine. if they have the same # occurances, myLine is
* returned. if neither contains str, "" is returned.
*/
return "";
}
/**
* getter!
*
* @author sinister
* @return myLine the variable in question
*/
public String getInfo() {
return myLine;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment