Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 497
  • Last Modified:

Java BufferedWriter, Printwriter help

Hi,
I would like to have some help with this code. I want to be able to save customer data in a file and then show the result. The code works as long as I only write one customer.. but I want to be able to save several customers before I print the result.

I need the help right away! :)
package test;

import java.io.*;
import javax.swing.*;
import static javax.swing.JOptionPane.*;

public class Main {
  public static void main(String[] arg) throws IOException {
    PrintWriter outputStream = new PrintWriter
                         (new BufferedWriter
                         (new FileWriter("customers.txt", true)));
    while(true) {
      String name = JOptionPane.showInputDialog
                     ("The Customers name: ");
      
         
      
      if (name == null)
        break;
     
      String category = JOptionPane.showInputDialog
                      ("Customer Category (Business or Private): ");
      
      String discount = JOptionPane.showInputDialog
                      ("Discount: ");
      
      outputStream.println(name);
      outputStream.println(category);
      outputStream.println(discount);
      
      showMessageDialog(null, "The result: " + name + " " + category + " " + discount);
    }       
    outputStream.close();
    
  }      
}

Open in new window

0
Asimo
Asked:
Asimo
  • 10
  • 6
1 Solution
 
CEHJCommented:
The code is fine although i'd do
if (name == null || (name = name.trim()).length() < 1) {
				break;
			}

Open in new window

0
 
AsimoAuthor Commented:
But I want to save several customers before I get to the result.. right now the result shows up after I have saved  one customer?

what is the difference with your code?
0
 
for_yanCommented:

DOnt' know but your code did not compile for me - this one I tried - it works

package test;

import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.JOptionPane.*;

public class Main extends Frame {
    public Main() {

        try {
        PrintWriter outputStream = new PrintWriter
                              (new BufferedWriter
                              (new FileWriter("customers.txt", true)));
            String savedCustomers = "";
         while(true) {
           String name = JOptionPane.showInputDialog
                          ("The Customers name: ");



           if (name == null || name.trim().length() == 0)
             break;

           String category = JOptionPane.showInputDialog
                           ("Customer Category (Business or Private): ");

           String discount = JOptionPane.showInputDialog
                           ("Discount: ");

           outputStream.println(name);
           outputStream.println(category);
           outputStream.println(discount);
                                       savedCustomers += " " +name + " ";
         }
          //   showMessage("The result: " + name + " " + category + " " + discount);
           JDialog d = new JDialog(this,  "Saved coutomers: " + savedCustomers);
             JLabel lbl = new JLabel(  "Saved customers: " + savedCustomers);
             d.add(lbl);
             d.setSize(300,300);
             d.show();

         outputStream.close();
        }catch(Exception ex){
            System.out.println("error " + ex.toString());
            ex.printStackTrace();
        }

    }


  public static void main(String[] arg) throws IOException {
                          new Main();

   //     showMessage("The result: " + name + " " + category + " " + discount);
   //   JDialog d = new JDialog(this, true, "The result: " + name + " " + category + " " + discount);

  }


}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
for_yanCommented:
Yes in the code which I posted it will save several, and then show the names of those saved
0
 
for_yanCommented:
It will finish and show result when you enter a customer with empty name
0
 
for_yanCommented:
This is the same thing but shows mssage window rather than
JDialog - that's why it didn't compile originally - there
was no JOptionPane befor showMeassageDialog

Anyway I also moved your code out of main method -
it is not a good practice


package test;

import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.JOptionPane.*;

public class Main extends Frame {
    public Main() {

        try {
        PrintWriter outputStream = new PrintWriter
                              (new BufferedWriter
                              (new FileWriter("customers.txt", true)));
            String savedCustomers = "";
         while(true) {
           String name = JOptionPane.showInputDialog
                          ("The Customers name: ");



           if (name == null || name.trim().length() == 0)
             break;

           String category = JOptionPane.showInputDialog
                           ("Customer Category (Business or Private): ");

           String discount = JOptionPane.showInputDialog
                           ("Discount: ");

           outputStream.println(name);
           outputStream.println(category);
           outputStream.println(discount);
                                       savedCustomers += " " +name + " ";
         }
          //   showMessage("The result: " + name + " " + category + " " + discount);

            JOptionPane.showMessageDialog(null, "Saved coutomers: " + savedCustomers);
              /*
           JDialog d = new JDialog(this,  "Saved coutomers: " + savedCustomers);
             JLabel lbl = new JLabel(  "Saved customers: " + savedCustomers);
             d.add(lbl);
             d.setSize(300,300);
             d.show();
            */
         outputStream.close();
        }catch(Exception ex){
            System.out.println("error " + ex.toString());
            ex.printStackTrace();
        }

    }


  public static void main(String[] arg) throws IOException {
                          new Main();

   //     showMessage("The result: " + name + " " + category + " " + discount);
   //   JDialog d = new JDialog(this, true, "The result: " + name + " " + category + " " + discount);

  }


}

Open in new window

0
 
