import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class payrate 
{


    public static void main(String[] args)
    {   




            String str;
            String name;
            double horasT; // horas trabajadas
            double rate;  // rate por hora 
            double salarioB; //  el salario bruto



            name = JOptionPane.showInputDialog ("Entre le nombre del empleado: ");

            do
            {
                str = JOptionPane.showInputDialog("Entre las horas trabajadas: ");
                horasT = Double.parseDouble(str);


                if(horasT <=0)
                {
                    JOptionPane.showMessageDialog(null, "No entreo suficiente datos, porfavor trate denuevo.");
                }// if horasT <=0
            }while(horasT <= 0);// do

            do
            {
                str = JOptionPane.showInputDialog("Entre el rate por hora: ");
                rate = Double.parseDouble(str);

                if(rate<=0)
                {
                    JOptionPane.showMessageDialog(null, "No entreo suficiente datos, porfavor trate denuevo.");
                }// 
            }while(rate <=0);


                    salarioB =getSalarioB(horasT, rate);
            mostrarLosResultados(name,horasT, rate, salarioB);



    }
        //metodo main       



        /**
         * 
         * 
         * @param horasT
         * @param rate
         * @return
         */

        private static double getSalarioB(double horasT, double rate) 
        {

            double salarioBruto;

            salarioBruto = horasT*rate;
            return salarioBruto;

        }// metodo getSalarioB

        /**
         * 
         * 
         * @param name
         * @param horasT
         * @param rate
         * @param salarioB
         */
        private static void mostrarLosResultados(String name, double horasT, double rate, double salarioB) 
        {
            DecimalFormat formatter = new DecimalFormat("#0.00");
            JOptionPane.showMessageDialog(null, "Nombre del empleado: " + name + "\n Las horas trabajadas son: " + horasT + 
                    "\n Su pago por hora es: $" + rate + "\n" + "Su salario bruto es: $" + formatter.format(salarioB ));



            JOptionPane.showInputDialog(null, "Empezar de nuevo? " + "\nEntre [1] para volver a empezar o [0] para salir" );

        }// metodo mostrarLosResultados



    }//payrate

Dani AI

Generated

Quick answer: wrap the input/computation/display steps in a repeat loop and actually use the user's reply to decide whether to run again. and correctly pointed to using a loop/do-while; the specific problem in 's code is that the final showInputDialog("Empezar de nuevo?") is never read or used to control repetition. Also handle Cancel (null) and invalid numbers so the app doesn't crash. (For clarity: horasT = hours worked, rate = hourly rate, salarioB = gross salary.)

A simple, robust pattern is to prompt, validate inside a helper, compute and show results, then ask a yes/no confirm dialog and loop while the user picks Yes:

public static void main(String[] args) {
    int choice;
    do {
        String name = JOptionPane.showInputDialog(null, "Enter employee name:");
        if (name == null) break; // user cancelled

        Double hours = promptForPositiveDouble("Enter hours worked:");
        if (hours == null) break;

        Double rate = promptForPositiveDouble("Enter hourly rate:");
        if (rate == null) break;

        double gross = hours * rate;
        DecimalFormat fmt = new DecimalFormat("#0.00");
        JOptionPane.showMessageDialog(null,
            "Name: " + name + "\nHours: " + hours + "\nRate: $" + rate + "\nGross: $" + fmt.format(gross));

        choice = JOptionPane.showConfirmDialog(null, "Start again?", "Repeat", JOptionPane.YES_NO_OPTION);
    } while (choice == JOptionPane.YES_OPTION);
}

// return null if user cancels; otherwise a positive double
private static Double promptForPositiveDouble(String message) {
    while (true) {
        String s = JOptionPane.showInputDialog(null, message);
        if (s == null) return null;
        try {
            double v = Double.parseDouble(s);
            if (v > 0) return v;
            JOptionPane.showMessageDialog(null, "Please enter a number greater than zero.");
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Invalid number. Try again.");
        }
    }
}

Notes: use showConfirmDialog (avoids parsing "1"/"0"), validate each input, and always check for null from showInputDialog. Splitting input/validation into a helper keeps main readable and makes repeating straightforward.

Recommended Answers

All 5 Replies

OH MY GOD!! Spanish!! I have no idea what this piece of code do. Can you explain what it is you want to do? And maybe translate it to english so I can understand your code?

I'm not really up on java as I program in C usually. If this was c, I would stick the program in a loop, at the end of the loop you ask the question to the user, if they say "no" you just break from the lo op. If they answer "yes" you do nothing and allow your code to begin again

Example pseudocode:

Int x = 0
While(x)
{
Your code goes here
Ask user question if the wants to continue
If "no" , x=0. 
}

This is very rough idea to get you started. Any java pros please feel free to convert this to java and tidy up.

: he has 2 do while loop. And I have no idea what they do. I am not sure if he has already accomplish what he has done or he has other problems. We just have to wait for him to reply to clear the air.

@Wen cai...Thanks.

You use a loop just like in C, except that instead of just breaking out of the loop you use a tidy do/while loop, eg

do {
   // stuff
   // get answer for y or n to continue
}  while (answer.equals("y"));
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.