473,395 Members | 1,411 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How do I create an array using user input?

Misaio
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

8 134178
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
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
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
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
thank you for your codes..
i highly appreciate it...
thank you and God bless,,,
Oct 6 '10 #6
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
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
3,112 Expert 2GB
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

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

Similar topics

15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
0
by: Paulers | last post by:
hello VB masters I have an issue that I am hoping you can help me with. I am creating an application that accepts user input via a richtextbox. the user can enter multiple entries and I need to...
6
by: joeschnell | last post by:
I'm in wk 12 of my first programming course, assignment is to create program using 2dimen array that displays grade, why does my code work for everything unless the user enters the final element...
5
by: yohan610 | last post by:
i have to build a program where the user enters the required input and output filenames..how do i open such files? tht is input_file = fget(............) finput =fopen(input_file,"rb") i...
4
by: mohaaron | last post by:
I can think of a lot of reasons why this might need to be done but as far as I can tell it's not possible. I've been looking for a way to add HtmlTableRows to a table using a button click for a...
3
by: stupidnewb | last post by:
I am having some trouble getting a prompt to print out so a user can know what information to type. I have a student class that has a scanner as a constructor. public Person(Scanner in) {...
5
by: Dbarten1982 | last post by:
I am brand new to C++ programming, and am completely stuck on a program. In the program I am supposed to create a string array that stores 5 user input words, and the string constant "END_OF_ARRAY'...
3
by: rickytb | last post by:
Hey all, I've hit a snag on a beginner Java problem involving building occupancy. I'm supposed to get user input from a GUI. My main problem is that I'm totally lost on how to take the information...
0
by: onyris | last post by:
Hi have this query which works ok now : SELECT * FROM WHERE (((Table.published) Like '*'&!!& '*' And (Table.published) Like '*'&!!& '*' and (Table.address) Like '*'&!!& '*' )); and...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.