Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By jim829
  • 1 Post By gimbal2

Thread: Recalling method from within itself

  1. #1
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    107
    Rep Power
    0

    Default Recalling method from within itself

    I am writing a project in which I need to get 2 incorrect answers. Getting the question and answer are both working. But when I try and get this method working, I get a stackoverflow error. I don't understand how I can pass in a random number, then if it doesn't meet criteria, pass in a NEW random number. I know that it isn't java giving me the same random number over and over so obviously I am messing up something in my code.
    Java Code:
    public Picture getWrong1(Picture question, int questionValue){
    		Picture pic = allPics.get(questionValue);
    		System.out.println("ALL PICS SIZE = " + allPics.size());
    		System.out.println("PIC VALUE = " + pic.getValue());
    		System.out.println("QUESTION VALUE = " + question.getValue());
    		
    		if(pic.getValue()==question.getValue()){
    			getWrong1(question, (int)Math.random()*allPics.size());
    		}
    		
    		return pic;
    	}
    Passing in
    Java Code:
    (int)Math.random()*allPics.size()
    which is reading 12 at the moment, I still get PIC VALUE = 1. Is there something I need to be doing if I am calling a method from within itself like this?

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,079
    Rep Power
    6

    Default Re: Recalling method from within itself

    seems to me like what you want is to get a random value between 0 and allPics.size()-1. I'd use the Random class for that purpose:

    Random (Java Platform SE 7 )

    You post to little currently to really see what the truth is, but I am guessing you are generating a random number that will index far beyond the size of the list.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    107
    Rep Power
    0

    Default Re: Recalling method from within itself

    Thank you for the nudge. I read the Random class, and decided to try it out. I switched all my Math.random() to Random.nextInt(). Still not clear on the difference between the two though.
    Shouldn't the following produce the same possibilities of numbers:
    Java Code:
    //This, allPics size = 12
    (int)Math.random()*allPics.size();
    
    // and this
    Random r = new Random();
    r.nextInt(allPics.size);

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,766
    Rep Power
    5

    Default Re: Recalling method from within itself

    Do you realize that the following:
    Java Code:
    //This, allPics size = 12
    (int)Math.random()*allPics.size();
    will always be 0?

    Math.random() gives a value between 0 and 1. Then you cast it to an int which makes it 0. Then you multiply
    by allPics.size(). You need to cast the entire expression after you multiply it.

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

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,079
    Rep Power
    6

    Default Re: Recalling method from within itself

    API documentation is your friend.

    Math (Java Platform SE 7 )

    Quote: "Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0"

    What happens when you cast a value between 0.0 and 1.0 to an int like you are doing? It becomes either 0 or 1. Do you still think this is supposed to produce the same result?

    EDIT: AAAAAAAAAAAH! Ninja'd by Jim. I won't go down without a fight!
    EDIT 2: good thing that you ask for further clarification in stead of just blindly accepting my "nudge" - I feel a bit of shame for not explaining why Math.random() is less ideal. Bad Gimby.
    Last edited by gimbal2; 03-20-2014 at 11:58 AM.
    rru96 likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 1
    Last Post: 12-12-2013, 07:08 PM
  2. Replies: 7
    Last Post: 04-11-2013, 05:31 AM
  3. Replies: 2
    Last Post: 03-23-2012, 04:53 AM
  4. Recalling an object from an external class
    By jack DANIEL's in forum New To Java
    Replies: 2
    Last Post: 10-30-2010, 12:51 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
  •