Results 1 to 4 of 4
  1. #1
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default Question about sentinal loops with strings

    import java.io.*;

    import javax.swing.JOptionPane;


    public class WritePayrollFile {

    static String year, month, day, name;
    static double weeklyhoursWorked, overtimehoursWorked, bonusPaid;


    public static void main(String[] args) {
    // TODO Auto-generated method stub
    writeData();

    }

    private static void writeData() {
    // TODO Auto-generated method stub
    year = JOptionPane.showInputDialog("Enter Year of today: ");
    month = JOptionPane.showInputDialog("Enter Month of today: ");
    day = JOptionPane.showInputDialog("Enter day of today: ");

    FileWriter payrollfile;
    try {
    payrollfile = new FileWriter("Monthlypayroll.txt");
    BufferedWriter buffer = new BufferedWriter(payrollfile);
    PrintWriter stream = new PrintWriter(buffer);
    stream.println(year+day+month);

    while(!"END".equals(name)) {
    name = JOptionPane.showInputDialog("Enter Name: ");
    weeklyhoursWorked = Double.parseDouble(JOptionPane.showInputDialog("En ter weekly hours worked: "));
    overtimehoursWorked = Double.parseDouble(JOptionPane.showInputDialog("En ter over time hours worked: "));
    bonusPaid = Double.parseDouble(JOptionPane.showInputDialog("En ter any bonus paid: "));

    }
    double normalhours = weeklyhoursWorked * 4;
    stream.println(name+","+normalhours+","+overtimeho ursWorked+","+bonusPaid);
    stream.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    Looking at the while loop: I want the user to enter the name, weekly hours worked, and over time hours worked and bonuspaid. Then the loop starts again at asking the name. at that point the user types "END" to exit out of the loop.

    When i run the above set of codes, the loop goes through twice and then writes the txt file. How can I make the loop end when the user types END?

    Thank you

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    The check is not performed until after the rest of the code is executed inside the loop. Even if user enters "END" it will still ask the other question. So you only want those other questions to asked if name is not "END".

  3. #3
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    The way you have written your code even if the user entered "END" it would have to run the whole loop anyways to do the test on the next loop start. Instead you could use a while(true) and brake the loop from inside. Second you need to write the file for each loop you do so you should put the stream.println() inside the loop.
    Java Code:
    payrollfile = new FileWriter("Monthlypayroll.txt");
                BufferedWriter buffer = new BufferedWriter(payrollfile);
                PrintWriter stream = new PrintWriter(buffer);
                stream.println(year + day + month);
    
                while (true) {
                    name = JOptionPane.showInputDialog("Enter Name: ");
                    if (name.equalsIgnoreCase("END")){
                        break;
                    }
                    weeklyhoursWorked = Double.parseDouble(JOptionPane.showInputDialog("Enter weekly hours worked: "));
                    overtimehoursWorked = Double.parseDouble(JOptionPane.showInputDialog("Enter over time hours worked: "));
                    bonusPaid = Double.parseDouble(JOptionPane.showInputDialog("Enter any bonus paid: "));
                    double normalhours = weeklyhoursWorked * 4;
                    stream.println(name + "," + normalhours + "," + overtimehoursWorked + "," + bonusPaid);
                }
                stream.close();
    That said you shouldn't use all these JOptionPanes. If the user hits cancel the program crashes. You should do something to handle that. Even better you could create a simple JFrame, throw in a couple of buttons and textfields and it would be much clearer for the user. And if you are going to always enter today's date at the beginning you should conciser using a date object to get the current date automatically, except you want to enter a different date.

  4. #4
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default

    Thank you both sooo much for your help. Really appreciate it :D

Similar Threads

  1. Simple question involving Strings and JOptionPane
    By Napkins in forum New To Java
    Replies: 6
    Last Post: 02-02-2011, 02:30 AM
  2. Replies: 11
    Last Post: 01-18-2011, 03:57 AM
  3. Question about Strings and Color type
    By Psyclone in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2010, 05:28 PM
  4. Question about loops
    By SwEeTAcTioN in forum New To Java
    Replies: 1
    Last Post: 10-23-2009, 07:15 AM
  5. Question about loops
    By BHCluster in forum New To Java
    Replies: 4
    Last Post: 04-16-2008, 05:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •