Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,671 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,181 people online right now. Registration is fast and FREE... Join Now!




Vowel/consonant problem

 
Reply to this topicStart new topic

Vowel/consonant problem

stutfly
post 4 Sep, 2008 - 07:38 PM
Post #1


New D.I.C Head

*
Joined: 30 Aug, 2008
Posts: 28


My Contributions


I am having trouble the code works and runs but if I dont enter y or n at the begining or end I have to push enter twice for it to read out the follow directions warning. I think that it is a problem with my loop I have tried alot of things but none have worked please help.

CODE

/**
* @(#)VowelConst.java
*
* VowelConst application
*
* @author
* @version 1.00 2008/8/27
*/


import java.util.Scanner;

public class VowelConst {
    
    public static void main(String[] args) {
        
        
        //declared strings
    String input, input2, input3;
        
    
        
        
        //used for keyboard input
    Scanner kboard = new Scanner (System.in);
        
        
        //Title
    System.out.println("WELCOME TO THE VOWEL and CONSONANT GAME");
    System.out.println("---------------------------------------");
  
        
        
        
        // asks the user if they want to play the game
        System.out.println("\nWould you like to play the game?\n If yes enter Y or y\n If No enter N or n and press return\n\n");
         input = kboard.nextLine();
         input = input.toLowerCase();
        
        
    
        // Reapeats the program
     while (!input.equals ("n") )
     {
          
          
          
          // tells the user what to enter if y
          if (input.equals ("y") )
          System.out.println("\nPlease insert a letter EX: A or a, and press return\n");
            input2 = kboard.nextLine();
            input2 = input2.toLowerCase();
        
          
            // ends if n
            if (input.equals ("n") )
              System.out.print("\n");
        
            
      // for vowel inputs
        
          if (input2.equals ("a") ||input2.equals ("e") ||input2.equals ("i") ||input2.equals ("o") ||input2.equals ("u"))
               System.out.println("\nYou have entered a vowl\n\n");
              
              
    
              // for consonant inputs
         else if (input2.equals ("b") || input2.equals ("c") || input2.equals ("d") || input2.equals ("f") || input2.equals ("g") || input2.equals ("h")  
               || input2.equals ("j") || input2.equals ("k") || input2.equals ("l") || input2.equals ("m")  || input2.equals ("n")  || input2.equals ("p")  
               || input2.equals ("q") || input2.equals ("r") || input2.equals ("s") || input2.equals ("t") || input2.equals ("v") || input2.equals ("w")
               || input2.equals ("x") || input2.equals ("y") || input2.equals ("z")  )
               System.out.println("\nYou have entered a consonant\n\n");
      
              
            else
                //if something else is inputed
                  System.out.println("\nPlease follow directions\n\n");
                  
          
          // Asks the user if they want to play again
          System.out.println("\nWould you like to play the game?\n If yes enter Y or y\n If No enter N or n and press return\n\n");
       input = kboard.nextLine();
       input = input.toLowerCase();
      
            
         }
        
    }
}



Edited to add the [ code] tag. Please post your code like this: code.gif

This post has been edited by stutfly: 4 Sep, 2008 - 08:31 PM
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 4 Sep, 2008 - 07:56 PM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,987



Thanked 78 times

Dream Kudos: 1175
My Contributions


Maybe try something liek so:

CODE
  while (input == "y" )


that should only run if the input is "y".

HTH
User is offlineProfile CardPM

Go to the top of the page

pbl
post 4 Sep, 2008 - 07:58 PM
Post #3


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,982



Thanked 190 times

Dream Kudos: 75
My Contributions


Better to check if the answer to continue is "Y" or "y"
No need for 3 String input, you only use one at a time
Check the post jsut before/or after for the use of an array

CODE

import java.util.Scanner;

public class VowelConst {

    public static void main(String[] args) {

        //declared strings
        String input;

        //used for keyboard input
        Scanner kboard = new Scanner (System.in);

        //Title
        System.out.println("WELCOME TO THE VOWEL and CONSONANT GAME");
        System.out.println("---------------------------------------");

        // asks the user if they want to play the game
        System.out.println("\nWould you like to play the game?\n If yes enter Y or y\n If No enter N or n and press return\n\n");
        input = kboard.nextLine();
        input = input.toLowerCase();

        // Reapeats the program
        while (input.equals ("y") )     // check here if a "y" was entered
        {
            System.out.println("\nPlease insert a letter EX: A or a, and press return");
            input = kboard.nextLine();
            input = input.toLowerCase();
            // for vowel inputs
            if (input.equals ("a") ||input.equals ("e") ||input.equals ("i") ||input.equals ("o") ||input.equals ("u"))
                System.out.println("\nYou have entered a vowl\n\n");
            // for consonant inputs
            else if (input.equals ("b") || input.equals ("c") || input.equals ("d") || input.equals ("f") || input.equals ("g") || input.equals ("h")  
                    || input.equals ("j") || input.equals ("k") || input.equals ("l") || input.equals ("m")  || input.equals ("n")  || input.equals ("p")  
                    || input.equals ("q") || input.equals ("r") || input.equals ("s") || input.equals ("t") || input.equals ("v") || input.equals ("w")
                    || input.equals ("x") || input.equals ("y") || input.equals ("z")  )
                System.out.println("\nYou have entered a consonant\n\n");
            else
                //if something else is inputed
                System.out.println("\nPlease follow directions");
            // Asks the user if they want to play again
            System.out.println("\nWould you like to play the game?\n If yes enter Y or y");
            input = kboard.nextLine();
            input = input.toLowerCase();
        }
    }
}


