|
import java.io.Console;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class RegexTestHarness {
public static void main(String[] args){
Console console = System.console();
if (console == null) {
System.err.println("No console.");
System.exit(1);
}
while (true) {
Pattern pattern =
Pattern.compile(console.readLine("%nEnter your regex: "));
Matcher matcher =
pattern.matcher(console.readLine("Enter input string to search: "));
boolean found = false;
while (matcher.find()) {
console.format("I found the text" +
" \"%s\" starting at " +
"index %d and ending at index %d.%n",
matcher.group(),
matcher.start(),
matcher.end());
found = true;
}
if(!found){
console.format("No match found.%n");
}
}
}
}
|
|
|
|
|
I don't know where you got that console hassle there - it's a complete mixup.
Your code works when the console is addressed right:
public class RegexTestHarness {
public static void main(String[] args) {
while (true) {
try {
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your regex:");
String strValue = console.readLine();
Pattern pattern = Pattern.compile(strValue);
System.out.println("Enter input string to search:");
String strMatch = console.readLine();
Matcher matcher = pattern.matcher(strMatch);
boolean found = false;
while (matcher.find()) {
System.out.println(String.format("I found the text" + " \"%s\" starting at "
+ "index %d and ending at index %d.",
matcher.group(), matcher.start(), matcher.end()));
found = true;
}
if (!found) {
System.console().format("No match found.");
}
} catch (IOException oException) { oException.printStackTrace();}
}
}
}
|
|
|
|
|
Still a problem Sir, it says: the method console() is undefined for the type System.
|
|
|
|
|
*däng* I missed that one, please change that too:
if (!found) {
System.out.println("No match found.");
}
Do you understand what had to be changed?
|
|
|
|
|
dont worry i changed it to:
System.out.println(String.format("No match found.%n"));
|
|
|
|
|
hallo Mr Torsten, i want us to collaborate on developing a cancer registration form for the
SA government. we gonna use java. the problem is that i dont know how we gonna integrate it into an already existing program because it doesn't meet the standard requirements.
|
|
|
|
|
You need to figure that out.
Please also check out GWT, which is a java based JS-Solution for Web based applications. Could be pretty helpful and is easy to use with java coding.
Have fun.
|
|
|
|