我有一个txt文件,我希望能够将名称的输入与文件中已有的名称进行比较。我收到了一条消息“无法找到符号”,因为我已经存储了txt文件数据。我不确定我是否在将数据存储到数组中时出错或者我需要做什么才能获得它可以从我的for循环中访问数组。
我在aryLines in for(String name:aryLines)
中得到错误package binarysearch;
import java.io.*;
import java.util.Scanner;
/**
*
* @author Timothy
*/
public class BinarySearch
{
public static void main(String[] args) throws IOException
{
String file_name = "C:/Users/Timothy/Documents/test.txt";
try
{
ReadFile file = new ReadFile(file_name);
String[] aryLines = file.OpenFile();
/*
for(int index = 0; index < aryLines.length; index++)
{
System.out.println(aryLines[index]);
}
*/
}
catch(IOException error)
{
System.out.println(error.getMessage());
}
String newName;
Scanner in = new Scanner(System.in);
System.out.println("Enter a name to compare to list: ");
newName = in.nextLine();
for(String name : aryLines)
{
if(name.equalsIgnoreCase(newName))
{
System.out.println("This name has already been added");
}
else
{
WriteFile data = new WriteFile(file_name, true);
data.writeToFile("/n" + newName);
}
}
}
}
WriteFile类
package binarysearch;
import java.io.*;
import java.util.Scanner;
/**
*
* @author Timothy
*/
public class WriteFile
{
private String path;
private boolean append_to_file = false;
public WriteFile(String file_path)
{
path = file_path;
}
public WriteFile(String file_path, boolean append_value)
{
path = file_path;
append_to_file = append_value;
}
public void writeToFile(String textLine) throws IOException
{
FileWriter write = new FileWriter(path, append_to_file);
PrintWriter print_line = new PrintWriter(write);
print_line.printf("%s" + "%n", textLine);
print_line.close();
}
}
ReadFile Class
package binarysearch;
import java.io.*;
/**
*
* @author Timothy
*/
public class ReadFile
{
private String path;
public ReadFile(String file_path)
{
path = file_path;
}
public String[] OpenFile() throws IOException
{
FileReader read = new FileReader(path);
BufferedReader textReader = new BufferedReader(read);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
for(int index = 0; index < numberOfLines; index++)
{
textData[index] = textReader.readLine();
}
textReader.close();
return textData;
}
int readLines() throws IOException
{
FileReader file_to_read = new FileReader(path);
BufferedReader buffer = new BufferedReader(file_to_read);
String aLine;
int numberOfLines = 0;
while((aLine = buffer.readLine()) != null)
{
numberOfLines++;
}
buffer.close();
return numberOfLines;
}
}
答案 0 :(得分:1)
BinarySearch 中有少数问题:
您在 try 块中声明 String [] aryLines 。所以它的范围仅限于try块。因此,您在 aryLines in for(String name:aryLines)中收到错误,因为由于范围而无法找到它。
您需要提供中断语句。目前由于缺少,因此当if条件失败时,newName被写入多次。
您需要将新行字符编写为&#39; \ n&#39; 而不是&#39; / n&#39;
我在BinarySearch类中进行了必要的更改:
package binarysearch;
import java.io.*;
import java.util.Scanner;
/**
*
* @author Timothy
*/
public class BinarySearch {
private static boolean write=false;
public static void main(String[] args) throws IOException {
String file_name = "C:\\Users\\Timothy\\Documents\\test.txt";
String[] aryLines = {};
try {
ReadFile file = new ReadFile(file_name);
aryLines = file.OpenFile();
} catch (IOException error) {
System.out.println(error.getMessage());
}
String newName;
Scanner in = new Scanner(System.in);
System.out.println("Enter a name to compare to list: ");
newName = in.nextLine();
for (String name : aryLines) {
if (name.equalsIgnoreCase(newName)) {
System.out.println("This name has already been added");
write = false;
break;
} else {
write = true;
}
}
if (write == true) {
WriteFile data = new WriteFile(file_name, true);
data.writeToFile("\n" + newName);
System.out.println("New name added successfully");
}
}
}
可以有任何不同的逻辑来打破循环并写入文件。目前提供了一种有效的解决方案。
答案 1 :(得分:0)
在这里你需要打破循环,如果找到。否则它将检查其他元素。
for(String name : aryLines){
if(name.equalsIgnoreCase(newName)){
System.out.println("This name has already been added");
break; // word is found need to stop iteration further.
}else{
WriteFile data = new WriteFile(file_name, true);
data.writeToFile("/n" + newName);
}
}