0
import javax.swing.JOptionPane;


public class SampleLT1 {

    public static void main(String[] args) {
        double dAllowance;
        int days;

        String strDAllowance, strDays;
        do{
            strDAllowance = JOptionPane.showInputDialog(null,"Please enter Daily Allowance $:(>0)",
                "Allowance", JOptionPane.INFORMATION_MESSAGE);
            dAllowance = Double.parseDouble(strDAllowance);
            if (dAllowance<0) System.out.println("Daily Allowance shoould be >0. Please enter again.");
            if (dAllowance==0) {//if enter daily Allowance as 0, exit the program
                System.out.println("Thank you for using Allowance Calculating Application.");
                System.exit(0);
            }
        }while (dAllowance<0);//if daily allowance is smaller than 0, enter again

        do{
            strDays = JOptionPane.showInputDialog(null,"Please enter No. of working days: (>0 and <42)",
                "Allowance", JOptionPane.INFORMATION_MESSAGE);
            days = Integer.parseInt(strDays);
            if(days<0 || days>42) System.out.println("No. of working days should >0 and <42. Please enter again.");
            if (days==0) {//enter 0 to quit the program
                System.out.println("Thank you for using Allowance Calculating Application.");
                System.exit(0);
            }
        }while (days<0 || days>42);//if days <0 or >42, enter days again
        System.out.println("For an internship student who worked "+days +" days with daily allowance $"+dAllowance+", his total allowance is $"+calAllowance(dAllowance,days));
        System.exit(0);
    }

    public static double calAllowance(double dailyAllowance, int noOfDays){
        return dailyAllowance * noOfDays;//calculate and return total allowance
    }
    }

How do I use while loop, for loop to replace this do-while loop?
Can anyone teach me? Need to know for my upcoming test.

4
  • How does a for loop work? What are the components involved? Commented Nov 27, 2013 at 15:23
  • docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html might help. Commented Nov 27, 2013 at 15:26
  • I'd recommend you play about with loops with some simple examples (e.g. printing out numbers). They are not difficult to understand, but the incidental complexity of all the Swing code makes it harder to "play" with your example. Commented Nov 27, 2013 at 15:34
  • Ah thanks alot! I will try to attempt other examples of loop before attempting this again! Commented Nov 27, 2013 at 15:43

2 Answers 2

2

You don't need to enter anything in a for loop. A completely empty for loop for(;;) is the same as while(true). Any while loop can be converted to a for loop:

int i = 0;
while (i < 10) i++;

Is the same as:

int i = 0;
for ( ; i < 10; ) i++;

Because your example is do-while you can't get the identical logic without making a totally empty loop and doing the break yourself. A for/while also makes a conditional check before the loop enters but a do-while only does it after.

You can get something very similar but you must assign the control variable to something that meets the condition:

int days = -1;

for ( ; days < 0 || days > 42; ) {

}

But I don't really know why the instructor wants you to refactor the loop in to something redundant like this. Because you are checking all the possible values inside the loop you don't need the outer condition. You can just break. Otherwise you are making the same check twice. I would personally do the following (even over do-while):

double dAllowance;
String strDAllowance;

for ( ; ; ) {
    strDAllowance = JOptionPane.showInputDialog(/* ~~~ */);

    try {
        dAllowance = Double.parseDouble(strDAllowance);

        if (dAllowance > 0) {
            break;

        } else if (dAllowance == 0) {
            System.exit(0);
        }
    } catch (NumberFormatException e) {}

    /* notify and automatically continues */
}

If you really want to delve in to the deepest of "what is valid loop logic" consider the following:

while (
    ( dAllowance = Double.parseDouble(
        JOptionPane.showInputDialog(
            null,
            "Please enter Daily Allowance $:(>0)",
            "Allowance",
            JOptionPane.INFORMATION_MESSAGE)
    ) ) < 0) {
        System.out.println("Daily Allowance shoould be >0. Please enter again.");
}

if (dAllowance == 0) {
    System.out.println(
        "Thank you for using Allowance Calculating Application.");
    System.exit(0);
}

Note that I only braced the while loop for readability. The braces should not be necessary. That could also of course be turned in to a for loop:

for (;
    ( dAllowance = Double.parseDouble(
        JOptionPane.showInputDialog(
            null,
            "Please enter Daily Allowance $:(>0)",
            "Allowance",
            JOptionPane.INFORMATION_MESSAGE)
    ) ) < 0;
        System.out.println("Daily Allowance shoould be >0. Please enter again.")
); // <- no body necessary

But that's the kind of answer you probably should not put down.

Sign up to request clarification or add additional context in comments.

3 Comments

That for ( ; i < 10 ; ) is useful: I didn't know you could do that. (+1).
@KnockFall No problem. Make sure to mark the question solved if it is. : )
@bluesh34 Indeed, you can more or less put anything you want in a for loop.
0

I've converted the first do-while loop to a for loop and the code is below.

EDIT:

I think the version below is a little better than the previous one:

import javax.swing.JOptionPane;

public class SampleLT1 {

    public static void main(String[] args) {
        double dAllowance = 0;
        int days;

        String strDAllowance, strDays;
        for(int i =-1;i<0;){
            strDAllowance = JOptionPane.showInputDialog(null,"Please enter Daily Allowance $:(>0)",
                "Allowance", JOptionPane.INFORMATION_MESSAGE);
            dAllowance = Double.parseDouble(strDAllowance);
            if (dAllowance<0){ 
                System.out.println("Daily Allowance shoould be >0. Please enter again.");
            }
            if (dAllowance==0) {//if enter daily Allowance as 0, exit the program
                System.out.println("Thank you for using Allowance Calculating Application.");
                System.exit(0);
            }
            if (dAllowance>0) i++;
        }//if daily allowance is smaller than 0, enter again

        do{
            strDays = JOptionPane.showInputDialog(null,"Please enter No. of working days: (>0 and <42)",
                "Allowance", JOptionPane.INFORMATION_MESSAGE);
            days = Integer.parseInt(strDays);
            if(days<0 || days>42) System.out.println("No. of working days should >0 and <42. Please enter again.");
            if (days==0) {//enter 0 to quit the program
                System.out.println("Thank you for using Allowance Calculating Application.");
                System.exit(0);
            }
        }while (days<0 || days>42);//if days <0 or >42, enter days again
        System.out.println("For an internship student who worked "+days +" days with daily allowance $"+dAllowance+", his total allowance is $"+calAllowance(dAllowance,days));
        System.exit(0);
    }

    public static double calAllowance(double dailyAllowance, int noOfDays){
        return dailyAllowance * noOfDays;//calculate and return total allowance
    }
    }

2 Comments

Thanks alot man! Though i am still not sure about loops, i will try to absorb as much as i can!
No problems. I've edited my answer to something I think's better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.