-
May 9th, 2014, 04:05 AM
#1
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.
-
May 9th, 2014, 06:51 AM
#2
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
-
May 9th, 2014, 07:17 AM
#3
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.
-
May 9th, 2014, 07:31 AM
#4
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
-
May 9th, 2014, 08:35 AM
#5
Re: Scanner Class
Originally Posted by Norm
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?
-
May 9th, 2014, 08:47 AM
#6
Re: Scanner Class
could be ctrl+c or ctrl+z
google it
Norm
-
May 9th, 2014, 09:10 AM
#7
Re: Scanner Class
Tried .... didnot get hence looked towards experts........ but alas!!!
-
May 9th, 2014, 09:16 AM
#8
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
-
May 9th, 2014, 09:25 AM
#9
Re: Scanner Class
Tell me how you use.... i will try to modify
-
May 9th, 2014, 09:30 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|