Skip to content

Instantly share code, notes, and snippets.

Created November 20, 2012 03:10
Show Gist options
  • Save anonymous/4115695 to your computer and use it in GitHub Desktop.
Save anonymous/4115695 to your computer and use it in GitHub Desktop.
LineInfo help!
package com.derp.StringMethods;
/**LineInfo class
* @author sinister
*
*/
public class LineInfo {
private static String myLine = "testor";
/**
* sees the number of words
* @Author sinister
* @return the number of words
*/
public static 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 static int numOccurances(String str) { //i know occurences is spelled wrong.
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;
}
} //end while loop
} //end numOccurances()
/**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 static String moreOccurances(String str, LineInfo otherLine) { //Completely broken i have no idea how to do this.
/* 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 "";
} //end moreOccurances()
/**
* getter!
* @author sinister
* @return myLine the variable in question
*/
public static String getInfo() {
return myLine;
} //end getInfo()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment