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

Thread: nextint() Method !

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Angry nextint() Method !

    hi all

    i need help in this exercise :-

    develop main method that initializes a random object with the default constructor and then determines the elapsed time for the nextint() method to generate 123456789

    -------

    how


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: nextint() Method !

    Define help...what do you have so far? If nothing, start here: The Java™ Tutorials

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: nextint() Method !

    Quote Originally Posted by copeg View Post
    Define help...what do you have so far? If nothing, start here: The Java™ Tutorials


    this is my basic idea

     Random r = new Random();
     
     for(int i=1;i<10;i++){
     int Number = r.nextInt(10);
     System.out.println(Number);

    this code generate 9 random numbers , now i need to generate 123456789 using this method

    anyone help me

  4. #4
    Junior Member
    Join Date
    Mar 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: nextint() Method !

    ok i trying in this idea :-


    Random r = new Random();
    for(int i=0;i<9;i++){
         int[] array=new int[9];
     int Number = r.nextInt(10);
     if((Number==1) || (Number==2) || (Number==3)||(Number==4)||(Number==5)||(Number==6)||(Number==7)||(Number==8)||(Number==9))
             Number=array[i];
     System.out.println(array[i]);
        }


    the output is :

    0
    0
    0
    0
    0
    0
    0
    0
    0


    Why !! why it not working

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: nextint() Method !

    When equating two values, the left operand is assigned the value of the right. Inspect your code carefully and you will see that the values within the array are never assigned anything (and thus you print their default value of 0)

  6. The Following User Says Thank You to copeg For This Useful Post:

    M7MD (October 24th, 2010)

  7. #6
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: nextint() Method !


  8. #7
    Junior Member
    Join Date
    Mar 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: nextint() Method !

    ???????????

  9. #8
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: nextint() Method !

    ???????????
    !!!!!!!!!!!

  10. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: nextint() Method !

    Quote Originally Posted by M7MD View Post
     int Number = r.nextInt(10);
     if((Number==1) || (Number==2) || (Number==3)||(Number==4)||(Number==5)||(Number==6)||(Number==7)||(Number==8)||(Number==9))
    That condition is completely unnecessary. The definition of nextInt() is that it will return an integer in the range [0,10). Simply change your random-number generation statement to this and there's no need for an if statement.

    int number = r.nextInt(9) + 1;
    // no need for an if statement, number is guaranteed to be in the range [1,10]
    array[i] = number;
    // ...other code

  11. The Following User Says Thank You to helloworld922 For This Useful Post:

    M7MD (October 27th, 2010)

  12. #10
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: nextint() Method !

    I would do it like this:

    import java.util.Random;
     
    public class Numbers {
     
    	/**
    	 * JavaProgrammingForums.com
    	 */	
    	public static void main(String[] args) {
     
    		Random r = new Random();
     
    		int Number;
    		int Count = 1;
     
    		while(Count != 10){
     
    			Number = r.nextInt(10)+1;
     
    			if(Number == Count){
    				System.out.print(Number + " ");
    				Count++;
    			}						
    		}
    	}	 
    }

    Output:

    1 2 3 4 5 6 7 8 9
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  13. The Following User Says Thank You to JavaPF For This Useful Post:

    M7MD (October 27th, 2010)

  14. #11
    Junior Member
    Join Date
    Mar 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: nextint() Method !

    Quote Originally Posted by JavaPF View Post
    I would do it like this:

    import java.util.Random;
     
    public class Numbers {
     
    	/**
    	 * JavaProgrammingForums.com
    	 */	
    	public static void main(String[] args) {
     
    		Random r = new Random();
     
    		int Number;
    		int Count = 1;
     
    		while(Count != 10){
     
    			Number = r.nextInt(10)+1;
     
    			if(Number == Count){
    				System.out.print(Number + " ");
    				Count++;
    			}						
    		}
    	}	 
    }

    Output:
    Man !

    thanks very much

    100%

    and i'am understand the idea in true way

  15. #12
    Junior Member
    Join Date
    Mar 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: nextint() Method !

    Quote Originally Posted by helloworld922 View Post
    That condition is completely unnecessary. The definition of nextInt() is that it will return an integer in the range [0,10). Simply change your random-number generation statement to this and there's no need for an if statement.

    int number = r.nextInt(9) + 1;
    // no need for an if statement, number is guaranteed to be in the range [1,10]
    array[i] = number;
    // ...other code
    Yes !

    thanks for your useful idea

Similar Threads

  1. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM