我有一个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块内声明String [] aryLines。因此,其范围仅限于try块。因此,您在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");
         }
    }
}


中断循环和写入文件可以有任何不同的逻辑。当前提供了一种有效的解决方案。

09-30 17:47