简体   繁体   中英

How to run while loop till user enter input?

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");
    }
    
}

eg: 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?

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);

    }

}

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");
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM