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

Thread: Scanner Class

Threaded View

  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.

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