Использование сопоставления шаблонов для сортировки из файла, javaJAVA

Программисты JAVA общаются здесь
Anonymous
Использование сопоставления шаблонов для сортировки из файла, java

Сообщение Anonymous »

Итак, я получил свою программу до такой степени, что она должным образом отделяет строки текстового файла и может даже соответствовать шаблону для первой строки текста, но мне также необходимо иметь возможность обнаружить и отделить адресные строки текстового файла и сортировать их в зависимости от их направления или улицы/Бродвей, но я даже не могу определить начальный шаблон для установки адреса. Я использую regex неправильно, и поэтому адресная часть не будет обнаружена должным образом? < /p>

code < /strong> < /p>

package csi311;

// Import some standard Java libraries.
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.ArrayList;
/**
* Hello world example. Shows passing in command line arguments, in this case a filename.
* If the filename is given, read in the file and echo it to stdout.
*/
public class HelloCsi311 {

/**
* Class construtor.
*/
public HelloCsi311() {
}

/**
* @param filename the name of a file to read in
* @throws Exception on anything bad happening
*/
public void run(String filename) throws Exception {
if (filename != null) {
readFile(filename);
}
}

/**
* @param filename the name of a file to read in
* @throws Exception on anything bad happening
*/
private void readFile(String filename) throws Exception {
System.out.println("Dumping file " + filename);
// Open the file and connect it to a buffered reader.
BufferedReader br = new BufferedReader(new FileReader(filename));
ArrayList foundaddr = new ArrayList();
String line = null;
String pattern = "^\\d\\d\\d-[A-Za-z][A-Za-z][A-Za-z]-\\d\\d\\d\\d";
String address[] = new String[4];
address[0] = "\\d{1,3}\\s\\[A-Za-z]{1,20}";
address[1] = "\\d{1,3}\\s\\[A-Za-z]{1,20}\\s\\d{1,3}\\[A-Za-z]{1,20}\\s\\[A-Za-z]{1,20}";
address[2] = "\\d{1,3}\\s\\d{1,3}\\[A-Za-z]{1,20}\\s\\[A-Za-z]{1,20}";
address[3] = "\\d\\d\\s\\[A-Za-z]{1,20}";
Pattern r = Pattern.compile(pattern);
// Get lines from the file one at a time until there are no more.
while ((line = br.readLine()) != null) {
if(line.trim().isEmpty()) {
continue;
}
String sample = line.replaceAll("\\s+,", ",").replaceAll(",+\\s",",");
String[] result = sample.split(",");
String pkgId = result[0].trim().toUpperCase();
String pkgAddr = result[1].trim();

Float f = Float.valueOf(result[2]);
for(String str : result){
// Trying to match for different types
for(String pat : address){
if(str.matches(pat)){
System.out.println(pat);
}
}

if(f < 50 && !pkgId.matches(pattern)) {
Matcher m = r.matcher(str);
if(m.find()) {
foundaddr.add(str);
}
}
}
}

if(foundaddr != null) {
System.out.println(foundaddr.size());
}

// Close the buffer and the underlying file.
br.close();
}

/**
* @param args filename
*/
public static void main(String[] args) {
// Make an instance of the class.
HelloCsi311 theApp = new HelloCsi311();
String filename = null;
// If a command line argument was given, use it as the filename.
if (args.length > 0) {
filename = args[0];
}
try {
// Run the run(), passing in the filename, null if not specified.
theApp.run(filename);
}
catch (Exception e) {
// If anything bad happens, report it.
System.out.println("Something bad happened!");
e.printStackTrace();
}
}
}
< /code>

текстовый файл < /strong> < /p>

123-ABC-4567, 15 W. 15th St., 50.1
456-BGT-9876,22 Broadway,24
QAZ-456-QWER, 100 East 20th Street,50
Q2Z-457-QWER, 200 East 20th Street, 49
6785-FGH-9845 ,45 5th Ave, 12.2,
678-FGH-9846 ,45 5th Ave, 12.2

123-ABC-9999, 46 Foo Bar, 220.0
347-poy-3465, 101 B'way,24


Below is the lines of code that should be able to process the address lines but for some reason it wont match the pattern and the outputs which properly separate the address lines and can be seen in the print statement above the for loop dealing with the addresses but for some reason the address lines arent even being detected as matches and im confused as to why that is.

Line of Code Issue с < /strong> < /p>

for(String str : result){
//System.out.println(str);
// Trying to match for different types
for(String pat : address){
if(str.matches(pat)){
System.out.println(pat);
}
}
< /code>

желаемый вывод < /strong> - редактировать как запрос - < /p>

22 Broadway
45 5th Ave
101 B'way


Подробнее здесь: https://stackoverflow.com/questions/544 ... -file-java

Вернуться в «JAVA»