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());
Ranga Ganesh
  • 75
  • 1
  • 3
  • 12
  • 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? – Juned Ahsan May 29 '15 at 06:47
  • Actually it's an interview question sir. I am preparing for it. Question is given as without knowing n values. – Ranga Ganesh May 29 '15 at 06:50
  • If n is unknown then you need a dynamically sized collection such as List. – Juned Ahsan May 29 '15 at 06:51
  • So you can have an array, e.g. `int a[];` initially, user inputs `n`, and you'll do this `a = new int[n]`. – almightyGOSU May 29 '15 at 06:51
  • Please post the question – Uma Kanth May 29 '15 at 06:52
  • Do you have to use arrays? What if you put the values into some List, like ArrayList? – Michal Krasny May 29 '15 at 06:52
  • Ya its specified as arrays. – Ranga Ganesh May 29 '15 at 06:58
  • Not sure what your are after but if you must use Array you can do it by using an other temp array and swapping the values. Here is the code (not debugged) `while (s.hasNextLine()){ int temp[] = null; if(a!=null){ temp = a; } a = new int[i]; a[i]=s.nextInt(); if(temp!= null){ for(int j=0;j – Shahzeb May 29 '15 at 07:11
  • If you do not know what the size of the array will be, and yet you want to use an array, just declare a gigantic array then? `int [] arr = new int [9912312]`?? I am quite sure this is a simple question, and you just do not know what you want, so here is a stupid solution if you insist. – almightyGOSU May 29 '15 at 07:14

4 Answers4

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++;
        }

    }
Shahzeb
  • 4,745
  • 4
  • 27
  • 40
-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

Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
-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");
    }
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • [A code-only answer is not high quality](//meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers). 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. – Stephen Ostermiller Oct 22 '21 at 08:55
-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());

}
ismaro3
  • 79
  • 1
  • 5