简体   繁体   English

在文本文件中查找字符串

[英]Finding a string within a text file

I am a complete java novice. 我是一个完整的Java新手。 If you could help me out that would be great. 如果您能帮助我,那将很棒。 So I need to write a program, that makes money transaction. 所以我需要写一个程序,赚钱交易。 The way i wrote it is as follows: 我写的方式如下:

class program
{

    public static void main (String[] param)throws IOException
    {
        intro();
        System.exit(0);
    }
    public static void intro()throws IOException
    {
        PrintWriter x =new PrintWriter(new FileWriter("data.txt"));
        while (true)
        {
            Object[] possibleValues = { "Transfer", "Receive", "Cancel"};
            Object selectedValue = JOptionPane.showInputDialog(null,"Choose one", "Input",JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);

            String name;
            String surname;
            String amount;
            String info;

            if(selectedValue.equals(possibleValues[0]))
            {
                name = JOptionPane.showInputDialog(null, "recipients name");
                surname = JOptionPane.showInputDialog(null, "recipients surname");
                amount = JOptionPane.showInputDialog(null, "amount");
                info = name+surname+amount;
                x.println(info);



            }

            else if(selectedValue.equals(possibleValues[1]))
            {
                String inputname;
                String inputsurname;
                String inputamount;
                inputname = JOptionPane.showInputDialog(null, "your name");
                inputsurname = JOptionPane.showInputDialog(null, "your surname");
                inputamount = JOptionPane.showInputDialog(null, "amount");
                String inputinfo = inputname + inputsurname + inputamount;

                if(x.contains.String(inputinfo))
                {
                    JOptionPane.showMessageDialog(null,"you will recieve "+inputamount+"$");
                }
                else
                {
                    JOptionPane.showMessageDialog(null,"request not found");
                }

            }
            else if(selectedValue.equals(possibleValues[2]))
            {
                x.close();
                System.exit(0);
            }
        }

    }
}

and everything seems to be working except for the: 除了:

If (x.contains.String(inputinfo)) line If (x.contains.String(inputinfo))

basically what this program should do is that when you're making transaction it takes down the persons firstname surname and amount you want to give them and stores it in data.txt and when the person wants to collect the money writes down his name and surname and amount he should be receiving and if that information matches to the one stored in the data.txt the program tells them that they will receive the amount but if it doesn't match then the program will tell them that request wasn't found. 基本上,该程序应该做的是,在进行交易时,它会将您要提供给他们的人的姓氏和金额记下来,并将其存储在data.txt中,而当该人想要收款时,请写下其姓名和姓氏和他应该收到的金额,如果该信息与data.txt中存储的信息匹配,则程序告诉他们他们将收到该金额,但如果不匹配,则程序将告诉他们未找到请求。

The part with the program storing the names and amounts in data.txt works; 程序中将名称和金额存储在data.txt中的部分起作用; only the part that finds it doesn't. 只有找到它的部分没有。 Please help. 请帮忙。

As Java doc says, PrintWriter is used for writing to the file. 正如Java文档所说, PrintWriter用于写入文件。 In your case, I would suggest you to use BufferedReader class. 对于您的情况,我建议您使用BufferedReader类。 You can use its readLine method to read line by line and search for the text. 您可以使用其readLine方法逐行读取并搜索文本。

It roughly goes like this... 大概是这样的...

BufferedReader br = new BufferedReader(new FileReader(new File("data.txt")));
while((line = br.readLine()) != null)
{
     if (line.contains("your text")
         //do
}

Haven't had the time to refine the code for you; 没有时间为您优化代码; but the following would work: 但以下方法会起作用:

package Rezi30035097;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.JOptionPane;


class Program01 {

    protected static BufferedReader br;
    protected static PrintWriter x;

    public static void main(String[] param) throws IOException {
        intro();
        System.exit(0);
    }

    protected static boolean matchString(String s) throws FileNotFoundException, IOException {
        br = new BufferedReader(new FileReader(new File("data.txt")));
        String line;
        while ((line = br.readLine()) != null) {
            if (line.contains(s)) {

                br.close();
                br = null;
                return true;
            }
        }
        return false;
    }

    public static void intro() throws IOException {
        x = new PrintWriter(new FileWriter("data.txt"));

        while (true) {
            Object[] possibleValues = {"Transfer", "Receive", "Cancel"};
            Object selectedValue = JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);

            String name;
            String surname;
            String amount;
            String info;

            if (selectedValue.equals(possibleValues[0])) {
                name = JOptionPane.showInputDialog(null, "recipients name");
                surname = JOptionPane.showInputDialog(null, "recipients surname");
                amount = JOptionPane.showInputDialog(null, "amount");
                info = name + surname + amount;

                x.write(info);
                x.flush();

            } else if (selectedValue.equals(possibleValues[1])) {
                String inputname;
                String inputsurname;
                String inputamount;
                inputname = JOptionPane.showInputDialog(null, "your name");
                inputsurname = JOptionPane.showInputDialog(null, "your surname");
                inputamount = JOptionPane.showInputDialog(null, "amount");
                String inputinfo = inputname + inputsurname + inputamount;

                if (matchString(inputinfo)) {
                    JOptionPane.showMessageDialog(null, "You will receive " + inputamount + "$");
                } else {
                    JOptionPane.showMessageDialog(null, "Request not found");
                }
                inputinfo = null;

            } else if (selectedValue.equals(possibleValues[2])) {
                x.close();
                System.exit(0);
            }
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM