我是一个完整的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中有效;只有发现它的部分不会。 请帮忙。
答案 0 :(得分:0)
正如Java doc所说,PrintWriter用于写入文件。在您的情况下,我建议您使用BufferedReader课程。您可以使用其readLine方法逐行读取并搜索文本。
大致如此......
BufferedReader br = new BufferedReader(new FileReader(new File("data.txt")));
while((line = br.readLine()) != null)
{
if (line.contains("your text")
//do
}
答案 1 :(得分:0)
没有时间为您优化代码;但以下情况可行:
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);
}
}
}
}