AsimoAuthor Commented:
for yan> Your code works. But it is a bit to advanced for me since I am still a beginner.. I am supposed to use dialogboxes and then show the result in a dialog box. :-( So even if it works I can not use it.. and even if I could I can´t explan it.
0
 
for_yanCommented:
I made both variants - first with Dialog, which I guedss you are not suppose to use - the second one is with JOptionPane - you can remove all stuff relted too dialog from that varoiant - it is commeneted out and is not needed
0
 
for_yanCommented:
Remove all lines between the /* and */ symbols and also the lines which are commented out by two slashes // in the second variant of code - and you'll not see any trace of dialog
0
 
AsimoAuthor Commented:
Right! Sorry missed that.. but the result box doesn´t show all the information. Just the names.. I have to get everything in the result box. Name, category and discount...
0
 
for_yanCommented:
Well you can add it as it was befire, but if you want to accumulaste for all of them add it to the string savedCustomer whoich is being acuumulated throur the loop. Try to do it, if it does not work, I'll help you in an hour when I get back to my computer
0
 
for_yanCommented:
That is with the rest of information

package test;

import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.JOptionPane.*;

public class Main extends Frame {
    public Main() {

        try {
        PrintWriter outputStream = new PrintWriter
                              (new BufferedWriter
                              (new FileWriter("customers.txt", true)));
            String savedCustomers = "";
         while(true) {
           String name = JOptionPane.showInputDialog
                          ("The Customers name: ");



           if (name == null || name.trim().length() == 0)
             break;

           String category = JOptionPane.showInputDialog
                           ("Customer Category (Business or Private): ");

           String discount = JOptionPane.showInputDialog
                           ("Discount: ");

           outputStream.println(name);
           outputStream.println(category);
           outputStream.println(discount);
                                       savedCustomers += " " +name + " " + category + " " + discount + " ";
         }
          //   showMessage("The result: " + name + " " + category + " " + discount);

            JOptionPane.showMessageDialog(null, "Results: " + savedCustomers);
              /*
           JDialog d = new JDialog(this,  "Saved coutomers: " + savedCustomers);
             JLabel lbl = new JLabel(  "Saved customers: " + savedCustomers);
             d.add(lbl);
             d.setSize(300,300);
             d.show();
            */
         outputStream.close();
        }catch(Exception ex){
            System.out.println("error " + ex.toString());
            ex.printStackTrace();
        }

    }


  public static void main(String[] arg) throws IOException {
                          new Main();

   //     showMessage("The result: " + name + " " + category + " " + discount);
   //   JDialog d = new JDialog(this, true, "The result: " + name + " " + category + " " + discount);

  }


}

Open in new window

0
 
AsimoAuthor Commented:
Thanks! I changed it a little.. :)

I have some questions and I hope you can help me with them. Why is the main method in the buttom? and why is the code this?

"public class Main extends Frame {
    public Main() "
package test;

import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.JOptionPane.*;

public class Main extends Frame {
    
    public Main() {

        try {
        PrintWriter outputStream = new PrintWriter
                              (new BufferedWriter
                              (new FileWriter("customers.txt", true)));
             
        String savedCustomers = "";

                
         while(true) {
           String name = JOptionPane.showInputDialog
                          ("The Customers name: ");


           if (name == null || name.trim().length() == 0)
             break;

           String category = JOptionPane.showInputDialog
                           ("Customer Category (Business or Private): ");

           String discount = JOptionPane.showInputDialog
                           ("Discount: ");

           outputStream.println(name);
           outputStream.println(category);
           outputStream.println(discount);
                                     
           savedCustomers += "Customer: " + name + "\n" + "Category: " + category + "\n" + "Discount: " + discount + " %" + "\n" + "\n";
           
          } 
           JOptionPane.showMessageDialog(null, "Customer Information: " + "\n" + "\n" + savedCustomers);


         outputStream.close();
         
        }catch(Exception ex){
            System.out.println("error " + ex.toString());
            ex.printStackTrace();
        }

    }


  public static void main(String[] arg) throws IOException {
                          new Main();
    
  }

}

Open in new window

0
 
for_yanCommented:
I believ you can remove "extends Frame" - it will still work - I neede it for Dialog
with JOptionPane you don't need it.

It does not matter where the methods are placed in a class - it will always start with method main()

You name the calss Main - even thow Java is case sensitive and upper case letter
allows to distingusih it from method main - it is a bad practice to do so - better name
calsses with what they are doing - say MyWriter  

The first line public class Main ... is a class declaration and opening line

the next line piblic Main() - is the openeing of specuial method - constructor
of the class which always shoul be named as the class but has parenthezes as oposed to class openeing line

 

 
0
 
AsimoAuthor Commented:
I thought I could. :) And I tried to move main etc.. and it worked but it was awesome to get an explanation from you.  

Thanks a lot for your help!
0
 
AsimoAuthor Commented:
Perfect explanation!
0
 
for_yanCommented:
You are welcome
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 10
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now