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 4 of 4

Thread: Integer and if statement

  1. #1
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Integer and if statement

    I am trying to determine if the user did not input a correct value without throwing an exception.

    I have a String variable, done, which stores the user input and then if it is not storing "done" then it coverts the String into Integer. I got that part.

    The question is, how do I determine that what is stored in done is not equal to an integer?

    do
    {
    	if(done != null)
    		if(Integer.parseInt(done).equals(Integer)
    			list.add(Integer.parseInt(done));
    	done = scan.nextLine();
    }
     
    while(!done.equalsIgnoreCase("done"));

    I know thats wrong but it gives you the idea of what i want. anyway to do this?

    BTW, list is an ArrayList<Integer>


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Integer and if statement

    You can't call a method on a primitive, and the compiler should be complaining to you about this. You would do your parsing in a try/catch block and catch for a NumberFormatException. Search on this as there are a lot of examples to be found.

    A better way is to use Scanner's ability to know if the next token is an int or not.

  3. #3
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Integer and if statement

    This is what I changed it to and it worked:

    do
    {
    	if(done != null)
    		list.add(Integer.parseInt(done));
    	if(scan.hasNextInt() || scan.hasNext("done"))
    		done = scan.nextLine();
    	else
    		scan.nextLine();
    }
     
    while(done == null || !done.equalsIgnoreCase("done"));

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Integer and if statement

    Myself, I'd prefer doing something like (in pseudocode):

    do
       prompt for an int
       if no int held by Scanner (! hasNextInt)
         done gets the Scanner's next line
       else
         list get's the Scanner's next int
         call nextLine() on Scanner to swallow end of line token
    while done doesn't hold "done"

Similar Threads

  1. Counting # of 7's in an integer
    By greystreet34 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 25th, 2012, 12:41 PM
  2. Use of Integer.parseInt
    By tarkal in forum Java SE APIs
    Replies: 3
    Last Post: September 8th, 2011, 03:37 AM
  3. Help with ArrayList<Integer>
    By shanklove in forum Collections and Generics
    Replies: 2
    Last Post: September 6th, 2010, 09:15 AM
  4. Using Integer.parseInt
    By Fraz in forum Object Oriented Programming
    Replies: 1
    Last Post: April 5th, 2010, 07:50 AM
  5. int and Integer
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: December 31st, 2009, 03:20 AM