Click to See Complete Forum and Search --> : Sentinel-controlled loop
hugo84
September 6th, 2009, 02:41 AM
Hi how can i use a sentinel controlled loop to keep prompting the user to enter the next character after it computes whether the char is a digit or a vowel till the user inputs a sentinel character '#' it will exit the program.
import java.util.Scanner;
class test1 {
public static void main(String[] args) {
Scanner zz= new Scanner(System.in);
System.out.println("Enter a character:");
String s = zz.next();
char input = s.charAt(0);
if (input=='a'||input=='e'||input=='i'||input=='o'||input=='u') {
System.out.println("Vowel");
}
else if(Character.isDigit(input))
{
System.out.println("Digit");
}
}
}
holestary
September 6th, 2009, 03:13 AM
you asked this??
import java.util.Scanner;
class test1 {
public static void main(String[] args) {
Scanner zz= new Scanner(System.in);
System.out.println("Enter a STRING:");
String s = zz.next();
//char input = s.charAt(0);
for(int k=0;k<=s.length()-1;k++)
{
if (s.charAt(k)=='a'||s.charAt(k)=='e'||s.charAt(k)=='i'||s.charAt(k)=='o'||s.charAt(k)=='u') {
System.out.println("Vowel");
}
else if(Character.isDigit(s.charAt(k)))
{
System.out.println("Digit");
}
}
}
}
output:
Enter a STRING:
1necmi6
Digit
Vowel
Vowel
Digit
hugo84
September 6th, 2009, 03:35 AM
Nope what i meant was to use a while loop then prompt for user to input character and compute whether the char is a digit or vowel. If user enter # then it will will break the loop.
So basically output will be:
Enter Character:
A
Vowel
Enter Character:
3
Digit
Enter Character:
E
Vowel
....
....
....
....
....
....
Enter Character:
#
// It will exit the program
holestary
September 6th, 2009, 05:04 AM
according to me you can do this with do-while loop..
hugo84
September 6th, 2009, 05:20 AM
im not sure i tried doing like this.. but it keeps looping forever.. please help
import java.util.Scanner;
class test1 {
public static void main(String[] args) {
Scanner zz= new Scanner(System.in);
System.out.println("Enter a character:");
String s = zz.next();
char input = s.charAt(0);
do{
if (input=='a'||input=='e'||input=='i'||input=='o'||input=='u') {
System.out.println("Vowel");
}
else if(Character.isDigit(input))
{
System.out.println("Digit");
}
}
System.out.println("Enter a Character again:");
while(input !='#');
}
}
holestary
September 6th, 2009, 05:40 AM
import java.util.Scanner;
class test1 {
public static void main(String[] args) {
Scanner zz= new Scanner(System.in);
System.out.println("Enter a character:");
String s = zz.next();
char input = s.charAt(0);
do{
if (input=='a'||input=='e'||input=='i'||input=='o'||input=='u')
System.out.println("Vowel");
else if(Character.isDigit(input))
System.out.println("Digit");
else if(input=='#')
break;
System.out.println("\nEnter a character:");
s = zz.next();
input = s.charAt(0); //new value for input
}while(input !='#');
System.out.println("\n******");
}
}
holestary
September 6th, 2009, 05:44 AM
while writing your codes use
[code][code] tags..
holestary
September 6th, 2009, 05:51 AM
im not sure i tried doing like this.. but it keeps looping forever.. please help
import java.util.Scanner;
class test1 {
public static void main(String[] args) {
Scanner zz= new Scanner(System.in);
System.out.println("Enter a character:");
String s = zz.next();
char input = s.charAt(0);
do{
if (input=='a'||input=='e'||input=='i'||input=='o'||input=='u') {
System.out.println("Vowel");
}
else if(Character.isDigit(input))
{
System.out.println("Digit");
}
}
System.out.println("Enter a Character again:");
while(input !='#');
}
}
because you didn't give new value for "input"...always it controls input's first value and loops forever..
hugo84
September 6th, 2009, 05:51 AM
Very much thanks to your help holstary. appreciate it dude. I will take note of the code posting tags very sorry about it.
holestary
September 6th, 2009, 08:29 AM
u can write this instead of do-while..
while(input !='#')
{
if (input=='a'||input=='e'||input=='i'||input=='o'||input=='u')
System.out.println("Vowel");
else if(Character.isDigit(input))
System.out.println("Digit");
else if(input=='#')
break;
System.out.println("\nEnter a character:");
s = zz.next();
input = s.charAt(0); //new value for input
}
dlorde
September 7th, 2009, 03:00 PM
u can write this instead of do-while..
... else if(input=='#')
break;
What is the point of that 'else if...' ?
Good teaching is more a giving of the right questions than a giving of the right answers...
J. Albers
holestary
September 8th, 2009, 01:33 AM
What is the point of that 'else if...' ?
... else if(input=='#')
break;
Good teaching is more a giving of the right questions than a giving of the right answers...
J. Albers
i've missed out this code..it is unnecessary while using while loop in this prog..
i know u can do this program better than me, short, etc..:)
thanks..
hugo84
September 8th, 2009, 09:56 AM
Thanks for the inputs.. Appreciate it
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.