作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 txt 文件,我希望能够将输入的名称与文件中已有的名称进行比较。我收到一条消息“无法找到我存储 txt 文件数据的数组的符号”。我不确定我在将数据存储在数组中是否做错了什么,或者我需要做什么才能获得它能够从我的 for 循环访问数组。
我在 for(String name : aryLines) 中的 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;
}
}
最佳答案
BinarySearch 类中存在一些问题:
您正在 try block 内声明 String[] aryLines。所以它的范围被限制在try block 内。因此,您会在 for(String name : aryLines) 中的 aryLines 处收到错误,因为由于作用域的原因,它找不到它声明的内容。
您需要给出一个break语句。目前,由于它不存在,newName 的写入次数与 if 条件失败的次数一样多。
您需要将换行符写为“\n”,而不是“/n”。
我已在 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");
}
}
}
可以有任何不同的逻辑来打破循环并写入文件。目前提供了可行的解决方案。
关于java - 如何将输入与存储在数组中的txt文件字符串进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26708303/
我是一名优秀的程序员,十分优秀!