我需要将变量附加到TextArea
的每一行文本中。 TextArea
已编码,效果很好。我可以使用TextArea
从TextArea.getText();
检索信息
为了打破它,我试图使用BufferedReader
。不幸的是,这不起作用。有没有不同的方式这样做?以下是如何在文本区域中编写信息的示例:
“workerName”
“workerDepartment”
“workerNumber”
BufferedReader inStream= new BufferedReader
(new InputStreamReader(TextArea.getText()));
String workerName = "";
String workerDepartment = "";
int workerNumber = 0;
String line = inStream.readLine();
while (line != null) {
workerName = line;
line = inStream.readLine();
workerDepartment = line;
line = inStream.readLine();
workerNumber = Integer.parseInt(line);
}
inStream.close();
答案 0 :(得分:1)
如果行由任何分隔符分隔(例如换行符,逗号...),则使用String的split方法并放置分隔符
String[] lines = TextArea.getText().split("\n");
//then you can access your array
String workerName = lines[0];
String workerDepartment = lines[1];
// and so on
此外,您需要在获取值之前检查数组大小以防止ArrayOutOfIndexException,例如,如果只有两行,那么您不应该调用行[2],所以请检查:
if ( lines.length < 3 ) {
// input is not complete, show error message
}
else {
// do your splitting and reading values
}