gpt4 book ai didi

java - 如何将输入与存储在数组中的txt文件字符串进行比较

转载 作者:行者123 更新时间:2023-12-01 12:20:21 25 4
gpt4 key购买 nike

我有一个 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 类中存在一些问题:

  1. 您正在 try block 内声明 String[] aryLines。所以它的范围被限制在try block 内。因此,您会在 for(String name : aryLines) 中的 aryLines 处收到错误,因为由于作用域的原因,它找不到它声明的内容。

  2. 您需要给出一个break语句。目前,由于它不存在,newName 的写入次数与 if 条件失败的次数一样多。

  3. 您需要将换行符写为“\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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com