program will identify vowels but not consonants

can anyone tell me what is wrong with this program

Page 1 of 1

9 Replies - 8335 Views - Last Post: 07 December 2006 - 09:33 PM Rate Topic: -----

#1 cap10kid3  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 36
  • Joined: 16-August 05

program will identify vowels but not consonants

Posted 12 September 2005 - 05:54 PM

okay, this program is supposed to identify vowels and consonants, but when i type a consonant, it says "Follow directions!" what is wrong?

here's the actual code
import java.io.*;
import java.lang.*;
public class RandomStuff
{
	public static void main (String[] args) throws IOException
	{
		try{
		
		System.out.println ("Thomas Boeckman \n Program #1: charachter game \n RRHS Java class");
		playGame();
		}
		catch (NullPointerException e)
		{
			System.out.println ("ATTENTION: Under normal circumstances, this error would have gone through. However, attention to detail as been paid and this error now is invalid.");
			playGame();
		}
		
	}	   
		
	 public static void playGame() throws IOException
	 {
	 	try{
	 	
	 	String Yorn;
		
		BufferedReader keyboard;
		boolean loopNeeded;
		
		keyboard = new BufferedReader(new InputStreamReader(System.in));
		
		System.out.println ("Would you like to play the game? \n Type Y or N and press Enter");
		Yorn = keyboard.readLine();
		
		if (Yorn.equalsIgnoreCase("y"))
			enterChar();
		else if (Yorn.equalsIgnoreCase("n"))
			System.exit(1);
		
		else
		{
			System.out.println ("Follow Directions!");
			playGame();
		} 
		
	  } 
	  
	  catch (NullPointerException e)
		{
			System.out.println ("ATTENTION: Under normal circumstances, this error would have gone through. However, attention to detail as been paid and this error now is invalid.");
			playGame();
		} 
	  }
	  public static void enterChar() throws IOException
	  {
		  try{
		  
			  String letter;
			  BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
			  char x,y,z;
		  
			  final String consonant = "qwrtypsdfghjklzxcvbnm";
			  final String vowel = "aeuio";		  
				  
			  System.out.println ("Enter one letter from A to Z and press Enter");
			  letter = keyboard.readLine();
			  x = letter.charAt(0);
			  for (int t=0; t < 26; t++) 
			{

				y = vowel.charAt (t);
				 z = consonant.charAt (t);
				
			   
				if (x == z)
				{
					System.out.println ("You entered a consonant!");
					playGame();
				}
				else if (x == y)
				{
					System.out.println ("You entered a vowel!");
			   		playGame();
			   	}
				else 
				{			  
					System.out.println ("Follow directions!");
					enterChar();
			   	}
	 		}
	 		}
	 		catch (NullPointerException e)
			{
				System.out.println ("ATTENTION: Under normal circumstances, this error would have gone through. However, attention to detail as been paid and this error now is invalid.");
				playGame();
			}
	 		}
	  
		
	}

This post has been edited by jayman9: 07 December 2006 - 10:08 PM


Is This A Good Question/Topic? 0
  • +

Replies To: program will identify vowels but not consonants

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: program will identify vowels but not consonants

Posted 12 September 2005 - 06:12 PM

AS you go through the loop, the first instance will be captured, then the comparative statement is run...only an entered 'a' will match. If you enter an 'a', it will match as a vowel, but anything else will fall through, because it is trying to match against the first instance...the a. The enterChar() function is then run again. You'll need to take another look at the for loop and the logic within. I'd suggest that instead of the loop, you use the inherent string memeber functions...maybe like so:
if (consonant.indexOf(x) != -1)
{
System.out.println ("You entered a consonant!");
playGame();
}
else if (vowel.indexOf(x) != -1)
{
System.out.println ("You entered a vowel!");
playGame();
}
else
{
System.out.println ("Follow directions!");
enterChar();
}


Was This Post Helpful? 0
  • +
  • -

#3 cap10kid3  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 36
  • Joined: 16-August 05

Re: program will identify vowels but not consonants

Posted 12 September 2005 - 06:21 PM

will you tell me how to fix it?
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: program will identify vowels but not consonants

Posted 12 September 2005 - 06:21 PM

Look up
Was This Post Helpful? 0
  • +
  • -

#5 cap10kid3  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 36
  • Joined: 16-August 05

Re: program will identify vowels but not consonants

Posted 12 September 2005 - 06:36 PM

THANK YOU SOOOO MUCH!!!!
Was This Post Helpful? 0
  • +
  • -

#6 cap10kid3  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 36
  • Joined: 16-August 05

Re: program will identify vowels but not consonants

Posted 12 September 2005 - 06:49 PM

so, i still have one question, how does that work (aka what is the exclamation point for, etc.)

This post has been edited by cap10kid3: 12 September 2005 - 06:50 PM

Was This Post Helpful? 0
  • +
  • -

#7 supersloth  Icon User is online

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4502
  • View blog
  • Posts: 28,409
  • Joined: 21-March 01

Re: program will identify vowels but not consonants

Posted 12 September 2005 - 07:01 PM

'!=' means 'does not equal'
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: program will identify vowels but not consonants

Posted 12 September 2005 - 07:09 PM

Exactly...the indexOf method returns an integer that represents the letters position in the string, if it is found...so it could be from 0 to length-1. If the character being searched for is not in the string, it returns a value of -1 to indicate that, so in words, that statement says

Quote

if the character represented by x is in the string then do something

Was This Post Helpful? 0
  • +
  • -

#9 cap10kid3  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 36
  • Joined: 16-August 05

Re: program will identify vowels but not consonants

Posted 12 September 2005 - 07:41 PM

thank you both
Was This Post Helpful? 0
  • +
  • -

#10 Synapse  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-December 06

Re: program will identify vowels but not consonants

Posted 07 December 2006 - 09:33 PM

can anyone convert this to C or C++???

please....

thankx a lot
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1