Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

searching using scanner class

 
Supriya Kumar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code works fine for me
 
Waldemar Macijewski
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic