fuzzyl0g1c
February 29th, 2008, 06:43 PM
Hello everyone!
I'm having some trouble with regular expressions, and I was wondering if any kind soul could give me some guidance. I'm using the Scanner class to read in a text file, and I'm trying to use regular expressions to get the data I want out.
The text file is like this:
-------------------------------
|D|N|O|P|S|N|O|I|T|A|C|A|V|F|X|
-------------------------------
|B|S|E|R|E|N|I|T|Y|N|Z|E|K|Y|I|
-------------------------------
|Z|B|R|E|A|M|O|A|N|A|R|H|E|C|M|
and so on...
The end result I want is to have a 2-d array of all of the characters like so:
D N O P S N O T A C A V F X
The reason it needs to be like this is I'm going to use brute force to search for a bank of words.
Here is my code so far:
import java.io.*;
import java.util.Scanner;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WordFind {
/**
* @param args
*/
public static void main(String[] args)throws IOException {
String[][] Word = new String[31][31];
try {
Scanner sc = new Scanner(new File("cashiers.txt"));
Pattern pattern = Pattern.compile("[A-Z]");
Matcher matcher = pattern.matcher(sc.next(sc.next()));
System.out.println(matcher.group());
} catch(FileNotFoundException e) {
System.out.println("This file does not exist.");
}
}
}
This is as far as I got because when I run the above I get this:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.next(Scanner.java:1394)
at WordFind.main(WordFind.java:29)
Can anyone tell me what might be wrong? I'm guessing it can't resolve the dashes to string type, but I'm unsure of what to do. Thanks a lot!
:guitar:
I'm having some trouble with regular expressions, and I was wondering if any kind soul could give me some guidance. I'm using the Scanner class to read in a text file, and I'm trying to use regular expressions to get the data I want out.
The text file is like this:
-------------------------------
|D|N|O|P|S|N|O|I|T|A|C|A|V|F|X|
-------------------------------
|B|S|E|R|E|N|I|T|Y|N|Z|E|K|Y|I|
-------------------------------
|Z|B|R|E|A|M|O|A|N|A|R|H|E|C|M|
and so on...
The end result I want is to have a 2-d array of all of the characters like so:
D N O P S N O T A C A V F X
The reason it needs to be like this is I'm going to use brute force to search for a bank of words.
Here is my code so far:
import java.io.*;
import java.util.Scanner;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WordFind {
/**
* @param args
*/
public static void main(String[] args)throws IOException {
String[][] Word = new String[31][31];
try {
Scanner sc = new Scanner(new File("cashiers.txt"));
Pattern pattern = Pattern.compile("[A-Z]");
Matcher matcher = pattern.matcher(sc.next(sc.next()));
System.out.println(matcher.group());
} catch(FileNotFoundException e) {
System.out.println("This file does not exist.");
}
}
}
This is as far as I got because when I run the above I get this:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.next(Scanner.java:1394)
at WordFind.main(WordFind.java:29)
Can anyone tell me what might be wrong? I'm guessing it can't resolve the dashes to string type, but I'm unsure of what to do. Thanks a lot!
:guitar: