do while help please

Discussion in 'Programming' started by xamd404, Feb 27, 2011.

  1. #1
    Hello everyone,

    I am having some trouble getting some code to work. What it needs to do is ask for a value input by the user, so e.g. "2", then ask again and again collecting the values and adding them up untill the user selects cancel and after doing so is shown the total addition of all the values they input.

    So far it requests the values untill you hit cancel but when you hit cancel a new window showing the total values is not showing up. Here is the code:

    import javax.swing.JOptionPane;
    public class Testing
    {
    public static void main (String[] args)
    {

    String inputBegin = JOptionPane.showInputDialog("Enter the first value:");
    double firstInput = Double.parseDouble(inputBegin);

    double Number ;
    String numInput = "";
    double Total;
    double newNumber = 0;

    do
    {
    numInput = JOptionPane.showInputDialog("Enter the next value or hit cancel to end:");
    Number = Double.parseDouble(numInput);
    newNumber = newNumber + Number;
    Total = firstInput + newNumber;
    } while (numInput != null);

    JOptionPane.showMessageDialog(null, "£" + Total);

    }
    }
     
    xamd404, Feb 27, 2011 IP
  2. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    I don't use Java, but do you need to specify the message box type: e.g
    JOptionPane.showMessageDialog(null, "£" + Total,JOptionPane.INFORMATION_MESSAGE);
    Or is it possible that you need to convert the variable 'Total', to prevent datatype error. Probably something like:
    JOptionPane.showMessageDialog(null, "£" + Total.toString(),JOptionPane.INFORMATION_MESSAGE);
    I'm really only just taking a stab at this
     
    camjohnson95, Mar 1, 2011 IP
  3. JPF

    JPF Guest

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    JPF, Mar 2, 2011 IP