CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Scanner Class

  1. #1
    Join Date
    Aug 2011
    Posts
    7

    Scanner Class

    Dear All

    Please help me out. This is just a simple porgram in BjueJ where the program accepts a sentence & a word. Returns the number of times the word exists in the sentence using the Scanner class. The code is given below


    import java.util.* ;

    public class ScannerClass1
    {
    public void main()
    {
    int c =0 ;
    Scanner sc = new Scanner (System.in); // accept input from std input keyboard
    System.out.print("Enter Sentence: ");
    String s=sc.nextLine();
    System.out.print("Enter Search Word: ");
    String ss=sc.nextLine();
    while(sc.hasNext())
    { String d=sc.next();
    if (d.equals(ss))
    {
    c++;
    }

    }
    System.out.println("Search word exists " + c +" times") ;
    }
    }


    The program is accepting the inputs, but not processing further, getting into a continuous loop. Have to use crtl+shift+r to reset.
    Last edited by amarjitamarNew1; May 9th, 2014 at 04:16 AM.

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Scanner Class

    Please edit the post and wrap the code in code tags to make it readable.
    Unformatted code is very hard to read. Nested statements (inside {}s) should be indented.
    Norm

  3. #3
    Join Date
    Aug 2011
    Posts
    7

    Re: Scanner Class

    Code:
    import java.util.* ;
    
    public class ScannerClass1
    {
       public void main()
       {
        int c =0 ;   
        Scanner sc = new Scanner (System.in); // accept input from std input keyboard
        System.out.print("Enter Sentence: ");
        String s=sc.nextLine();
        System.out.print("Enter Search Word: ");
        String ss=sc.nextLine();
        while(sc.hasNext())
         {  String d=sc.next();
             if (d.equals(ss))
                {
                    c++;
                }
             
            }
        System.out.println("Search word exists " + c +" times") ;  
        }
    }
    Last edited by amarjitamarNew1; May 9th, 2014 at 07:19 AM.

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Scanner Class

    The API doc says:
    This method may block while waiting for input

    That implies that the program will continue forever until it is told there is no more input.

    Is there a keyboard combination that will close the keyboard for input so the program knows there isn't any more?
    Norm

  5. #5
    Join Date
    Aug 2011
    Posts
    7

    Re: Scanner Class

    Quote Originally Posted by Norm View Post
    The API doc says:
    This method may block while waiting for input

    That implies that the program will continue forever until it is told there is no more input.

    Is there a keyboard combination that will close the keyboard for input so the program knows there isn't any more?

    How?? any example?

  6. #6
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Scanner Class

    could be ctrl+c or ctrl+z
    google it
    Norm

  7. #7
    Join Date
    Aug 2011
    Posts
    7

    Re: Scanner Class

    Tried .... didnot get hence looked towards experts........ but alas!!!

  8. #8
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Scanner Class

    Sorry, i never use console input like that. I always use a sentinel value or use a GUI.
    I'm sure its CTRL+? where ? is a letter like c or z
    What combinations did you try?
    Norm

  9. #9
    Join Date
    Aug 2011
    Posts
    7

    Re: Scanner Class

    Tell me how you use.... i will try to modify

  10. #10
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Scanner Class

    Use a specific String to end the input. Say <exit>. When user enters <exit>, exit the loop
    Norm

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