1

I want to read n values from the user but i don't know the value of n.

Say in first case {4,3,5,6,11,22} In second case{11,22,77,43,2,1,2111,322} Say i want to read 10 integer values from the user(second time 5 int value)(depends on each cases).

Second thing is I want to store this values in an array.

I am really stuck with this. Any help???

I tried the following code-

int a[50],i=-1;//how to dynamically assign memory to an array
Scanner s=new Scanner(System.in);
do{
   a[++i]=s.nextInt();
}while(!hasNextLine());
9
  • 2
    why don't you ask the user about number and assign that to a final variable that can be used as the size of your array? Commented May 29, 2015 at 6:47
  • Actually it's an interview question sir. I am preparing for it. Question is given as without knowing n values. Commented May 29, 2015 at 6:50
  • If n is unknown then you need a dynamically sized collection such as List. Commented May 29, 2015 at 6:51
  • So you can have an array, e.g. int a[]; initially, user inputs n, and you'll do this a = new int[n]. Commented May 29, 2015 at 6:51
  • Please post the question Commented May 29, 2015 at 6:52

4 Answers 4

3

Based of what I understood use this.

public static void main(String[] args){
        int a[] = null;
        int i = 1;
        Scanner s=new Scanner(System.in);
        while (s.hasNextLine()){
            int temp[] = null;
            if(a!=null){
                 temp = a;
            }
            a = new int[i];
            a[i-1]=s.nextInt();         
            if(temp!= null){
                for(int j=0;j<temp.length;j++){
                a[j]=temp[j];   
                }

            }
            i++;
        }

    }
Sign up to request clarification or add additional context in comments.

1 Comment

If there is no input left.
-1

Try this:

import java.io.*;

public class Hey

{
    public static void main(String ar[])throws IOException
    {
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       String input = "";//get the input
       while((input = br.readLine()) != null)
            {
                String []numbers = input.split(",");
                System.out.println(numbers.length);
            }
    }
}

Look at this

5 Comments

while(T -- != 0)? Why the T--?
T is the number of testcases.
I don't know how much values to be stored in that array?? this code isn't the answer for my question
@RangaGanesh If you know the values are seperated by a comma, then use my code
@RangaGanesh if you want to create arrays dynamically each time a line of input gets changed, then this might help you
-1
#include<stdio.h>
#include<stdlib.h>
int main()
{
    int n,x,c;
    scanf("%d",&n);
    while((x=getchar())!='\n')
    {
        scanf("%d",&x);
        print("%d\n");
    }
}

1 Comment

A code-only answer is not high quality. While this code may be useful, you can improve it by saying why it works, how it works, when it should be used, and what its limitations are. Please edit your answer to include explanation and link to relevant documentation.
-2

How is the user going to indicate that the input has finished? How is the format of the input? I suppose:

  • User inputs one Integer for each line or a list of integers separated by spaces.
  • User inputs an empty line or something different to an Intege tor indicate it has finished.

You need an ArrayList because you don't know the number of inputs before you start asking for it. ArrayList are dinamic so you can add content to it without declaring its size before.

Scanner s = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<>();
while(s.hasNextInt()){

    list.add(s.nextInt());

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.