No errors, but output not correct? Only one loop performed correctly! Help please :)
Here's the code! I've gotten everything to work properly, and all my if statements are correct- but my output is strange and I can't figure out why it's doing this:
Code :
Original String: XQAC ZVM
w/ Suffix: XQCZVM
w/ Plural: XQACCH
Original String: PDAE AV
Original String: SNIC SY
Original String: AAAB BB
My intention was to get 'Original String', 'w/ Suffix', and'w/ Plural' outputs for each. Somehow I only got it for the first line of input data. Please help!
Input data txt document looks like this:
Code :
XQAC ZVM
PDAE AV
SNIC SY
AAAB BB
Here's the code:
(I apologize for the poor formatting, you probably should repaste it into the IDE of your choice :S
Code :
import java.io.*;
import java.util.StringTokenizer;
import java.util.HashSet;
public class Vowels{
private static FileInputStream inputFile;
private static InputStreamReader inputReader;
private static BufferedReader lineReader;
private static StringTokenizer tokenizer;
public static void main(String args[]) throws IOException
{
//Methods
importFile();
sortInfo();
}
public static void importFile() throws IOException
{
//Data Input
inputFile = new FileInputStream ("D:\\My Documents\\Senior\\APCompsci\\VHS APCS\\Data\\blooby.txt");
inputReader = new InputStreamReader (inputFile);
lineReader = new BufferedReader (inputReader);
}
public static void sortInfo() throws IOException
{
//Line can't be null to begin.
String line = new String("start");
while (line != null){
line = lineReader.readLine();
//Stops process when there is no more information in the input file.
if (line == null){
break;
}
tokenizer = new StringTokenizer(line, " ");
String stem = tokenizer.nextToken();
String suffix = tokenizer.nextToken();
//Test
System.out.println("Original String: "+ stem +" "+ suffix);
//Hashset for comparing
HashSet vowelSet = new HashSet();
vowelSet.add("A");
vowelSet.add("C");
vowelSet.add("S");
vowelSet.add("L");
//Determining endings!
//Check for vowels at end of string (specifically last two characters)
if ((vowelSet.contains(stem.substring(2,3)) == true) && (vowelSet.contains(stem.substring(3,4)) == true)) {
//If last letter is vowel...
if (vowelSet.contains(stem.substring(3, 4)) == true){
//Checking if second to last letter is vowel.
if (vowelSet.contains(stem.substring(2, 3)) == true){
//'STEM ENDS IN DOUBLE VOWEL'
//Add Suffix - Starts with Consonant;
/*Drop the leftmost letter of the final sequence of vowels, then add the suffix*/
if (vowelSet.contains(suffix.substring(0,1)) == false){
StringBuffer buf = new StringBuffer(stem);
buf.replace(2, 3, "");
String stemFinal = buf.toString();
String finalstr = new String (stemFinal + suffix);
System.out.println("w/ Suffix: "+finalstr);
}
//Add Suffix - Starts with Vowel;
/*Add the first letter of the suffix, then add the suffix*/
if (vowelSet.contains(suffix.substring(0,1)) == true){
StringBuffer buf = new StringBuffer(suffix);
char suffixLetter = suffix.charAt(0);
buf.insert(1, suffixLetter);
String suffixFinal = buf.toString();
String finalstr = new String (stem+suffixFinal);
System.out.println("w/ Suffix: "+finalstr);
}
//Add Plural - Double last letter, add 'H'.
StringBuffer buf = new StringBuffer(stem);
char stemLetter = stem.charAt(3);
buf.insert(4, stemLetter);
String finalL = buf.toString();
System.out.println("w/ Plural: "+ finalL + "H");
//But, if not, ends in single vowel.
}else if (vowelSet.contains(stem.substring(2, 3)) == false){
//INSERT CODE FOR 'STEM ENDS IN SINGLE VOWEL'
//Add Suffix - Starts with Consonant;
//Add the first letter of the suffix and then add the suffix.
if (vowelSet.contains(suffix.substring(0,1)) == false){
StringBuffer buf = new StringBuffer(suffix);
char suffixLetter = suffix.charAt(0);
buf.insert(0, suffixLetter);
String suffixFinal = buf.toString();
String finalstr = new String (stem + suffixFinal);
System.out.println("w/ Suffix: "+finalstr);
}
//Add Suffix - Starts with Vowel;
//Drop the first letter of the suffix and add the rest of the suffix
if (vowelSet.contains(suffix.substring(0,1)) == true){
StringBuffer buf = new StringBuffer(suffix);
buf.deleteCharAt(0);
System.out.println(buf);
String suffixFinal = buf.toString();
System.out.println("w/ Suffix: "+stem+suffixFinal);
}
//Plural
//Drop the final vowel and add ‘G’
StringBuffer buf = new StringBuffer(stem);
buf.deleteCharAt(3);
String finalP = buf.toString();
System.out.println("w/ Plural: "+ finalP + "G");
}
//If last letter is not vowel...
}else if (vowelSet.contains(stem.substring(3, 4)) == false){
//INSERT CODE FOR 'ENDS IN SINGLE CONSONANT'
//Add Suffix - Starts with Consonant/Vowel (doesn't matter)
//Simply add the suffix.
System.out.println("w/ Suffix: "+ stem + suffix);
//Plural
//Add “GH”
System.out.println("w/ Plural: "+ stem +"GH");
}
//No vowels. Must be double consonants.
}else if ((vowelSet.contains(stem.substring(2,3)) == false) && (vowelSet.contains(stem.substring(3,4)) == false)){
//INSERT CODE FOR 'STEM ENDS IN DOUBLE CONSONANT' - - - SAME AS DOUBLE VOWEL
//'STEM ENDS IN DOUBLE VOWEL'
//'STEM ENDS IN DOUBLE VOWEL'
//Add Suffix - Starts with Consonant;
/*Drop the leftmost letter of the final sequence of vowels, then add the suffix*/
if (vowelSet.contains(suffix.substring(0,1)) == false){
StringBuffer buf = new StringBuffer(stem);
buf.replace(2, 3, "");
String stemFinal = buf.toString();
String finalstr = new String (stemFinal + suffix);
System.out.println("w/ Suffix: "+finalstr);
}
//Add Suffix - Starts with Vowel;
/*Add the first letter of the suffix, then add the suffix*/
if (vowelSet.contains(suffix.substring(0,1)) == true){
StringBuffer buf = new StringBuffer(suffix);
char suffixLetter = suffix.charAt(0);
buf.insert(1, suffixLetter);
String suffixFinal = buf.toString();
String finalstr = new String (stem+suffixFinal);
System.out.println("w/ Suffix: "+finalstr);
}
//Add Plural - Double last letter, add 'H'.
StringBuffer buf = new StringBuffer(stem);
char stemLetter = stem.charAt(3);
buf.insert(4, stemLetter);
String finalL = buf.toString();
System.out.println("w/ Plural: "+ finalL + "H");
}
}
}
}
Re: No errors, but output not correct? Only one loop performed correctly! Help please
Quote:
but my output is strange and I can't figure out why it's doing this:
Is the problem that only the first line read has the Suffix and Prefix lines following it?
Are those two lines correct?
What should the output look like?
Re: No errors, but output not correct? Only one loop performed correctly! Help please
Quote:
Originally Posted by
Norm
Is the problem that only the first line read has the Suffix and Prefix lines following it?
Are those two lines correct?
What should the output look like?
The output should be:
Code :
Original String: XQAC ZVM
w/ Suffix: XQCZVM
w/ Plural: XQACCH
Original String: PDAE AV
w/ Suffix: PDAEAV
w/ Plural: PDAEGH
Original String: SNIC SY
w/ Suffix: SNICY
w/ Plural: SNIG
Original String: AAAB BB
w/ Suffix: AAABBB
w/ Plural: AAABGH
So, the first three lines are correct, as well as the other 'Original String' lines. I just can't figure out why '/w Plural:' and 'w/ Suffix' statements aren't appearing as well for the other Original Strings.
Re: No errors, but output not correct? Only one loop performed correctly! Help please
AH! Nevermind. I needed to change a logical operator.