By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,711 Members | 2,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,711 IT Pros & Developers. It's quick & easy.

How do I create an array using user input?

Misaio
P: 16
I'm trying to create a program that prompts the user to put in several different numbers. I'd like to put those numbers into an array for easy use. To save me time on coding, I want to loop the request for user input. Would I use a for loop and use the subscript in the array to make changes as needed? Please help.
Mar 5 '10 #1

✓ answered by anurag275125

Scanner class allows you to take input from the keyboard.
You can get a little bit information about Scanner class by just typing java.util.Scanner at the command prompt.

for further information visit http://java.sun.com/javase/7/docs/ap...l/Scanner.html

Share this Question
Share on Google+
8 Replies


anurag275125
P: 79
You should use Scanner class to create an array from user input like this---

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class array
  3. {
  4.     public static void main(String args[])
  5.     {
  6.         int[] a=new int[6];
  7.         Scanner sc=new Scanner(System.in);
  8.         System.out.println("Please enter elements...");
  9.         for(int j=0;j<6;j++)
  10.             a[j]=sc.nextInt();
  11.         System.out.println("Array elements are : ");
  12.         for (int i=0;i<a.length;i++)
  13.             System.out.println(a[i]);
  14.     }
  15. }
Mar 6 '10 #2

Misaio
P: 16
Actually, I don't think I'm that far in my Java yet. But what I did find was that I could loop the input and just parse it into a secondary array like this:

Expand|Select|Wrap|Line Numbers
  1.     for (x = 0; x < PLACE_HOLDER; ++x)
  2.         {
  3.             stringArray[x] = JOptionPane.showInputDialog(null, "Please input the price of the item you ordered.");
  4.             doubleArray[x] = Double.parseDouble(stringArray[x]);
  5.         }
  6.  
This way, I could still use the array information in my calculations that I used later on in the code. But now I have to find out just what this scanner thing is. Do you have any links of where I could find out more about it? Thanks in advance!
Mar 6 '10 #3

anurag275125
P: 79
Scanner class allows you to take input from the keyboard.
You can get a little bit information about Scanner class by just typing java.util.Scanner at the command prompt.

for further information visit http://java.sun.com/javase/7/docs/ap...l/Scanner.html
Mar 6 '10 #4

Misaio
P: 16
Thanks so much, Anurag! This really helped me out. And it's something I hadn't read in my Java books yet.
Mar 6 '10 #5

P: n/a
thank you for your codes..
i highly appreciate it...
thank you and God bless,,,
Oct 6 '10 #6

P: 1
This code should help.

Expand|Select|Wrap|Line Numbers
  1. Scanner input = new Scanner(System.in);
  2.                 //Read user  input.
  3.                 System.out.println("How many numbers do you want to enter?");
  4.                 int num = input.nextInt();
  5.                 int array[] = new int[num];
  6.                 System.out.println("Enter the " + num + " numbers now.");
  7.                 for (int i = 0 ; i < array.length; i++ ) 
  8.                     {
  9.                         array[i] = input.nextInt();
  10.                     }
May 2 '13 #7

P: 1
your programme works but doesn't show the array that has been created. as system.out.println(""); suppose to show it.


@anurag275125
May 27 '13 #8

Nepomuk
Expert 2.5K+
P: 3,112
Lines 11-13 of anurags code will print the contents of the array. Alternatively, one could use the java.util.Arrays class like this:
Expand|Select|Wrap|Line Numbers
  1. System.out.println("Array elements are : ");
  2. System.out.println(Arrays.toString(a));
This can be very useful for debugging.
May 27 '13 #9

Post your reply

Sign in to post your reply or Sign up for a free account.