tags:

views:

974

answers:

5

how to take user input in Array using Java? i.e we are not initializing it by ourself in our program but the user is going to give its value.. please guide!!

A: 

You can process the user-supplied arguments using

public static void main(String[] args) {
    // do whatever...
}

within your class that you're executing. Check out this link for a more detailed explantion.

Brian Agnew
can you please give any example of such code in which we prompt user to enter values in our array????
sadia
A: 

It vastly depends on how you intend to take this input, i.e. how your program is intending to interact with the user.

The simplest example is if you're bundling an executable - in this case the user can just provide the array elements on the command-line and the corresponding array will be accessible from your application's main method.

Alternatively, if you're writing some kind of webapp, you'd want to accept values in the doGet/doPost method of your application, either by manually parsing query parameters, or by serving the user with an HTML form that submits to your parsing page.

If it's a Swing application you would probably want to pop up a text box for the user to enter input. And in other contexts you may read the values from a database/file, where they have previously been deposited by the user.

Basically, reading input as arrays is quite easy, once you have worked out a way to get input. You need to think about the context in which your application will run, and how your users would likely expect to interact with this type of application, then decide on an I/O architecture that makes sense.

Andrzej Doyle
can you please give any example of such code in which we prompt user to enter values in our array????
sadia
sadia - in what context? What kind of application are you talking about, and how do you want the prompt presented?
Andrzej Doyle
i want to make a program in which i create an array and instead of initializing it by myself i want my user to enter values in my array. but i don't know how to prompt user for entering values in array in Java. so can you please give any such example in which any relevant code is shown? plz help!
sadia
Is it just me or is "any example" almost always mentioned in the context of "give me teh codez!"?
Joachim Sauer
+1  A: 

Here's a simple code that reads strings from stdin, adds them into List<String>, and then uses toArray to convert it to String[] (if you really need to work with arrays).

import java.util.*;

public class UserInput {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        Scanner stdin = new Scanner(System.in);

        do {
            System.out.println("Current list is " + list);
            System.out.println("Add more? (y/n)");
            if (stdin.next().startsWith("y")) {
                System.out.println("Enter : ");
                list.add(stdin.next());
            } else {
                break;
            }
        } while (true);

        System.out.println("List is " + list);
        String[] arr = list.toArray(new String[0]);
        System.out.println("Array is " + Arrays.toString(arr));
    }
}

See also:

polygenelubricants
A: 

import java.util.Scanner;

class Example{

//Checks to see if a string is consider an integer.

public static boolean isInteger(String s){

    if(s.isEmpty())return false;

    for (int i = 0; i <s.length();++i){

        char c = s.charAt(i);

        if(!Character.isDigit(c) && c !='-')

            return false;
    }

    return true;
}

//Get integer. Prints out a prompt and checks if the input is an integer, if not it will keep asking.

public static int getInteger(String prompt){
    Scanner input = new Scanner(System.in);
    String in = "";
    System.out.println(prompt);
    in = input.nextLine();
    while(!isInteger(in)){
        System.out.println(prompt);
        in = input.nextLine();
    }
    return Integer.parseInt(in);
}

public static void main(String[] args){
    int [] a = new int[6];
    for (int i = 0; i < a.length;++i){
        int tmp = getInteger("Enter integer for array_"+i+": ");//Force to read an int using the methods above.
        a[i] = tmp;
    }

}

}

flopex
you have never heard of Integer.parseInt("1")?
fuzzy lollipop
no i have never heard of such thing. basically i am new in java.
sadia
It's pretty basic. Accepts a string and parses it to an integer.
flopex
A: 

**How to accept array by user Input

Answer:-

import java.io.*;

import java.lang.*;

class Reverse1

{

public static void main(String args[]) throws IOException

{

int a[]=new int[25];

int num=0,i=0;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the Number of element");

num=Integer.parseInt(br.readLine());

System.out.println("Enter the array");

for(i=1;i<=num;i++)

a[i]=Integer.parseInt(br.readLine());

for(i=num;i>=1;i--)

{

System.out.println(a[i]);

}

}

}

Santosh kumar shah