Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    passingTime is offline Member
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    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
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Don't forget to tell us about any and all error messages that do occur (hint, hint), for therein lies the key to your problem.

  3. #3
    passingTime is offline Member
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    I don't know if these are error messages but once i hit cancel i get these:

    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:991)
    at java.lang.Double.parseDouble(Double.java:510)
    at Testing.main(Testing.java:18)
    Last edited by passingTime; 02-27-2011 at 07:23 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  5. #5
    passingTime is offline Member
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    Exception in thread "main" java.lang.NullPointerException
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:991)
    at java.lang.Double.parseDouble(Double.java:510)
    at ShoppingBasket.main(ShoppingBasket.java:18)
    Interactive Session Ended


    Is this it?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by passingTime View Post
    Exception in thread "main" java.lang.NullPointerException
    at ....

    Is this it?
    Yeah, that's it. I'll bet the line of code causing it is this one:

    Java Code:
    Number = Double.parseDouble(numInput);

    The key to this is what happens if the user presses cancel? To find out, you could always have your program display what happens like so:

    Java Code:
    do  {
        numInput = JOptionPane.showInputDialog("Enter the next value or hit cancel to end:");
        System.out.println("numInput = " + numInput);

    If you do this, you'll see that numInput is null if the user presses cancel. If that's the case, you'll get a NullPointerException if you try to parse a null with the next line:

    Java Code:
    Number = Double.parseDouble(numInput);

    A solution is to test numInput for null in an if block immediately after trying to get it from the JOptionPane. If it == null, then don't try to parse it, but if it's not null, then go ahead and parse it.

  7. #7
    passingTime is offline Member
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    not sure how to do that but do you mean something like this?:

    do
    {
    numInput = JOptionPane.showInputDialog("Enter the value of the next item (or hit cancel to see the total):");
    if numInput != null
    {
    Number = Double.parseDouble(numInput);
    }

    newNumber = newNumber + Number;
    Total = firstInput + newNumber;
    } while (numInput != null);

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

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Something like that, but you'll want to use valid if block syntax including parenthesis since your code doesn't look like it should compile. You'll also want to include all code that uses the double returned from the parse inside of the if block. Keep at it!

  9. #9
    passingTime is offline Member
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    Thanks for the help btw.

    I did this and got this error, how do i resolve this?:

    do
    {

    numInput = JOptionPane.showInputDialog("Enter the value of the next item (or hit cancel to see the total):");

    if (numInput != null)
    {
    Number = Double.parseDouble(numInput);
    newNumber = newNumber + Number;
    Total = firstInput + newNumber;
    }



    } while (numInput != null);

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




    error:

    Testing.java:31: variable Total might not have been initialized
    JOptionPane.showMessageDialog(null, "" + Total);

    error is pointing to the T meaning it isn't finding the variable?
    Last edited by passingTime; 02-27-2011 at 09:06 PM.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by passingTime View Post
    Java Code:
    error:
    
    Testing.java:31: variable Total might not have been initialized
    			JOptionPane.showMessageDialog(null, "" + Total);
    			                                      
    error is pointing to the T meaning it isn't finding the variable?
    The error means that you're using Total, but that there is a chance you could be using it before it has been given any value. A solution is to give it a logical initial value when you declare it, in this case 0.0 would work nicely.

  11. #11
    passingTime is offline Member
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    I have done that still getting the error. Trying different things with no success yet

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by passingTime View Post
    I have done that still getting the error. Trying different things with no success yet
    This tells us little regarding what you've tried and what's not working. Please show the new code, show the new errors, show which line causes the error. Also, please use code tags when posting code. The link in my signature immediately below will show you how it's done.

  13. #13
    passingTime is offline Member
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    Java Code:
    import javax.swing.JOptionPane;
    public class Testing
    {
    	public static void main (String[] args)
    	{
    
    	String inputBegin = JOptionPane.showInputDialog("Enter the value of the first item in your shopping basket:");
    	double firstInput = Double.parseDouble(inputBegin);
    
    	double Number = 0;
    	String numInput = "";
    	double Total = 0;
    	double newNumber = 0;
    
    	boolean done = false;
    	while (!done)
    		if (numInput == null)
    			{
    				done = true;
    			}
    	
    		else while (numInput != null)
    		{
    			numInput = JOptionPane.showInputDialog("Enter the value of the next item (or hit cancel to see the total):");
    			Number = Double.parseDouble(numInput);
    			newNumber = newNumber + Number;
    		}
    			Total = firstInput + newNumber;
    			JOptionPane.showMessageDialog(null, "" + Total);
    	}
    }

    This is what i'm trying now and still not getting the total displayed when i hit cancel. Getting the same error as before.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Think through each step logically. If the user cancels the JOptionPane and numInput is null, will your code above prevent the parseDouble from being called? Walk through the code step by step and you'll see what I mean. Then when it comes time to re-trying this, think through your logic on paper, the steps needed before trying to commit code to screen. It will work much better, trust me.

  15. #15
    passingTime is offline Member
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    I get what you mean by paper first but still not sure what you mean by the numInput being null. Isn't the loop supposed to stop once it is null?

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Again, this is not programming but thinking logically, pure and simple. Please follow the logic in my comments:

    Java Code:
                    while (numInput != null) {
                        numInput = JOptionPane.showInputDialog("Enter value or hit cancel:");
                        // what is the value of numInput if the user presses cancel?
                        
                        // if it is null, is there anything in this code to stop the next
                        // line from occurring? 
                        number = Double.parseDouble(numInput);
                        newNumber = newNumber + number;
                    }
    Do you see what I'm getting at now? Again, you must walk through your code as if you were the computer, pretend that the user performs various actions, such as hit cancel, and think through what happens. Then it will be clear to you.

  17. #17
    passingTime is offline Member
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    Ok I understand now. Would it have to be something like this?:

    Java Code:
    		numInput = JOptionPane.showInputDialog("Enter the value of the next item (or hit cancel to see the total):");
    		if (numInput != null)
    			{
    			Number = Double.parseDouble(numInput);
    			newNumber = newNumber + Number;
    			}
    This is giving me an infinite loop, hitting cancel or ok just brings up another dialog box.

  18. #18
    passingTime is offline Member
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    The full block of code:

    Java Code:
    import javax.swing.JOptionPane;
    public class Testing
    {
    	public static void main (String[] args)
    	{
    
    	String inputBegin = JOptionPane.showInputDialog("Enter the value of the first item in your shopping basket:");
    	double firstInput = Double.parseDouble(inputBegin);
    
    	double Number = 0;
    	String numInput = "";
    	double Total = 0;
    	double newNumber = 0;
    
    	boolean done = false;
    	while (!done)
    		
    		numInput = JOptionPane.showInputDialog("Enter the value of the next item (or hit cancel to see the total):");
    		if (numInput != null)
    			{
    			Number = Double.parseDouble(numInput);
    			newNumber = newNumber + Number;
    			}
    	
    		else
    		{
    			done = true;
    		}
    			Total = firstInput + newNumber;
    			JOptionPane.showMessageDialog(null, "" + Total);
    	}
    }

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You're making things too complicated and may be over-thinking the wrong things. Again, get the logic on paper, and try to make it as simple as possible. To do this, we often use something called "pseudo-code" where we write down what we want the code to do, but in Engllish. For e.g.,

    Java Code:
            create a total double variable and initialize it to 0.0
            Create a numInput String variable and initialize it to ""
            
            Repeat while numInput is not null.
                Ask user for new numInput amount.
                if numInput isn't null, do
                    convert numInput into a double
                    add this double into total
                end if numInput...
            end repeat while...
            Display the total result

  20. #20
    passingTime is offline Member
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    I've tried this and i'm back to how i was before. It would add each entered value and straight after show it. So I input 2, it shows me 2, i input another 2, it shows me 4, i input 4, it shows me 8, i input 12, it shows me 20. It will keep doing that showing a dialog box requesting input with OK and Cancel then when i hit OK the show dialog box with the total amount and an OK button.


    Here is the code, much simpler but i'm still unsure how to make it only show the total when Cancel is hit:

    Java Code:
    import javax.swing.JOptionPane;
    public class testing
    {
    	public static void main (String[] args)
    	{
    
    	double total = 0.0;
    	String valueInput = "";
    	
    	
    	
    	while (valueInput != null)
    	{
    	
    		valueInput = JOptionPane.showInputDialog("Enter a value:");
    		if (valueInput != null)	
    		{
    			double numInput = Double.parseDouble(valueInput);
    			total = total + numInput;
    		}
    	
    	JOptionPane.showMessageDialog(null, "" + total);
    }
    }
    }

Page 1 of 2 12 LastLast

Posting Permissions

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