• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Loop With Switch

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);

}
}
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks alot Sorry it was my first post! I got it working ty!
 
I AM MIGHTY! Especially when I hold this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic