Page 2 of 2 FirstFirst 12
Results 21 to 29 of 29

Thread: Vowels

  1. #21
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  2. #22
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default Re: Vowels

    to be honest...i'm really not getting this. Im using the internet for ideas and then i take some of those ideas and mix them together. If it weren't for the internet....i would be screwed....

  3. #23
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default Re: Vowels

    ok....so now that everything adds up correctly...how on earth do I ask the user if they want to do another string of characters using the while loop???

  4. #24
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default Re: Vowels

    ok i created this palindrome tester from my book that uses a while loop and asks the user if they want to do another one.
    Java Code:
    while (left < right)
        	{
        		charLeft = str.charAt(left);
        		charRight = str.charAt(right);
    
    
        		if (charLeft == charRight)
        		{
        			left++;
        			right--;
        		}
    
        		else if (charLeft == ',' || charLeft == '.' ||
     charLeft == '-' || charLeft == ':' ||
     charLeft == ';' || charLeft == ' ')
    
        			left++;
    
        		else if (charRight == ',' || charRight == '.' ||
     charRight == '-' || charRight == ':' ||
     charRight == ';' || charRight == ' ')
        			right--;
        		else
    
        			break;
    
        	}
    
        System.out.println();
    
    
        	if (left < right)
        		System.out.println ("That string is NOT a palindrome.");
                    		else
    
        		System.out.println ("That string IS a palindrome.");
    
    
            System.out.println();
    
        System.out.print ("Test another palindrome (y/n)? ");
    
        another = scan.nextLine();
        }
    
     }
    But how would I use that with my my vowels program?

  5. #25
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default Re: Vowels

    Alright I think I got it now.....here it is
    [CODE]
    package vowels;


    import java.util.Scanner;

    public class Main
    {
    public static void main(String[] args)
    {
    String strCount, another = "y";
    int aCount=0, eCount=0, iCount=0, oCount=0, uCount=0, count = 0;
    Scanner scan = new Scanner (System.in);

    while (another.equalsIgnoreCase("y"))
    {

    System.out.println("Please Enter a string of characters: ");
    strCount = scan.nextLine();


    char vowels = 0;

    for (count = 0; count < strCount.length(); count++)
    {
    vowels = strCount.charAt(count);

    switch (vowels)
    {

    case 'a':
    case 'A':
    aCount++;
    break;

    case 'e':
    case 'E':
    eCount++;
    break;


    case 'i':
    case 'I':
    iCount++;
    break;


    case 'o':
    case 'O':
    oCount++;
    break;


    case 'u':
    case 'U':
    uCount++;
    break;

    }

    }
    System.out.println ("a: " + aCount);
    System.out.println ("e: " + eCount);
    System.out.println ("i: " + iCount);
    System.out.println ("o: " + oCount);
    System.out.println ("u: " + uCount);
    System.out.println ("Other characters = " + (strCount.length() - (aCount + eCount + iCount + oCount + uCount)));
    System.out.println ();
    System.out.print ("Would you like to enter another string (y/n)? ");
    another = scan.nextLine();

    }
    }
    }
    And here is the output.....

    Please Enter a string of characters:
    Hello there
    a: 0
    e: 3
    i: 0
    o: 1
    u: 0
    Other characters = 7

    Would you like to enter another string (y/n)? y
    Please Enter a string of characters:
    My name is ryan
    a: 2
    e: 4
    i: 1
    o: 1
    u: 0
    Other characters = 7

    Would you like to enter another string (y/n)? n
    BUILD SUCCESSFUL (total time: 1 minute 25 seconds)

  6. #26
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default Re: Vowels

    Nevermind i didn't get it...ugh...................now the prob is that it is counting all the vowels and other characters from the previous string.........ugh........what do I need to do now!!?? x(

  7. #27
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Vowels

    You need to remember that all that needs to be looped, needs to be inside of the loop. So either declare all of those variables inside of the loop at the beginning of the loop so that they are created anew each time the loop iterates, or create them before the loop as you are doing, but initialize them, set them to default values inside of the loop at the start of the loop.

    You are making good progress, trust me. Keep this up and you will be one of the smart ones in your class.

  8. #28
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default Re: Vowels

    I thought I did intitalize them all???

  9. #29
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Vowels

    Quote Originally Posted by hockey101 View Post
    I thought I did intitalize them all???
    You do -- but the key is where do you initialize them? If you initialize them before the while loop, then they won't be reset with each iteration of the loop. If on the other hand you initialize them inside of the while loop, then the variables will be reset. So look at your code above (you may wish to fix the code tags too), and see where you initialize:

    Java Code:
    // do you initialize your variables here, before the while loop?
    
    while (somethingIsTrue) {
       // or do you initialize them here inside and at the start of the while loop?
    
       // ..... the rest of the while loop code has been deleted for sake of brevity
    }

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Counting Vowels, getting an error
    By gabrielpr12 in forum New To Java
    Replies: 8
    Last Post: 01-07-2012, 07:50 PM
  2. Replies: 2
    Last Post: 11-08-2010, 08:01 AM
  3. counting the vowels in a sentence
    By huntrguy102 in forum New To Java
    Replies: 3
    Last Post: 11-08-2010, 06:31 AM
  4. Replacing Vowels in a word.
    By mklprasad in forum Advanced Java
    Replies: 1
    Last Post: 10-05-2009, 12:31 PM
  5. Counting Vowels and Constonants
    By MattN in forum New To Java
    Replies: 3
    Last Post: 11-20-2007, 05:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •