help with Scanner class please.

i've been trying to use some of the scanner readLine() and hasNext() and all is working so far, but i have run across a problem (probibly a silly one) but hopefully someone can help me. Using the if statement with the scanner can anyone help me create a staement that says if (scanner.readLine("something") == true) {

do something

}

so basically. if you find "something" in the "string" do this... else if bla bla bla..

does that make sence..? i know that an if staement is after a boolean but i'm sure i've done something like this before..? any help would be great..

thank you.

[622 byte] By [chiller149a] at [2007-10-2 5:56:35]
# 1

sorry.. i meant

if ((scanner.findInLine("something")) == true){

do this;

} else if {

do something else;

} else {

go to sleep;

}

chiller149a at 2007-7-16 2:05:41 > top of Java-index,Java Essentials,Java Programming...
# 2

Look at the API documentation of Scanner.findInLine(String pattern).

You will see in the documentation that that method does not return a boolean (true or false), but a String. You can't compare a String to a boolean directly, so you can't use "== true" here.

Also note that the argument to the findInLine() method is a regular expression pattern, not just a string.

It's easier to do it like this:

String line = scanner.nextLine();

if (line.indexOf("something") != -1) {

System.out.println("Found the text in the line");

}

else {

System.out.println("Go to sleep");

}

Lookup the various classes and methods in the API documentation to understand how this works.

jesperdja at 2007-7-16 2:05:41 > top of Java-index,Java Essentials,Java Programming...
# 3

It's not clear whether you are asking a question about method findInLine,

Scanner in general, or just how to write an if statement. Anyhowdy, here

is an example of all three. Note that findInLine returns the match, if any,

not a boolean.

import java.util.Scanner;

public class ScannerExample {

public static void main(String[] args) {

Scanner s = new Scanner("a 99 b\nc 888 777 d\nno numbers\n66 the end");

while (s.hasNextLine()) {

String match;

if ((match = s.findInLine("\\d+")) == null) {

System.out.println("no match in [" + s.nextLine() + "]");

} else {

System.out.println("match [" + match + "]");

System.out.println("rest of line [" + s.nextLine() + "]");

}

}

}

}

BigDaddyLoveHandlesa at 2007-7-16 2:05:41 > top of Java-index,Java Essentials,Java Programming...
# 4

Thanks for you help really worked out.. ish..

this is what i have..

public void translate()

{

String JIll = "Render { Box ( 1,1, 9, 9 ) /n Circle (1,5, 9)}";

Scanner scan = new Scanner(JIll);

while (!done)

{

while(scan.hasNextLine()) {

String match;

if ((match = scan.findInLine("Box")) == null) {

System.out.println("No match");

} else {

System.out.println("match = " + match );

System.out.println("rest of line = " + scan.nextLine() );

}

}

}

basically i want to be able to read the string line by line and if "box" is found then do something.. else if circle is found then do something etc..

it kindof works with that code i have but it doesn't give me a new search with the new line \n for some reason like in your example..?

thanks

chiller149a at 2007-7-16 2:05:41 > top of Java-index,Java Essentials,Java Programming...
# 5
Notice that I called nextLine in either case.
BigDaddyLoveHandlesa at 2007-7-16 2:05:41 > top of Java-index,Java Essentials,Java Programming...
# 6

Ah yes.. i have that working but now for a new problem..

i can read in line by line. but if i change the code a little to check for a word and if the word is not found check for anoth.. and another etc.. for some reason it doesn't continue in the loop..

my code.

while(scan.hasNextLine()) {

String match = "";

if ((match = scan.findInLine("Box")) == (match)) {

System.out.println("Found box");

System.out.println(scan.nextLine());

} else if ((match = scan.findInLine("Circle")) == (match)) {

System.out.println("Found circle");

System.out.println(scan.nextLine());

} else if ((match = scan.findInLine(("Circle")||("Box")) == null) {

System.out.println("Nothing Found, match =" + match);

} else {

System.out.println("No Match");

}

}

sorry to keep bugging you guys, but i need a way of checking line by line for 2 or 3 "key" words and performing an action dependant on which one is found.. I thought the above code would have worked. i was wrong..any ideas.?

chiller149a at 2007-7-16 2:05:41 > top of Java-index,Java Essentials,Java Programming...