我正在尝试开发一个简单的正则表达式程序,但我不断从eclipse中得到这个错误。
错误在此行中,在非大写url
中CharSequence getURLContent(URL url)抛出IOException {
语法错误,插入“;”完成LocalVariableDeclarationStatement
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
public class VisualizaActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.visualiza);
String expr = "<td><span\\s+class=\"flagicon\"[^>]*>";
CharSequence getURLContent(URL url) throws IOException{
URLConnection conn = url.openConnection();
String enconding = conn.getContentEncoding();
if (enconding == null){
enconding = "ISO-8859-1";
}
BufferedReader br = new BufferredReader(new InputStreamReader(conn.getInputStream(), enconding));
StringBuilder sb = new StringBuilder(16384);
try{
String line;
while ((line = br.readLine()) != null){
sb.append(line);
sb.append('\n');
}
} finally{
br.close();
}
return sb;
}
Pattern patt = Pattern.compile(expr,Pattern.DOTALL | Pattern.UNIX_LINES);
URL url = new URL("http://en.wikipedia.org/wiki/Mexico");
Matcher m = patt.matcher(getURLContent(url));
while (m.find()) {
String stateURL = m.group(1);
String stateName = m.group(2);
System.out.println(stateName + "," + stateURL);
}
}
}
答案 0 :(得分:9)
您无法在方法内创建方法。您试图在getURLContent
onCreate