Topic edited for a more meninful title

This post has been edited by pbl: 4 Sep, 2008 - 08:03 PM
User is offlineProfile CardPM

Go to the top of the page

stutfly
post 4 Sep, 2008 - 08:12 PM
Post #4


New D.I.C Head

*
Joined: 30 Aug, 2008
Posts: 28


My Contributions


QUOTE(BetaWar @ 4 Sep, 2008 - 08:56 PM) *

Maybe try something liek so:

CODE
  while (input == "y" )


that should only run if the input is "y".

HTH


Well I dont really want the program to just end but repeat the input.
User is offlineProfile CardPM

Go to the top of the page

stutfly
post 4 Sep, 2008 - 08:29 PM
Post #5


New D.I.C Head

*
Joined: 30 Aug, 2008
Posts: 28


My Contributions


QUOTE(pbl @ 4 Sep, 2008 - 08:58 PM) *

Better to check if the answer to continue is "Y" or "y"
No need for 3 String input, you only use one at a time
Check the post jsut before/or after for the use of an array

CODE

import java.util.Scanner;

public class VowelConst {

    public static void main(String[] args) {

        //declared strings
        String input;

        //used for keyboard input
        Scanner kboard = new Scanner (System.in);

        //Title
        System.out.println("WELCOME TO THE VOWEL and CONSONANT GAME");
        System.out.println("---------------------------------------");

        // asks the user if they want to play the game
        System.out.println("\nWould you like to play the game?\n If yes enter Y or y\n If No enter N or n and press return\n\n");
        input = kboard.nextLine();
        input = input.toLowerCase();

        // Reapeats the program
        while (input.equals ("y") )     // check here if a "y" was entered
        {
            System.out.println("\nPlease insert a letter EX: A or a, and press return");
            input = kboard.nextLine();
            input = input.toLowerCase();
            // for vowel inputs
            if (input.equals ("a") ||input.equals ("e") ||input.equals ("i") ||input.equals ("o") ||input.equals ("u"))
                System.out.println("\nYou have entered a vowl\n\n");
            // for consonant inputs
            else if (input.equals ("b") || input.equals ("c") || input.equals ("d") || input.equals ("f") || input.equals ("g") || input.equals ("h")  
                    || input.equals ("j") || input.equals ("k") || input.equals ("l") || input.equals ("m")  || input.equals ("n")  || input.equals ("p")  
                    || input.equals ("q") || input.equals ("r") || input.equals ("s") || input.equals ("t") || input.equals ("v") || input.equals ("w")
                    || input.equals ("x") || input.equals ("y") || input.equals ("z")  )
                System.out.println("\nYou have entered a consonant\n\n");
            else
                //if something else is inputed
                System.out.println("\nPlease follow directions");
            // Asks the user if they want to play again
            System.out.println("\nWould you like to play the game?\n If yes enter Y or y");
            input = kboard.nextLine();
            input = input.toLowerCase();
        }
    }
}


Topic edited for a more meninful title




I think this makes more sense to but I think my project has to be so you enter a n or N to end the program.
User is offlineProfile CardPM

Go to the top of the page

pbl
post 4 Sep, 2008 - 08:35 PM
Post #6


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,982



Thanked 190 times

Dream Kudos: 75
My Contributions


QUOTE(stutfly @ 4 Sep, 2008 - 09:29 PM) *

I think this makes more sense to but I think my project has to be so you enter a n or N to end the program.


OK so you can replace

while (input.equals ("y") )

by

