Results 1 to 3 of 3
  1. #1
    rania.idan is offline Member
    Join Date
    Dec 2010
    Location
    Chicago, IL
    Posts
    10
    Rep Power
    0

    Default How to ask the user how many elements to print from an array?

    Hello,
    I have a really simple question, I'm feeling a little slow right now. The objective is to initialize the first n positions of a given array of 100 elements with n random integers using the Random class, n should be given by the user.

    Basically, I just need help figuring out what to assign to this print statement: System.out.print("How many elements would you like to print? Must be between 0 and 100.");. I tried n[0] = keyboard.nextInt(); but that didnt work.

    This is my code:


    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class PA6 {
    	public static void main(String[] args) {
    
    		int n[] = new int[100];
    
    		Scanner keyboard = new Scanner(System.in);
    
    		Random randomNumbers = new Random();
    
    		System.out.print("How many elements would you like to print? Must be between 0 and 100.");
    		//////// what to put here?? //////////////////
    
    		for (int i = 0; i < n.length; i++) {
    			n[i] = (int) (randomNumbers.nextInt(1000) + 1);
    		}
    		for (int b = 0; b < n.length; b++)
    
    			System.out.print(n[b] + " ");
    		System.out.println();
    
    	}
    }

  2. #2
    f1gh is offline Member
    Join Date
    Nov 2010
    Posts
    46
    Rep Power
    0

    Default

    ///what to post here
    basically you need to declare a int variable
    int num;
    num = keyboard.nextInt();

    but i have questions about your logic:
    Java Code:
    int n[] = new int[100];
    
    		Scanner keyboard = new Scanner(System.in);
    
    		Random randomNumbers = new Random();
    
    		System.out.print("How many elements would you like to print? Must be between 0 and 100.");
    		//////// what to put here?? //////////////////
                    // shouldn't the second expression in for statment be i<num;i++;?
    		for (int i = 0; i < n.length; i++) {
    			n[i] = (int) (randomNumbers.nextInt(1000) + 1);
    		}
    		for (int b = 0; b < n.length; b++)
    
    			System.out.print(n[b] + " ");
    		System.out.println();
    shouldn't the second expression in both for loops be i <num;?
    this is my sample output when i set both for loops middle expression with i<num, instead of array length
    Java Code:
    How many elements would you like to print? Must be between 0 and 100: 20
    481 309 430 173 262 502 462 602 929 972 299 890 902 405 738 18 783 197 526 547

  3. #3
    rania.idan is offline Member
    Join Date
    Dec 2010
    Location
    Chicago, IL
    Posts
    10
    Rep Power
    0

    Default

    That's what I was having trouble with, but you just answered all my questions. You made it sound so simple. Thank you so much! =)

Similar Threads

  1. Replies: 7
    Last Post: 08-30-2010, 03:35 PM
  2. sum of elements in array
    By myst in forum New To Java
    Replies: 7
    Last Post: 07-17-2010, 08:36 AM
  3. Get value of a String into elements of an Array.
    By mainy in forum New To Java
    Replies: 1
    Last Post: 08-01-2009, 09:17 PM
  4. Replies: 8
    Last Post: 08-27-2008, 10:45 PM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 05:33 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
  •