Results 1 to 15 of 15
  1. #1
    DrKitten-Paws is offline Member
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Unhappy Allow only postive, nonzero integers

    I am writing a program which will ask a user for a positive nonzero integer value and then find the sum of all the integers from 1 up to the number entered. The problem I run into is when I input a decimal. Is there a way to restrict a user from inputing a decimal (aside from explicitly stating DO NOT INPUT DECIMAL!)?
    I am pretty new to Java and I am using jGRASP.

    Java Code:
    import javax.swing.*;
    
    public class SumofNumbers
    {
    	public static void main(String[] args)
    	{
    		int count, sum =0;
    		
    		double input = Double.parseDouble(JOptionPane.showInputDialog("Please enter a positive, nonzero integer."));
    		
    		
    		do
    		{
    		
    			if(input<1)
    			{
    			input = Integer.parseInt(JOptionPane.showInputDialog("Input is invalid, please enter a positive, nonzero integer.\n (i.e. 1, 2, 3, etc...)"));
    			}			
    
    		}
    		
    		while (input<1);
    
    		
    		
    		for (count=1; count<=input; count++)
    		{
    			sum += count;
    		}
    
    		JOptionPane.showMessageDialog(null, "The sum of the total numbers is " + sum+".");
    
    	
    	}
    }
    Thank you so much. I've Googled it but I don't think I'm working my searches right. Sorry for being such a newb but if you can teach me, I will learn!

    I've been pulling my eyebrows out trying to figure this out. I'm sure browless chicks are cute, right?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,955
    Rep Power
    8

    Default Re: Allow only postive, nonzero integers

    You could do a check when the input comes back, and if it's a decimal, show a warning and ask for the input again.

    Or you could modify the input- use a JSpinner, or something fancier like a JTextField that restricts input.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default Re: Allow only postive, nonzero integers

    If you've done exceptions you can use Integer.parseInt() and handle the exception.
    Keep it in a loop until they enter a correct value.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    DrKitten-Paws is offline Member
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default Re: Allow only postive, nonzero integers

    Okay... Could anyone orient me where I could find HOW to do what you guys are telling me to do? Or how I should Google it?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default Re: Allow only postive, nonzero integers

    You already have the code for getting the value from a user.
    Have you done try/catch blocks (ie exception handling)?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    DrKitten-Paws is offline Member
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default Re: Allow only postive, nonzero integers

    No, I have not. I'll try to Google try/catch blocks and exception handling. I'll let you know if I'm successful. Thanks!

  7. #7
    DrKitten-Paws is offline Member
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default Re: Allow only postive, nonzero integers

    I don't want to be the damsel in distress but I am not able to dicipher what I need. Most of the tutorials I'm finding are going way over my head. =/

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default Re: Allow only postive, nonzero integers

    You've already got the do while loop.
    So you should use that.
    Now, no need to do the first JOptionPane before the do/while, as that's the point of do-while...it will do it once at least.
    And get rid of the if...just leave the parseInt(JoptionPane).

    Oh, and since this is supposed to be an integer then I would make the 'input' variable an int. After all, that's what you want to use.

    Now, what's going to happen when the user enters a non-int?
    The parseInt method will throw an exception.

    In order to see what exception is thrown you can either look it up in the API, or run the code with the above changes.
    I suggest both, just so you can see what happens.

    Then we can go through handling them.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    DrKitten-Paws is offline Member
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default Re: Allow only postive, nonzero integers

    I moved the JOptionPane from before do and to right after do and it does not change anything at all.

    I got rid of the if but now even when I input a nonzero positive integer, it states: "Input is invalid, please enter a positive, nonzero integer.\n (i.e. 1, 2, 3, etc...)"
    I input the same nonzero positive integer and THEN it gives the answer.

    If I change everything back to the way it was and just change input to integer so it looks like this:
    Java Code:
    		int input, count, sum =0;
    		
    		do
    		{
    				input = Integer.parseInt(JOptionPane.showInputDialog("Please enter a positive, nonzero integer."));
    It gives the following error when I input 1.5:
    Exception in thread "main" java.lang.NumberFormatException: For input string: "1.5"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at SumofNumbers.main(SumofNumbers.java:9)

    So now I'm back to square one with an even more confused look.
    Last edited by DrKitten-Paws; 03-14-2013 at 04:41 PM.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default Re: Allow only postive, nonzero integers

    Ah, but now you know, when they enter a duff value, you'll get a NumberFormatException.
    This is where try/catch comes into it.
    The basic structure is:
    Java Code:
    try {
       ...code that can throw an exception...
    } catch (<the relevant exception class> ex) {
       // Do something, in your case you can get away with nothing as it's expected the user will
       // possibly give duff values.
    }
    And you're done.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    DrKitten-Paws is offline Member
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default Re: Allow only postive, nonzero integers

    Thank you so much! =)

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,595
    Rep Power
    5

    Default Re: Allow only postive, nonzero integers

    Quote Originally Posted by DrKitten-Paws View Post
    I don't want to be the damsel in distress but I am not able to dicipher what I need. Most of the tutorials I'm finding are going way over my head. =/
    Check out the tutorials at the URL in my signature. They are basic and also cover exception handling.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    DrKitten-Paws is offline Member
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default Re: Allow only postive, nonzero integers

    Okay, so I did it a bit differently than everyone was telling me. Not because I don't appreciate all of your advice, but because I didn't understand how to do it any other way.
    This fixes the decimal problem but I have a new problem now. If I input 1.0 or anything .0, it crashes. I guess because once it has been truncated it is equal to the input, but I don't know what to do with it. Any ideas?

    Java Code:
    import javax.swing.*;
    import java.text.DecimalFormat;
    
    public class SumofNumbers
    {
    	public static void main(String[] args)
    	{
    		int count, sum =0;
    		double checker;
    		
    		double input = Double.parseDouble(JOptionPane.showInputDialog("Please enter a positive, nonzero integer."));
    		checker = (int)(input);
    		
    		
    		do
    		{
    		 
    			if (checker != input)
    			{
    			input = Integer.parseInt(JOptionPane.showInputDialog("Input is invalid, please enter a positive, nonzero integer.\n(i.e. 1, 2, 3, etc...)\nInvalid input includes negative numbers, decimals, and words."));
    			}
    			
    			if(input<1)
    			{
    			input = Integer.parseInt(JOptionPane.showInputDialog("Input is invalid, please enter a positive, nonzero integer.\n (i.e. 1, 2, 3, etc...)\nInvalid input includes negative numbers, decimals, and words."));
    			}			
    
    		}
    		
    		while (input<1);
    
    		
    		
    		for (count=1; count<=input; count++)
    		{
    			sum += count;
    		}
    
    		JOptionPane.showMessageDialog(null, "The sum of the total numbers is " + sum +".");
    
    	System.exit(0);
    	}
    }
    Thanks, you guys!

    And I know I'm probably making this harder than it looks but with the limited knowledge I have, this is the best I could come up with.
    Last edited by DrKitten-Paws; 03-14-2013 at 06:29 PM.

  14. #14
    DrKitten-Paws is offline Member
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default Re: Allow only postive, nonzero integers

    Sorry to keep bothering but I've looked online and I haven't been able to solve this pesky problem. =/

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default Re: Allow only postive, nonzero integers

    OK.
    You could do it by hand I suppose?
    Get the String value (before you parse to a double) and check for a decimal point.
    If there is one then reject it.
    If not then parse to an int.

    Here's the String API.
    You'll probably want the contains() method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. read integers from txt
    By genji in forum New To Java
    Replies: 12
    Last Post: 05-27-2010, 09:33 PM
  2. Sum of Integers - From File
    By Cubba27 in forum New To Java
    Replies: 8
    Last Post: 03-25-2010, 11:45 AM
  3. Sorting 3 Integers Using If Else
    By MSteinman in forum New To Java
    Replies: 12
    Last Post: 02-19-2010, 12:52 PM
  4. Set of Integers
    By rsjava24 in forum New To Java
    Replies: 7
    Last Post: 01-28-2010, 10:29 AM
  5. how to get the Integers out of a String
    By JordashTalon in forum New To Java
    Replies: 10
    Last Post: 01-30-2009, 06:28 PM

Posting Permissions

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