1

I am a complete java novice. 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

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.

The part with the program storing the names and amounts in data.txt works; only the part that finds it doesn't. Please help.

AwkwardCoder
  • 24,893
  • 27
  • 82
  • 152
Rezi
  • 23
  • 2
  • Any errors being thrown? – Josef E. May 04 '15 at 16:33
  • 1
    If you have troubles parsing a .txt file, you should better not write programs that transfer money! – isnot2bad May 04 '15 at 16:41
  • josef e. yes,error: cannot find symbol if(x.contains.String(inputinfo)) ^ – Rezi May 04 '15 at 16:56
  • isnot2bad, its not gonna be dealing with actual money its just an assginemnt – Rezi May 04 '15 at 16:57
  • you cannot read a file using PrintWriter, to compare the string with the content of txt file you should use Scanner or FileReader etc to red the contents and compare – Muhammad May 04 '15 at 17:50
  • PrintWriter is used to write to a file. For searching in a text file check this: http://stackoverflow.com/questions/20418319/find-a-string-or-a-line-in-a-txt-file-java – Henrik May 04 '15 at 18:06

2 Answers2

0

As Java doc says, PrintWriter is used for writing to the file. In your case, I would suggest you to use BufferedReader class. You can use its readLine method to read line by line and search for the text.

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
}
Santhosh
  • 1,771
  • 1
  • 15
  • 25
0

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

    }
}
Satadru
  • 28
  • 4