1
votes

I am trying to run while loop for each input user enter but don't know how to stop if user is not entering input. I need help in stopping while loop if user doesn't enter input.

Code:

import java.util.*;

class petrol{
    public static void main(String[] args){
    int vehicle_counter=0,petrol_counter=0;
    System.out.println("Enter quantity of petrol to be filled in vehicles seperated by space:");
    Scanner s1 = new Scanner(System.in);
    ArrayList<Integer> array = new ArrayList<Integer>();
    
    while(true){
        if(petrol_counter<=200 && array.size()<50){
            int input = s1.nextInt();
            petrol_counter=petrol_counter+input;
            vehicle_counter++;
            array.add(input);   
        }
        else{
            System.out.println("Quantity Excedded then 200 Litres or no of Vehicles excedded then 50.");
            break;
        }   
    }
    System.out.println("Hii");
    }
    
}

e.g: If I enter 2 3 4 2 and press enter loop should stop.

Problem with possible solution : If I use while(s1.nextInt().equals(true)) I get an error. How do I use break?

2
As Maruthi mentioned in the answer section, BufferedReader is better way. But if you still want to stick to Scanner, then this is not a preferable solution but just to give you another line of thought, probably you can use while(s1.hasNextInt()) instead of while(true). Now if you want to stop inputting values, then before pressing enter type any non integer characters like punctuation.Aniruddha Sardar
@AniruddhaSardar s1.hasNextInt() works. i added a cooment to add period(.) at the end.Jay Patel

2 Answers

0
votes

So, you can try something like this. Instead of Scanner, use BufferedReader to parse the input since all you need is a single line input. Also, BufferedReader is faster compared to Scanner.

Here is the code:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Petrol {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int vehicle_counter=0,petrol_counter=0;
        System.out.println("Enter quantity of petrol to be filled in vehicles seperated by space:");
        String input = br.readLine();
        String[] values = input.split(" ");
        if(values.length>50){
            System.out.println("Vehicles exceeded 50");
        }else{
            for(int i=0;i<values.length;i++){
                int ip = Integer.parseInt(values[i]);
                petrol_counter+=ip;
                vehicle_counter++;
                
                if(petrol_counter>200){
                    System.out.println("Petrol Quantity exceeded 200L");
                }
            }
        }
        
        System.out.println(petrol_counter+" Litres "+ " Vehicles "+vehicle_counter);

    }

}

0
votes

If you want to keep the Scanner you could also make the input as char and having a if statement that will exit the loop when you press a certain character.

import java.util.*;

public class petrol {
    public static void main(String[] args) {
        int vehicle_counter = 0, petrol_counter = 0;
        System.out.println("Enter quantity of petrol to be filled in vehicles seperated by space:");
        Scanner s1 = new Scanner(System.in);
        ArrayList<Integer> array = new ArrayList<Integer>();

    while (true) {
        if (petrol_counter <= 200 && array.size() < 50) {
            char input = s1.next().charAt(0); // use char instead

            if (input == 'q' || input == 'Q') { //if user enters q or Q
                System.out.println("Quantity Excedded then 200 Litres or no of Vehicles excedded then 50.");
                break;
            }

            petrol_counter = petrol_counter + input;
            vehicle_counter++;
            array.add((int) input); //Cast to int
        }
    }
    System.out.println("Hii");
}

}