【发布时间】:2016-11-11 02:24:58
【问题描述】:
我的代码的目标是要求用户输入,用户指定这个输入,我的代码是搜索并找出用户输入所在的行。
一旦找到,我就会有代码将该行提取到某个东西(字符串数组?),这样我就可以编辑它的部分内容。
我的问题是我不确定要使用哪些代码/方法/类来执行此操作。
我现在设置了我的代码,以便它读取 .CSV 文件并将其输出到类型列表的字符串数组中。然后我遍历数组并找出指定的用户输入是否位于文件中。
目前,我的代码仅说明是否找到它,但我不确定如何提取该特定行,我希望你们能帮助我。
这是我点击按钮的代码:
try{
String strSearch = searchSerialField.getText();
CSVReader reader = new CSVReader(new FileReader("test.txt"), ',');
List<String[]> myEntries = reader.readAll();
reader.close();
//Write to existing file
//CSVWriter writer = new CSVWriter(new FileWriter("test.txt"), ',',CSVWriter.NO_QUOTE_CHARACTER, "\r\n");
//Iterate through my array to find the row the user input is located on
for (String[] line : myEntries)
{
ArrayList<String> newLine = new ArrayList<String>();
for (String word : line)
{
if (word.contains(strSearch))
{
//Need a method so I can find out what row my item is on
System.out.println("Found - Your item is on row: ...");
//Code to extract row into something (String array?) so I can edit it's contents
//I then need to write the edited row back to it's original row
}else
{
System.out.println("Not found");
}
}
}
}catch (IOException e)
{
System.out.println(e);
}
我已经尽了最大努力,但我现在对如何提取行感到困惑,我该怎么做呢?
感谢您的帮助。
【问题讨论】:
标签: java arrays list csv opencsv