473,325 Members | 2,342 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,325 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 134120
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.