Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2014
    Posts
    11
    Rep Power
    0

    Default So close yet so far away.

    Okay so i am trying to have something checked without it continuing unless a number doesn't equal any previous numbers. Yet when I run my code it will just continue on. I will show you what I have so far but, I don't know how to fix this. Can someone throw an assist to me? I thought I had this figured out but, I apparently still have errors no matter what.


    Java Code:
    for (int size = 0; size < 5; ++size) 
    		{
    			while (noDupes = true) {
    
    				System.out
    						.println("Enter the "
    								+ place[size]
    								+ " Powerball number which should be greater than 0 and less than 60");
    				number = input.nextInt();
    
    				if (number > 0 && number <= 59) {
    					checkNumber(size); //This is the method in question that I keep having problems with. Next Box of Java code shows the actual method
    					pick[size] = number;
    					break;
    				} else {
    					System.out
    							.println("Numbers cannot be less than 1 or greater than 59.");
    				}
    
    			}
    
    		}
    Java Code:
    public static  void checkNumber(int number) {
    		for (int k = 0; k < 6; k++)
    		{
    			if (number == pick[k])
    			{
    			System.out.println("The number you have entered cannot be a duplicate.");
    			}
    			else
    			{
    			break; //This is the part where I am struggling most at I believe. I don't know if this what 
                                     //I need to put in or do I need to put continue or what. 
    			}
    		}
    		
    	}
    For example I input a 1 for the pick[1] and then on pick[2] I try to enter 1 again but, the program forces me to enter a different number instead. May someone please help me understand this better?
    Last edited by MysterioUnknown; 12-04-2014 at 10:25 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: So close yet so far away.

    Perhaps checkNumber() should return a boolean value indicating whether the number it is given is ok, ie is not a duplicate. That way you could do something useful with checkNumber(size) because at the moment you are checking but not doing anything with the result of the check.

    checkNumber() could return the result as well as, or instead of, printing a message for the user. Whatever you decide it should do you should document that with a small comment before the checkNumber() method definition.

    ---

    Then think about the logic of the calling method. Get it clear in your head before you start coding. The general idea of a while loop inside the for loop is OK, but first express the procedure in plan English before you attempt to code it.

    Notice there are two reasons why you might reject a number: either because it is a duplicate, or because it is out of range. checkNumber() could do both checks and return the value true if it passes both.

    But, to repeat myself, get the steps clearly expressed in English before you code.

  3. #3
    Join Date
    Dec 2014
    Posts
    11
    Rep Power
    0

    Default Re: So close yet so far away.

    I'm trying to do this and it isn't working. I feel like I am going to punch my screen through any second now. Something isn't clicking.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: So close yet so far away.

    A couple observations.
    1. Your while loop is not checking noDupes correctly. Don't use = use ==. And for booleans, just use the value since using == evaluates to true or false anyway.
    2. Assuming you use noDupes correctly, where do you change its value?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: So close yet so far away.

    Jim++. while(noDups) is enough.

    But, at the risk of belaboring my point, express what you consider a reasonable way of checking-and-possibly-assigning in plain English. People here will say if they think it's sensible or not. And might offer comments about how a sensible approach might be implemented in code.

  6. #6
    Join Date
    Dec 2014
    Posts
    11
    Rep Power
    0

    Default Re: So close yet so far away.

    Are you saying I need to put another method that pulls a statement??? I don't know. I am getting really infuriated at this last part. I am lost on what you said.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: So close yet so far away.

    What I meant in my first comment was.

    don't use

    Java Code:
    while(noDupes = true) {
    }
    that just assigned true to noDupes and will always be true.

    Just do this
    Java Code:
    while (noDupes == true) {
    }
    or the more preferable

    Java Code:
    while (noDupes) {
    }
    You also need to take some action if the user types in a duplicate. All you do now is print out a message. You might want to return a boolean from your checkNumber() method. It could be true for a valid entry and false for an invalid entry. Then take some action on that.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. JMF mediaPlayer.close
    By Gog in forum New To Java
    Replies: 2
    Last Post: 02-09-2011, 06:22 PM
  2. Replies: 13
    Last Post: 04-09-2010, 11:15 AM
  3. close to zero
    By nokicky in forum New To Java
    Replies: 6
    Last Post: 10-26-2009, 04:30 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
  •