我试图简化一些我作为第一个项目编写的java代码,将其分为几类。我对此非常新,所以我在下面创建了这个,但是在标记点我不断收到错误:
Syntax error on token ";", { expected after this token
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TxtExtr_AnatomyRegex {
private String x;
private Map<String,String> map= new LinkedHashMap<String,String>();
private String [] seAnatomy=null;
private ArrayList<String> Anatomy_arr = new ArrayList<String>();
private ArrayList<List<String>> Anatomy_table2d = new ArrayList<List<String>>();
String el[]=null;
public TxtExtr_AnatomyRegex(String x,Map<String,String> map) {
// TODO Auto-generated constructor stub
x=this.x;
map=this.map;
}
Pattern Anatomy_pattern = Pattern.compile("Anatomy\\*(.*?Hiatal hernia[^\n]*)",Pattern.DOTALL);
Matcher matcherAnatomy_pattern = Anatomy_pattern.matcher(x); /////////////////////////////////ERROR IS HERE
while (matcherAnatomy_pattern.find()) {
seAnatomy=matcherAnatomy_pattern.group(1).split("\\n|\\r");
}
for (int ise=0;ise<seAnatomy.length;ise++){
Anatomy_arr.add(seAnatomy[ise]);
}
for (String ss:Anatomy_arr){
ArrayList<String> tbb = new ArrayList<String>();
el=ss.split("\\t");
for (String e:el){
if(!e.isEmpty()){
tbb.add(e.trim());
}
}
if(!tbb.isEmpty()){
Anatomy_table2d.add(tbb);
for (int ff=0;ff<Anatomy_table2d.size();ff++){
//Symptom
map.put("SumAnatomy"+Anatomy_table2d.get(ff).get(0),Anatomy_table2d.get(ff).get(1));
}
}
}
System.out.println(Anatomy_table2d);
}
}
我很感激我会阅读很多关于新手课程的内容。输入评论....(我有,但谢谢)....但我认为问题是我需要在声明的变量中放置Matcher matcherAnatomy_pattern = Anatomy_pattern.matcher(x);
。我这样做只是为了得到另一个不可见的错误。我在这里错过了什么基本概念?
答案 0 :(得分:1)
您在构造函数之后编写的所有内容都必须在构造函数或单独的方法中进行编码。您可以将anatomy_pattern等用作类的属性。
public class TxtExtr_AnatomyRegex {
private String x;
private Map<String,String> map= new LinkedHashMap<String,String>();
private String [] seAnatomy=null;
private ArrayList<String> Anatomy_arr = new ArrayList<String>();
private ArrayList<List<String>> Anatomy_table2d = new ArrayList<List<String>>();
String el[]=null;
Pattern Anatomy_pattern = null;
Matcher atcherAnatomy_pattern = null;
public TxtExtr_AnatomyRegex(String x,Map<String,String> map) {
// TODO Auto-generated constructor stub
x=this.x;
map=this.map;
Anatomy_pattern = Pattern.compile("Anatomy\\*(.*?Hiatal hernia[^\n]*)",Pattern.DOTALL);
matcherAnatomy_pattern = Anatomy_pattern.matcher(x); /////////////////////////////////ERROR IS HERE
//...put here your for and while stuff
}//end of the constructur
//... some methods ... you maybe also can do something of the loops in methods
} //end of the class
答案 1 :(得分:1)
合理格式化的代码,位于online java compiler IDE。
问题是,像while等的控制结构只能存在于方法中。在这种情况下,您已将其直接添加到类块中。
将构造函数后面的行包装在方法中,然后编译。
作为旁注,我建议你遵循java编码风格指南。
public class TxtExtr_AnatomyRegex {
private String x;
private Map<String,String> map= new LinkedHashMap<String,String>();
private String [] seAnatomy=null;
private ArrayList<String> Anatomy_arr = new ArrayList<String>();
private ArrayList<List<String>> Anatomy_table2d = new ArrayList<List<String>>();
String el[]=null;
public TxtExtr_AnatomyRegex(String x, Map<String,String> map) {
// TODO Auto-generated constructor stub
x=this.x;
map=this.map;
}
public void doSomething() {
Pattern Anatomy_pattern = Pattern.compile("Anatomy\\*(.*?Hiatal hernia[^\n]*)",Pattern.DOTALL);
Matcher matcherAnatomy_pattern = Anatomy_pattern.matcher(x);
// and other code also here
}
}