我在使用以下方法时遇到困难。我无法弄清楚我的问题是否存在,但我已将其缩小到不从文件中填充数组列表。非常感谢任何帮助。
private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {
//create arraylists
ArrayList<String> model = new ArrayList<String>();
ArrayList<String> length = new ArrayList<String>();
ArrayList<String> width = new ArrayList<String>();
ArrayList<String> radius = new ArrayList<String>();
ArrayList<String> depth = new ArrayList<String>();
ArrayList<String> volume = new ArrayList<String>();
ArrayList<String> shape = new ArrayList<String>();
//fill arraylists from file
try {
String outputline = "";
BufferedReader fin = new BufferedReader(new FileReader("stock.dat"));
while((outputline = fin.readLine()) != null) {
// for(int i = 0; i < outputline.length(); i++) {
int i = 0;
//model
boolean flag = false;
String pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
pass.concat(Character.toString(outputline.charAt(i)));
else
flag = true;
i++;
}
model.add(pass);
//length
flag = false;
pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
pass.concat(Character.toString(outputline.charAt(i)));
else
flag = true;
}
length.add(pass);
//width
flag = false;
pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
pass.concat(Character.toString(outputline.charAt(i)));
else
flag = true;
}
width.add(pass);
//radius
flag = false;
pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
pass.concat(Character.toString(outputline.charAt(i)));
else
flag = true;
}
radius.add(pass);
//depth
flag = false;
pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
pass.concat(Character.toString(outputline.charAt(i)));
else
flag = true;
}
depth.add(pass);
//volume
flag = false;
pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
pass.concat(Character.toString(outputline.charAt(i)));
else
flag = true;
}
volume.add(pass);
//shape
pass = "";
for(int j = i; j < outputline.length(); j++)
pass.concat(Character.toString(outputline.charAt(i)));
shape.add(pass);
}
fin.close();
}
catch(IOException e) {
System.err.print("Unable to read from file");
System.exit(-1);
}
int at = -1;
for(int i = 0; i < model.size(); i++) {
if(model.get(i).equals(searchIn.getText())) {
at = i;
i = model.size();
}
}
Component frame = null;
if(at != -1) {
searchDepthOut.setText(depth.get(at));
searchLengthOut.setText(length.get(at));
searchRadiusOut.setText(radius.get(at));
searchVolumeOut.setText(volume.get(at));
searchWidthOut.setText(width.get(at));
}
else
JOptionPane.showMessageDialog(null, "Your search did not return any results", "ERORR", JOptionPane.ERROR_MESSAGE);
}
答案 0 :(得分:4)
用逗号分隔readline并完成它。我还要为模型,长度,宽度等创建一个对象......然后有一个该对象的arraylist。
while((outputline = fin.readLine()) != null) {
String[] tokens = outputline.split(",");
if(tokens.length == 7){
SObj o = new SObj; //Some Object
o.model = tokens[0];
o.length = tokens[1];
//and so on
oList.add(o);
}
}
答案 1 :(得分:3)
除了人们列出的所有其他问题......
String pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
pass.concat(Character.toString(outputline.charAt(i)));
pass是一个String。字符串是不可变的。你想要
pass = pass.concat(.....)
答案 2 :(得分:2)
while(flag = false)
- 它总是评估为false
。试试while (!flag)
答案 3 :(得分:0)
我建议你重新构建代码。问题不仅在于存在某种解析器错误,而且还很难分辨出发生了什么 - 代码显然是在假设输入行的结构,但是你必须仔细阅读并追踪在你的脑海里重构它的方法。
像
这样的东西/** Expect a line of the form model, length, ...,
return a list of ...
*/
private String[] parse (String inputLine)
{
//check input line charachteristics-not null, length, ...
String out= inputLine.split(",");
if (out.length()!= ...
//whatever sanity checking...
}
private List<String[]> extract(BufferedReader fin)
{
while((outputline = fin.readLine()) != null)
{
//do something with parse(outputline);
}
}
有用的事情是将文件读取和行解析分开,这样你就可以看到什么是破坏的,而不是一切都在破坏,很可能是关于代码中隐藏的行结构的假设。它需要4个逗号分隔的整数吗? 5?如果他们用空格填充怎么样?以空行为前缀?