Getting variable number of inputs Using Scanner in Java

2292 views java
0

I want to have input like:- The first line of the input contains the two cities for where the listing of flight options must be produced – the departure city and the arrival city, separated by a white-space.

The next N lines will contain the available flights and their costs. Each line is a direct flight starting with the departure city name, followed by the arrival city name, and lastly followed by the cost of that flight – all three separate by a white-space character.

Where 1<=N<=20.

Can anyone plz help I want to know how to end the input. I am using:-

Scanner s = new Scanner(System.in);
while(!(input = s.nextLine()).equals(" "))

but all in vain.

The data is in the form of

CityA CityB // Upto its I have done. The next N lines i.e. second paragraph is a confusion 

Next N lines should be in form

CityA CityC 5000
CityA CityD 3000

answered question

Can you paste that date you will be scanning?

I have edited the question @JoseMartinez. Mainly I want to get input from user via console using Scanner class.

1 Answer

3

You have to use the scanner.hasNext() method for check available inputs

posted this

Have an answer?

JD

Please login first before posting an answer.