Results 1 to 16 of 16
Like Tree1Likes
  • 1 Post By xcaldk74

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

  1. #1
    xcaldk74 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    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:

    Java Code:
    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
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

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

    You'll need to give us a hint of what you expected to see and what you did see for the input you gave to the program.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    xcaldk74 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    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

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

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

    And what are you getting instead?

    That sort of thing gives us an idea of where you think it is going wrong.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    xcaldk74 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

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

    Enter a line of text: aAe
    5
    1

    my loop:

    Java Code:
    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);

  6. #6
    xcaldk74 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    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

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

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

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    xcaldk74 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

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

    isn't that two different forums?

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

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

    Quote Originally Posted by xcaldk74 View Post
    isn't that two different forums?
    Yes. Have you ever volunteered at a forum, put time and effort into trying to help and answer the question only to find out later that the question was answered in a cross-post prior to your answer being posted? Do you like asking people to waste their time and do futile work?

    There's nothing wrong with cross-posting, but it's not polite to do so without informing all forums involved.

  10. #10
    xcaldk74 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

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

    Quote Originally Posted by Fubarable View Post
    Yes. Have you ever volunteered at a forum, put time and effort into trying to help and answer the question only to find out later that the question was answered in a cross-post prior to your answer being posted? Do you like asking people to waste their time and do futile work?

    There's nothing wrong with cross-posting, but it's not polite to do so without informing all forums involved.
    ok I understand - I'm sorry about that.... just little bit desperate for hints :( won't happen again
    Fubarable likes this.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

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

    You have two counters per vowel.
    You are incrementing them both when a letter matches that vowel, either upper or lower case.

    If you just need to know the number of a vowel and don't care whether it is upper or lower case then just use a single counter.
    If you need to know upper and lower case counts then you;lkl have to separate the if statement into two, one for upper and one for lower.

    And yes, they are two different forums, but it is considered rude not to inform each forum that you have asked the same question in another one. If someone in the other forum had answered this question already I would be wasting my time.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    xcaldk74 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

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

    I'm sorry about crosspost just didn't know. This is my code I think I've got it. Please let me know how would you shorten this OR make easier :(

    Thank you again for all the input guys, honestly!

    Java Code:
     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: ");
             System.out.print("");
             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 ++;
                	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 ++;
                	countTwo ++;
             }
             
             System.out.println("E: " + e);
          	
          	while(countThree<input.length())
             {
             	if(input.charAt(countThree)=='i'||input.charAt(countThree)=='I')
                		
                   i ++;
                	countThree ++;
             }
             
             System.out.println("I: " + i);
          	
          	while(countFour<input.length())
             {
             	if(input.charAt(countFour)=='o'||input.charAt(countFour)=='O')
                		
                   o ++;
                	countFour ++;
             }
             
             System.out.println("O :" + o);
          	
          	while(countFive<input.length())
             {
             	if(input.charAt(countFive)=='u'||input.charAt(countFive)=='U')
                		
                   u ++;
                	countFive ++;
             }
             
             System.out.println("U :" + u);
          }
       }

  13. #13
    xcaldk74 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

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

    OUTPUT:

    Enter a line of text: eaiou AEIOU
    A: 2
    E: 2
    I: 2
    O :2
    U :2

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

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

    Do a single loop over the array, and stick all the ifs in that loop.

    If you haven't done Maps yet then that's possibly it.
    Could maybe use an array of ints instead of 5 separate variables, though you'd need to note how you map between an index and a vowel.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    xcaldk74 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

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

    Quote Originally Posted by Tolls View Post
    Do a single loop over the array, and stick all the ifs in that loop.
    All ifs in a loop? BUt how am I going to differentiate what is a,e,i etc... in one loop? Since I need every character to be counted what I mean I have all the if's in one loop but how can I pick avery letter from just one loop?~? I don't hink i made it clear :(.

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

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

    Java Code:
    while (<condition>) {
       if (<letter is 'a' or 'A'>) increment acounter
       else if (<letter is 'e' or 'E'>) increment ecounter
    etc etc.
    }
    You could even use a case statement, if you've learnt those, since you can switch on a char.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  2. Problem with For Loop
    By dave12har in forum New To Java
    Replies: 5
    Last Post: 11-30-2010, 03:35 PM
  3. Loop problem (while)
    By roud9 in forum New To Java
    Replies: 10
    Last Post: 09-30-2010, 12:02 AM
  4. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  5. While-loop problem
    By jimmy-lin in forum New To Java
    Replies: 6
    Last Post: 11-02-2009, 03:10 AM

Posting Permissions

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