-1

I want to take input For example:

550
234234
874982634
3487239482349

The length of the input is not fixed and i want to take input in the array list of java.

ArrayList<Integer> basicpg = new ArrayList<Integer>();
Tom
  • 16,842
  • 17
  • 45
  • 54
ak47_kalani
  • 15
  • 1
  • 8
  • 3
    Good luck, you can do it! Try to read [this](http://stackoverflow.com/help/how-to-ask) when you have some time. – User404 Jun 01 '15 at 06:04
  • `basicpg.add("550 234234 874982634 3487239482349")`. –  Jun 01 '15 at 06:05
  • Do you want to store your value as single entry or do you want to save each number separately? – PoisonedYouth Jun 01 '15 at 06:06
  • 1
    User404 is correct: it sounds like you pretty much understand the problem, and basically have the solution. Since the #/items is variable, you want a "List" (e.g. ArrayList). So I guess your question is "How do I input the items?". A: read from System.in, e.g. `Scanner sc = new Scanner(System.in);`. Here are a couple of approaches: http://stackoverflow.com/questions/5488072/reading-in-from-system-in-java – paulsm4 Jun 01 '15 at 06:09
  • @ak47_kalani follow [this](http://stackoverflow.com/questions/5857812/long-vs-integer-long-vs-int-what-to-use-and-when) – amila isura Jun 01 '15 at 06:13
  • Actually I want to take input from the user for example case1-550 case2-3249 case3-73408 , the length of the input is not fixed , i want to add it to array list but the length is not fixed and in one run only one input is fed – ak47_kalani Jun 01 '15 at 06:30

4 Answers4

1
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
ArrayList<Integer> basicpg = new ArrayList<Integer>();
String input;
while((input = br.readLine()) != null && input.length() != 0)
{
    basicpg.add(Integer.parseInt(input));
}

1st case If they are space seperated, use .split()

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
String[] tokens = input.split(" ");
List<String> wordList = Arrays.asList(tokens); 

enter image description here

Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
  • Actually I want to take input from the user for example case1-550 case2-3249 case3-73408 , the length of the input is not fixed , i want to add it to array list but the length is not fixed and in one run only one input is fed – ak47_kalani Jun 01 '15 at 06:29
1
ArrayList<Long> basicpg = new ArrayList<Long>();
basicpg.add(550);
basicpg.add(234234);
basicpg.add(874982634);
basicpg.add(3487239482349);

If the Integer length is too long you might want to change it to Long. Integer has a limit & since you are not sure as to what might be the length of the input integer. Check this, or you can put a condition wherein the input integer should be <=Integer.MAX_VALUE

Community
  • 1
  • 1
underdog
  • 4,447
  • 9
  • 44
  • 89
0

You may use the following code snippet -

Scanner s = new Scanner(System.in);
ArrayList<Integer> basicpg = new ArrayList<Integer>();

while(s.hasNexInt()){

 Integer i = s.nextInt();
 basicpg.add(i);

}
Razib
  • 10,965
  • 11
  • 53
  • 80
0

why not just split the input string with space and add it in a loop?

String s = "550 234234 874982634 3487239482349"
ArrayList<Integer> basicpg = new ArrayList<>();
for(ss: s.split()){
    try{
       Integer i = Integer.parseInt(ss);
       basicpg.add(i);
    }catch(Exception e){
       e.printStackTrace();
       contine;
    }
}
armnotstrong
  • 8,605
  • 16
  • 65
  • 130