This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes searching using scanner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "searching using scanner class" Watch "searching using scanner class" New topic
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: 1776
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.
 
wood burning stoves
 
subject: searching using scanner class