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:
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);
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.
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
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.
Re: Problem with the loop ..... ;(
Enter a line of text: aAe
5
1
my loop:
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);
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
Re: Problem with the loop ..... ;(
Re: Problem with the loop ..... ;(
isn't that two different forums?
Re: Problem with the loop ..... ;(
Quote:
Originally Posted by
xcaldk74
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.
Re: Problem with the loop ..... ;(
Quote:
Originally Posted by
Fubarable
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
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.
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!
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);
}
}
Re: Problem with the loop ..... ;(
OUTPUT:
Enter a line of text: eaiou AEIOU
A: 2
E: 2
I: 2
O :2
U :2
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.
Re: Problem with the loop ..... ;(
Quote:
Originally Posted by
Tolls
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 :(.
Re: Problem with the loop ..... ;(
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.