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
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
Re: Sentinel-controlled loop
according to me you can do this with do-while loop..
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 !='#');
}
}
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******");
}
}
Re: Sentinel-controlled loop
while writing your codes use
[code][code] tags..
Re: Sentinel-controlled loop
Quote:
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..
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.
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
}
Re: Sentinel-controlled loop
Quote:
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
Re: Sentinel-controlled loop
Quote:
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..
Re: Sentinel-controlled loop
Thanks for the inputs.. Appreciate it