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 14 of 14

Thread: Problem with the loop ..... ;(

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with the loop ..... ;(

    Hi java masters,

    Problem with the loop where I'm not able to trace it in memory - looking for some advice/hint.

    Program prompt the user to enter a line of text and then it iwll count how many occurrences there are of the letters a,e,i,o, and u regardless of case.

    ****** problem is that I'm getting the right answer when I will remove //A and leave only a++ and by leaving as SOP(a); , when I add SOP(a+A); than I'm getting weird results. I can't trace the memory and can't find jgrasp option to show the memory so I created this loop with trials and errors.

    This is what I have so far:

    import java.util.Scanner;
       public class test
       {
          public static void main(String [] args)
          {
             Scanner keyboard = new Scanner (System.in);
     
             String input;
     
             int a,e,i,o,u,A,E,I,O,U;
             int countOne, countTwo, countThree, countFour, countFive, numVawels, size; //a, e, i, o, u;
     
             System.out.print("Enter a line of text: ");
             input = keyboard.nextLine();
     
             //size = input.length();
     
             a = 0;
             A = 0;
             e = 0;
          	E = 0;
          	i = 0;
          	I = 0;
          	o = 0;
          	O = 0;
          	u = 0;
          	U = 0;
     
             countOne = 0;
             countTwo = 0;
          	countThree = 0;
          	countFour = 0;
          	countFive = 0;
     
             while(countOne<input.length())
             {
             	if(input.charAt(countOne)=='a'||input.charAt(countOne)=='A')//||input.charAt(count)=='e'||input.charAt(count)=='i'||input.charAt(count)=='o'||input.charAt(count)=='u')
     
                   a ++;
                   A ++;
                	countOne ++;
             }
     
             System.out.println(a+A);
     
          	while(countTwo<input.length())
             {
             	if(input.charAt(countTwo)=='e'||input.charAt(countTwo)=='E')//||input.charAt(count)=='e'||input.charAt(count)=='i'||input.charAt(count)=='o'||input.charAt(count)=='u')
     
                   e ++;
                   E ++;
                	countTwo ++;
             }
     
             System.out.println(e);


  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with the loop ..... ;(

    I'm sorry for that, the input should look like this:

    Enter a line of text: aeiou AEIOU

    A: 2
    E: 2
    I: 2
    O: 2
    U: 2

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    36
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Problem with the loop ..... ;(

    From your code, a and A have the same purpose. Both of them are to counter the letters of 'a' and 'A'. So you need only one of them and can delete another one.

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with the loop ..... ;(

    Enter a line of text: aAe
    5
    1

    my loop:

    while(countOne<input.length())
             {
             	if(input.charAt(countOne)=='a'||input.charAt(countOne)=='A')//||input.charAt(count)=='e'||input.charAt(count)=='i'||input.charAt(count)=='o'||input.charAt(count)=='u')
     
                   a ++;
                   A ++;
                	countOne ++;
             }
     
             System.out.println(a+A);
     
          	while(countTwo<input.length())
             {
             	if(input.charAt(countTwo)=='e'||input.charAt(countTwo)=='E')//||input.charAt(count)=='e'||input.charAt(count)=='i'||input.charAt(count)=='o'||input.charAt(count)=='u')
     
                   e ++;
                   E ++;
                	countTwo ++;
             }
     
             System.out.println(e);

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with the loop ..... ;(

    **NOTE

    when I'm going to remove //E ++; and //A++; output appears to be correct BUT I don't get it why? since SOP states System.out.println(e); AND NOT e+A where I need to get both lower and upper case letters... ~confused

    Enter a line of text: aAeee
    2
    3

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Problem with the loop ..... ;(

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/56136-problem-loop.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. The Following User Says Thank You to KevinWorkman For This Useful Post:

    blackscholes (February 29th, 2012)

  8. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    Hi, I'm new to java as well, so somebody correct me if I'm wrong

    I would use a switch/case statement to simplify this code.

  9. #8
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    Another hint, use keyboard.toLowerCase()

  10. #9
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with the loop ..... ;(

    Quote Originally Posted by luck999 View Post
    From your code, a and A have the same purpose. Both of them are to counter the letters of 'a' and 'A'. So you need only one of them and can delete another one.
    Lucky,

    remove but from where from int? or while?

  11. #10
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with the loop ..... ;(

    Quote Originally Posted by blackscholes View Post
    Another hint, use keyboard.toLowerCase()
    Hi,
    thx for reply I don't know what that function does, my prof didn't go over this. What does it do? where do you put it?

  12. #11
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with the loop ..... ;(

    Quote Originally Posted by luck999 View Post
    From your code, a and A have the same purpose. Both of them are to counter the letters of 'a' and 'A'. So you need only one of them and can delete another one.
    Luck so you mean a and A it is the same for java? so I should remove the counter A++; just leave a++ but what about declaration a=0, A=0; leave it right?

  13. #12
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    It turns all characters in a string to lower case : D

    Its a method of the String class, so put it after input.keyboard..

  14. #13
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    A and a are definitely not the same....

  15. #14
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with the loop ..... ;(

    keyboard.toLowerCase() is a method of the String class that takes all the letters in a String and makes them lower case. It would help you a lot in your program because you won't have to worry about capital vowels, they would all be the same capitalization.

Similar Threads

  1. [SOLVED] Problem with loop?
    By Rilstin81 in forum Loops & Control Statements
    Replies: 5
    Last Post: November 23rd, 2011, 07:48 PM
  2. Do while loop problem
    By kratos75 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 27th, 2011, 06:04 AM
  3. [SOLVED] for loop problem
    By javaneedhelp in forum Loops & Control Statements
    Replies: 3
    Last Post: October 9th, 2011, 10:25 AM
  4. Problem with Loop
    By Dragonkndr712 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 8th, 2011, 12:12 PM
  5. Little Loop Problem
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 17th, 2009, 04:40 AM