| Author |
searching using scanner class
|
Supriya Kumar
Greenhorn
Joined: Mar 01, 2012
Posts: 5
|
|
package myproject;
import java.util.*;
public class ScanIn {
public static void main(String args[])
{
System.out.print("input:");
System.out.flush();
try
{
Scanner s = new Scanner(System.in);
String token;
do
{
token = s.findInLine(args[0]);//-------------error 12
System.out.println("found"+ token);
}
while(token != null);
}catch(Exception e )
{
System.out.println("scan exc");
}
}
}
I execute this program from KB book.One error i am getting at line error12 that " reqired java.lang.String[] but found java.lang.String". I tried to declare the token variable as array but still i am getting error.I need help in this. Thanks.
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
The code works fine for me
|
 |
Waldemar Macijewski
Ranch Hand
Joined: Jun 22, 2012
Posts: 32
|
|
|
You are going to hear this from the "veterans", but use the code tag when posting your source code, its really that simple. Although i'm beginner myself, i have copied your code into my IDE and it compiles fine. However, it looks strange from the beginning, first. your not properly closing do while loop, basically nothing happens there (do {}). Second, your not handling any exceptions, you simple print "scan exc". I would suggest for such simple example simply throw any exceptions of main(). findinLine() has two versions: one which takes Pattern object, second String object. You are providing args[0] which is first command line argument of your program, when I execute your program i get ArrayIndexOutOfBounds Exception.
|
 |
Supriya Kumar
Greenhorn
Joined: Mar 01, 2012
Posts: 5
|
|
|
Okay thanks.
|
 |
 |
|
|
subject: searching using scanner class
|
|
|