CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Aug 2009
    Posts
    52

    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");
    }

    }
    }

  2. #2
    Join Date
    Apr 2009
    Location
    TR
    Posts
    97

    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

  3. #3
    Join Date
    Aug 2009
    Posts
    52

    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.

  4. #4
    Join Date
    Apr 2009
    Location
    TR
    Posts
    97

    Re: Sentinel-controlled loop

    according to me you can do this with do-while loop..

  5. #5
    Join Date
    Aug 2009
    Posts
    52

    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 !='#');

    }
    }

  6. #6
    Join Date
    Apr 2009
    Location
    TR
    Posts
    97

    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.

  7. #7
    Join Date
    Apr 2009
    Location
    TR
    Posts
    97

    Wink Re: Sentinel-controlled loop

    while writing your codes use

    [code][code] tags..

  8. #8
    Join Date
    Apr 2009
    Location
    TR
    Posts
    97

    Wink Re: Sentinel-controlled loop

    Quote Originally Posted by hugo84 View Post
    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..

  9. #9
    Join Date
    Aug 2009
    Posts
    52

    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.

  10. #10
    Join Date
    Apr 2009
    Location
    TR
    Posts
    97

    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
       }

  11. #11
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Sentinel-controlled loop

    Quote Originally Posted by holestary View Post
    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 &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  12. #12
    Join Date
    Apr 2009
    Location
    TR
    Posts
    97

    Thumbs up Re: Sentinel-controlled loop

    Quote Originally Posted by dlorde View Post
    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

  13. #13
    Join Date
    Aug 2009
    Posts
    52

    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
  •  





Click Here to Expand Forum to Full Width

Featured