Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: No errors, but output not correct? Only one loop performed correctly! Help please :)

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Cool 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:

    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:
    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

    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");
    		}
     
     
     
    }
    }
    }
    Last edited by coffecupcake; January 20th, 2012 at 11:22 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,350
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: No errors, but output not correct? Only one loop performed correctly! Help please

    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?

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: No errors, but output not correct? Only one loop performed correctly! Help please

    Quote Originally Posted by Norm View Post
    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:

    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.

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: No errors, but output not correct? Only one loop performed correctly! Help please

    AH! Nevermind. I needed to change a logical operator.

Similar Threads

  1. While loop not iterating correctly
    By Rhyssa6 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: May 18th, 2011, 10:13 PM
  2. I still have errors. Can you PLEASE correct my code?!
    By sam30317 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 6th, 2011, 07:10 AM
  3. [SOLVED] Code is correct, but incorrect output?
    By moodycrab3 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 6th, 2011, 02:32 PM
  4. Replies: 3
    Last Post: November 9th, 2010, 01:19 PM
  5. Need help getting things to loop correctly
    By egruna2 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 9th, 2010, 05:53 PM