【问题标题】:Finding a string within a text file在文本文件中查找字符串
【发布时间】:2015-07-14 03:10:03
【问题描述】:

我是一个完整的java新手。如果你能帮助我,那就太好了。所以我需要编写一个程序来进行货币交易。 我的写法如下:

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);
            }
        }

    }
}

一切似乎都在工作,除了:

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

这个程序基本上应该做的是,当你进行交易时,它会记下你想给他们的人的名字和金额,并将其存储在 data.txt 中,当这个人想要收钱时,写下他的他应该收到的姓名和金额,如果该信息与存储在 data.txt 中的信息匹配,程序会告诉他们他们将收到金额,但如果不匹配,则程序会告诉他们请求没有没有找到。

程序在data.txt中存储名称和金额的部分有效;只有找到它的部分没有。 请帮忙。

【问题讨论】:

  • 抛出任何错误?
  • 如果你在解析.txt文件时遇到问题,最好不要编写转账程序!
  • 约瑟夫·e。是的,错误:找不到符号 if(x.contains.String(inputinfo)) ^
  • isnot2bad,它不会处理实际货币,它只是一个假设
  • 您无法使用 PrintWriter 读取文件,要将字符串与 txt 文件的内容进行比较,您应该使用 Scanner 或 FileReader 等将内容变红并进行比较

标签: java string file


【解决方案1】:

正如 Java 文档所说,PrintWriter 用于写入文件。在您的情况下,我建议您使用 BufferedReader 类。您可以使用它的 readLine 方法逐行读取并搜索文本。

大致是这样的……

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

【讨论】:

  • 嘿@Santhosh,感谢您的回复,但它给了我一个错误,说找不到符号行
  • 你需要声明字符串行
【解决方案2】:

还没有时间为您完善代码;但以下方法可行:

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);
            }
        }

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多