|
-
September 6th, 2009, 02:41 AM
#1
Sentinel-controlled loop
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");
}
}
}
-
September 6th, 2009, 03:13 AM
#2
Re: Sentinel-controlled loop
you asked this??
Code:
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:
Code:
Enter a STRING:
1necmi6
Digit
Vowel
Vowel
Digit
-
September 6th, 2009, 03:35 AM
#3
Re: Sentinel-controlled loop
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
Last edited by hugo84; September 6th, 2009 at 03:37 AM.
-
September 6th, 2009, 05:04 AM
#4
Re: Sentinel-controlled loop
according to me you can do this with do-while loop..
-
September 6th, 2009, 05:20 AM
#5
Re: Sentinel-controlled loop
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 !='#');
}
}
-
September 6th, 2009, 05:40 AM
#6
Re: Sentinel-controlled loop
Code:
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******");
}
}
Last edited by holestary; September 6th, 2009 at 05:52 AM.
-
September 6th, 2009, 05:44 AM
#7
Re: Sentinel-controlled loop
while writing your codes use
[code][code] tags..
-
September 6th, 2009, 05:51 AM
#8
Re: Sentinel-controlled loop
 Originally Posted by hugo84
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..
-
September 6th, 2009, 05:51 AM
#9
Re: Sentinel-controlled loop
Very much thanks to your help holstary. appreciate it dude. I will take note of the code posting tags very sorry about it.
-
September 6th, 2009, 08:29 AM
#10
Re: Sentinel-controlled loop
u can write this instead of do-while..
Code:
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
}
-
September 7th, 2009, 03:00 PM
#11
Re: Sentinel-controlled loop
 Originally Posted by holestary
u can write this instead of do-while..
Code:
... 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
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
September 8th, 2009, 01:33 AM
#12
Re: Sentinel-controlled loop
 Originally Posted by dlorde
What is the point of that 'else if...' ?
Code:
... 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..
Last edited by holestary; September 8th, 2009 at 05:36 AM.
Please use code tags like this : [SIGPIC][/SIGPIC]
Code Your Dreams 
-
September 8th, 2009, 09:56 AM
#13
Re: Sentinel-controlled loop
Thanks for the inputs.. Appreciate it
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|