Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Loop With Switch  RSS feed

 
Jason Collins
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not lookin for anyone to do this for me.. no this is not homework.. it is a review sheet for a test. I have read the chapters over and over again and again and wrote this program and for some reason it will not exit the loop even after the loop control variable has been met. The program was simply suppose to be a while loop that uses inside the loop a switch statement that counts the number of times a user enters in a vowel. This is my code:

public class display
{
public static void main(String[] args)
{
char x;
int a =0,
e =0,
i =0,
o =0,
u =0;
System.out.println("Enter Character.");
x = Keyboard.readChar();
while (x !=$)
{
switch (x)
{
case 'a':
case 'A':
a++;
break;
case 'e':
case 'E':
e++;
break;
case 'i':
case 'I':
i++;
break;
case 'o':
case 'O':
o++;
break;
case 'u':
case 'U':
u++;
break;
default:
System.out.println("ERROR Please enter a character!");
x = Keyboard.readChar();
}
System.out.println("Enter character.")
x = Keyboard.readChar();
}
System.out.print("A:\t" + a + "\n" + "E:\t" + e + "\n" + "I:\t" + i
+ "\n" + "O:\t" + o + "\n" + "U:\t" + u);

}
}
 
K Sujit
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Collins
Compilation of your code gave me one-two errors. like a semi colon missing in the last system.out.
errors were sorted out. And it was perfectly running.

I don't understand why gave u an infinite loop. put $ in single quotes in
while (x !=$)
and see whether it helps.
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the the UBB code tags when you post code, as it will keep your indentations. You can find a button with code on it underneath the text area you type into.
 
Jason Collins
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks alot Sorry it was my first post! I got it working ty!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!