while (!input.equals ("n) )

but then it will continue even if the user enters "?"


User is offlineProfile CardPM

Go to the top of the page

H3R3T1C
post 6 Sep, 2008 - 02:26 PM
Post #7


New D.I.C Head

*
Joined: 30 Mar, 2007
Posts: 8


My Contributions


lulz dude are you doing this for mr hanosh's java class?
User is offlineProfile CardPM

Go to the top of the page

stutfly
post 6 Sep, 2008 - 07:04 PM
Post #8


New D.I.C Head

*
Joined: 30 Aug, 2008
Posts: 28


My Contributions


QUOTE(H3R3T1C @ 6 Sep, 2008 - 03:26 PM) *

lulz dude are you doing this for mr hanosh's java class?


Ya, who is this?
User is offlineProfile CardPM

Go to the top of the page

H3R3T1C
post 7 Sep, 2008 - 05:03 PM
Post #9


New D.I.C Head

*
Joined: 30 Mar, 2007
Posts: 8


My Contributions


Dude this is thomas who are you stutfly
User is offlineProfile CardPM

Go to the top of the page

stutfly
post 8 Sep, 2008 - 02:16 PM
Post #10


New D.I.C Head

*
Joined: 30 Aug, 2008
Posts: 28


My Contributions


QUOTE(H3R3T1C @ 7 Sep, 2008 - 06:03 PM) *

Dude this is thomas who are you stutfly


Hey whats up this is Jordan
User is offlineProfile CardPM

Go to the top of the page

kmbweb
post 8 Sep, 2008 - 08:24 PM
Post #11


New D.I.C Head

*
Joined: 8 Sep, 2008
Posts: 2

CODE
// tells the user what to enter if y
          if (input.equals ("y") )
          System.out.println("\nPlease insert a letter EX: A or a, and press return\n");
            input2 = kboard.nextLine();
            input2 = input2.toLowerCase();


the above section is the problem with this code.

When you check for "y" you only apply the if statement to the next line - System.out....

if input is not "y" you only bypass that line and still try to read in for input2.

Add a curly brace after the if (input.equals ("y") )-
CODE

  if (input.equals ("y") )

{


          System.out.println("\nPlease insert a letter EX: A or a, and press return\n");
            input2 = kboard.nextLine();
            input2 = input2.toLowerCase();
        
        




then add the closing brace after

CODE
    || testInput.equals("x") || testInput.equals("y")
                        || testInput.equals("z"))
                    System.out.println("\nYou have entered a consonant\n\n");

            }

            else

            {

                //if something else is inputed
                System.out.println("\nPlease follow directions\n\n");
            }



this way you check for "y",

if not you only print the warning message.

QUOTE(stutfly @ 4 Sep, 2008 - 08:38 PM) *

I am having trouble the code works and runs but if I dont enter y or n at the begining or end I have to push enter twice for it to read out the follow directions warning. I think that it is a problem with my loop I have tried alot of things but none have worked please help.

CODE

/**
* @(#)VowelConst.java
*
* VowelConst application
*
* @author
* @version 1.00 2008/8/27
*/


import java.util.Scanner;

public class VowelConst {
    
    public static void main(String[] args) {
        
        
        //declared strings
    String input, input2, input3;
        
    
        
        
        //used for keyboard input
    Scanner kboard = new Scanner (System.in);
        
        
        //Title
    System.out.println("WELCOME TO THE VOWEL and CONSONANT GAME");
    System.out.println("---------------------------------------");
  
        
        
        
        // asks the user if they want to play the game
        System.out.println("\nWould you like to play the game?\n If yes enter Y or y\n If No enter N or n and press return\n\n");
         input = kboard.nextLine();
         input = input.toLowerCase();
        
        
    
        // Reapeats the program
     while (!input.equals ("n") )
     {
          
          
          
          // tells the user what to enter if y
          if (input.equals ("y") )
          System.out.println("\nPlease insert a letter EX: A or a, and press return\n");
            input2 = kboard.nextLine();
            input2 = input2.toLowerCase();
        
          
            // ends if n
            if (input.equals ("n") )
              System.out.print("\n");
        
            
      // for vowel inputs
        
          if (input2.equals ("a") ||input2.equals ("e") ||input2.equals ("i") ||input2.equals ("o") ||input2.equals ("u"))
               System.out.println("\nYou have entered a vowl\n\n");
              
              
    
              // for consonant inputs
         else if (input2.equals ("b") || input2.equals ("c") || input2.equals ("d") || input2.equals ("f") || input2.equals ("g") || input2.equals ("h")  
               || input2.equals ("j") || input2.equals ("k") || input2.equals ("l") || input2.equals ("m")  || input2.equals ("n")  || input2.equals ("p")  
               || input2.equals ("q") || input2.equals ("r") || input2.equals ("s") || input2.equals ("t") || input2.equals ("v") || input2.equals ("w")
               || input2.equals ("x") || input2.equals ("y") || input2.equals ("z")  )
               System.out.println("\nYou have entered a consonant\n\n");
      
              
            else
                //if something else is inputed
                  System.out.println("\nPlease follow directions\n\n");
                  
          
          // Asks the user if they want to play again
          System.out.println("\nWould you like to play the game?\n If yes enter Y or y\n If No enter N or n and press return\n\n");
       input = kboard.nextLine();
       input = input.toLowerCase();
      
            
         }
        
    }
}



Edited to add the [ code] tag. Please post your code like this: code.gif

User is offlineProfile CardPM

Go to the top of the page

stutfly
post 15 Sep, 2008 - 01:44 PM
Post #12


New D.I.C Head

*
Joined: 30 Aug, 2008
Posts: 28


My Contributions


QUOTE(stutfly @ 8 Sep, 2008 - 03:16 PM) *

QUOTE(H3R3T1C @ 7 Sep, 2008 - 06:03 PM) *

Dude this is thomas who are you stutfly


Hey whats up this is Jordan




Ya whats up and by the way if you didnt already know PBl is one of Mr. Hanosh's profiles
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 06:01AM

Live Java Help!

Java Tutorials

Reference Sheets