0

I have a Problem where i need to take series of input (Integers) from the user. Where input can be variable the Challenge can't use any type of Array List or hash map or map or linked hashmap etc.

So I thought of Var-args.

Can we take input var-args in Scanner Class? I tried something like this but its not working.

class Caseone
{
     public void inputNumbers(int ... args)
     {

            for(int x: args)
            {
                System.out.print(x);
            }

     }
}
public class Tryon {

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
          Caseone obj = new Caseone();
          System.out.println("Input Please");
          int m = in.nextInt();
          obj.inputNumbers(m);


    }

}
Dino
  • 7,779
  • 12
  • 46
  • 85
  • Then use an array (which is compatible with var-args) – ernest_k Oct 01 '19 at 06:14
  • do you know how many int will be provided by user? – SSP Oct 01 '19 at 06:16
  • @SSP Nope.It is a variable. Is there any other way? –  Oct 01 '19 at 06:20
  • @ernest_k Since the input can be variable how to allocate the Array Length? –  Oct 01 '19 at 06:20
  • How will you know that input is ended. – SSP Oct 01 '19 at 06:22
  • @kundevme2 that depends on how you have to take that input. You can: 1-ask the user the number of ints they want to input, create an array of that length and loop to take the input, or 2-ask them to enter ints as space or comma separated, then use string.split to create the array, followed by integer.parseint – ernest_k Oct 01 '19 at 06:23
  • Maybe making an user to hit "Enter"...we can make him giving the input like "1,5,8,9...so on" @SSP –  Oct 01 '19 at 06:23
  • @ernest_k I have tried both of that. Since its a Challenge, i have to do with basic functions. cannot use "Split" or "integer.parseint". –  Oct 01 '19 at 06:25
  • Then use option 1 (ask the user how many numbers, then create an `int[]` array of that size, then loop to take one number at a time. after all that, call your method using the `int[]` array you created as argument) – ernest_k Oct 01 '19 at 06:43
  • @ernest_k Can we take input var-args in Scanner Class ? Is it Possible? –  Oct 01 '19 at 06:53

2 Answers2

0

You can read the whole line in the case and parse it youself. Use nextLine instead of nextInt here. Like

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Input Please");
    List<Integer> result = Arrays.stream(in.nextLine().split("\\s+"))
            .map(Integer::parseInt)
            .collect(Collectors.toList());
    System.out.println("you have " + result.size() + " of ints: " + result);
}

will behave like

Input Please
> 1 2 3 4 5 6
you have 6 of ints: [1, 2, 3, 4, 5, 6]
lotor
  • 1,060
  • 7
  • 18
0

You can read the next integer until the input stream has any:

System.out.println("Input Please");
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
  int next = in.nextInt();
  System.out.print(next);
  System.out.print(' ');
}

The program can be terminated in different ways, depending on the environment (OS, IDE, etc.)

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147