shaythan
shaythan

Reputation: 61

Inputing multiple space seperated integers in one line (java)

recently i started learning java and i came across an annoying issue which i did not find an answer for. when recieving an input you can't define 2 diffrent variables in one line. (unless it's an array, and then it's inside a loop) for exemple, here are 5 diffrent integers, and i wrote 5 diffrent lines to define them all:

        Scanner input = new Scanner(System.in);
        int 1 = input.nextInt();
        int 2 = input.nextInt();
        int 3 = input.nextInt();
        int 4 = input.nextInt();
        int 5 = input.nextInt();

it is important to read all of them space seperated, and they can't be inputed together to a sring and then be seperated, or to an array.

is there a way to minimize those 5 lines to 1 and still read all these integers space seperated? thanks to whoever answers!

Upvotes: 0

Views: 111

Answers (1)

Olivier Croisier
Olivier Croisier

Reputation: 6149

Well...

int i1 = input.nextInt(), 
    i2 = input.nextInt(),
    i3 = input.nextInt(),
    i4 = input.nextInt(),
    i5 = input.nextInt();

This is only 1 statement.

Upvotes: 2

Related Questions