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.

Page 3 of 3 FirstFirst 123
Results 51 to 65 of 65

Thread: Random variant.

  1. #51
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random variant.

    When you store data into an array, you need to change the index's value to point to the element in the array where you want the data stored. Normally the index starts at 0 and increases by 1 for each new item that is stored in the array.
    		anArray[0] = books;
    The above statement stores the data at the first element (index=0). If you want data put into the other elements in the array, the code needs to use a variable as index and increment the index every time data is stored in the array.
    If you don't understand my answer, don't ignore it, ask a question.

  2. #52
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    Can you give me the example?

  3. #53
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random variant.

    theArray[theIndex++] = theValue; // save theValue in theArray and incr the index
    If you don't understand my answer, don't ignore it, ask a question.

  4. The Following User Says Thank You to Norm For This Useful Post:

    Lukas (November 8th, 2013)

  5. #54
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    public void Authors(){
    		scan = new Scanner(System.in);
     
    		System.out.println("Please write the author: ");
    		authors = scan.nextLine();		
    		System.out.println("Please write how much author books you want to write: " + authors);
    		authors03 = scan.nextInt();
    		scan.nextLine();
     
    		for(int i = 0; i < authors03; i++){
    		System.out.println("Please write author books: " + authors);
    		setBooks(scan.next());
    		anArray = new String[1];
    	}
    	}
    	public void result(){
    		anArray[0] = books;
    		System.out.println("You have writed the author: " + authors);
    		String[] setBooks = anArray;
    		System.out.println("You have entered these authors books "+ java.util.Arrays.toString(setBooks)); //Program print me the last book. I want that the program print me all books.
    	}

    Then what's wrong???

    "theArray[theIndex++] = theValue; // save theValue in theArray and incr the index"
    I did: anArray[0] = books;

    My rezult is the last book.

  6. #55
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random variant.

    anArray[0] = books;
    That is the same as doing the following:
    int x;
    x = 3;
    x = 4;
    x = 5;
    and expecting x to remember it once had the values 3 and 4. That is NOT the way computers work. x can only have one value, the last one assigned.

    anArray[0] is always the first element in the array and can only have the last value that is assigned to it
    the code must use a variable instead of the 0 for its index. The variable must be incremented so it points to the next slot.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #56
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    Then how can I use the same "x" on my program?
    anArray[0] = 1;
    anArray[1] = 2;
    anArray[2] = 3;

  8. #57
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random variant.

    use the same "x"
    Can you explain what you mean?

    see posts #51 and #53
    If you don't understand my answer, don't ignore it, ask a question.

  9. #58
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    Then why this is bad?
    anArray[0] = books;

  10. #59
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random variant.

    anArray[0] = books;
    That statement puts the current value of books in the first slot in the array.

    Do you want to save a different value of books in another slot in the array?

    Where do you want to save the data about books that the user enters into the program?
    If there is only one value for books and you want that one value saved as the first element in the array,
    then that is the correct statement to do that.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #60
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    I want that the program print me all books where I inserted.

  12. #61
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random variant.

    Only one book's name can be stored in anArray[0]. If you want more than one name stored in the array, you need to use a variable for the index instead of [0]. When a name is stored in the array at the current value of the index, the code needs to change the value of the index to point to the next slot in the array.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #62
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    public void Authors(){
    		scan = new Scanner(System.in);
     
    		System.out.println("Please write the author: ");
    		authors = scan.nextLine();		
    		System.out.println("Please write how much author books you want to write: " + authors);
    		authors03 = scan.nextInt();
    		scan.nextLine();
     
    		for(int i = 0; i < authors03; i++){
    		System.out.println("Please write author books: " + authors);
    		books = scan.nextLine();
    		anArray = new String[2];
    	}
    	}
    	public void result(){
    		anArray[0] = books;
    		anArray[1] = books;
    		System.out.println("You have writed the author: " + authors);
    		System.out.println("You have entered these authors books "+ (anArray[0]));
    		System.out.println("You have entered these authors books "+ (anArray[1])); //Program print me the last book. I want that the program print me all books.
    	}

    My rezult is:
    Please write the author: 
    Lukas
    Please write how much author books you want to write: Lukas
    2
    Please write author books: Lukas
    Test1
    Please write author books: Lukas
    Test2
    You have writed the author: Lukas
    You have entered these authors books Test2
    You have entered these authors books Test2

    Please change the code that it would show me all my inserted...
    I must insert how many books author has created.
    For example:
    System.out.println("Please write how much author books you want to write: " + authors);
    And I must insert those books.

    I understood that how many I create anArray, and there will the books. No.
    I must insert how many be books, and I insert them...

    I thing you undestand want I want to say. Please change my code and write it here. Thanks...I will learn.

  14. #63
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random variant.

    Can you write a small program for testing how to use arrays? Leave this program for now.

    Put all the code for the test program in the main() method.
    1)Define an array of int with 5 slots
    2)write a loop that assigns the loop control variable to a slot in the array using the loop control variable as the index to the array.
    3)write a second loop that goes through the array and prints the contents of each slot in the array.

    You need to work on how arrays work and how to use them. A small test program will help you.
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    Lukas (November 9th, 2013)

  16. #64
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    I correct my program. No everything okey.

  17. #65
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random variant.

    Glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

Page 3 of 3 FirstFirst 123

Similar Threads

  1. Generate random numbers between 1 and 52 by passing a seed to Random.
    By Shareefuddin in forum Object Oriented Programming
    Replies: 2
    Last Post: April 22nd, 2013, 09:48 AM