Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: do while help please

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default do while help please

    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);
     
    	}
    }

  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: do while help please

    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  = 0;
    String numInput = "";
    double Total = 0;
    double newNumber = 0;
    boolean cancelSelected = false;
    do
    {
    numInput = JOptionPane.showInputDialog("Enter the next value or hit cancel to end:");
    if (numInput == null)
    {
    Number = 0;
    newNumber = newNumber;
    Total = firstInput + newNumber;
    cancelSelected = true;
    }
     
    else
    {
    Number = Double.parseDouble(numInput);
    newNumber = newNumber + Number;
    Total = firstInput + newNumber;
    }
    } while (cancelSelected == false);
     
    JOptionPane.showMessageDialog(null, "" + Total);
     
    }
    }

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: do while help please

    Hello passingTime, welcome to the forums.

    javapenguin, are you going to explain what you did here or what the issue was?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Tags for this Thread