简体   繁体   中英

Is there a way to stop scanner input loop after the condition has been met?

I am attempting to only allow the user to pick from the given of input. If they pick something other than "f", "F", "c", or "C" for the first optionGate , then they will be prompted to try again and looped back to the scanner. It is the same situation for the next gate, optionGate2 .

The gates accomplish what I wanted them to by stopping the user from putting in a different input than the options given. My problem comes in when both booleans are turned to true and the gates are passed. The scanner for some reason still asks for user input in an infinite loop. I can then type anything into the scanner over and over and the program runs no further.

Any suggestions or comments about what is going on?

Scanner in = new Scanner(System.in);
boolean optionGate = false;
boolean optionGate2 = false;

String tempLabel = "";    //initialize to F
String precipLabel = ""; //initialize to in

while(optionGate != true){
    System.out.println("Choose the temperature scale (F = Fahrenheit, C = Celsius:)");
    String scaleT = in.nextLine();

    if("f".equalsIgnoreCase(scaleT)){
        tempLabel = "F";
        optionGate = true;
    }
    else if("c".equalsIgnoreCase(scaleT)){
        tempLabel = "C";
        optionGate = true;
    }
    else{
        System.out.println("Error: not an option. Try again:");
        optionGate = false;
    }
}

while(optionGate2 != true){
    System.out.println("Choose the precipitation scale (i = inches, c = centimeters:)");
    String scaleP = in.nextLine();

    if("i".equalsIgnoreCase(scaleP)){
        precipLabel = "in";
        optionGate2 = true;
    }
    else if("c".equalsIgnoreCase(scaleP)){
        precipLabel = "cm";
        optionGate2 = true;
    }
    else{
        System.out.println("Error: not an option. Try again:");
        optionGate2 = false;
    }
}

EDIT: Adding the code that follows the above snippet. (Sorry for the obvious lack of stackoverflow know-how. This is my first post on the platform).

String [] month = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
        double [] temperature ={60.9, 62.6, 67.4, 71.5, 77.1, 81.2, 82.4, 82.5, 81.1, 75.3, 68.8, 63.0};     //initialize with Fahrenheit values
        double [] precipitation ={2.4, 2.4, 3.5, 2.4, 3.7, 7.4, 7.2, 6.3, 5.8, 2.7, 2.3, 2.3};      //initialize with inch values

        double[] updatedTemperature;
        updatedTemperature = new double[temperature.length];

        double[] updatedPrecipitation;
        updatedPrecipitation = new double[precipitation.length];


        while("f".equalsIgnoreCase(tempLabel)){
            for(int i = 0 ; i < temperature.length ; i++) {
            updatedTemperature[i] = temperature[i];
        }

             if("in".equals(precipLabel)){
            for(int i = 0 ; i < precipitation.length ; i++){
            updatedPrecipitation[i] = precipitation[i];    
            }

        }
        else if("cm".equals(precipLabel)){
            for(int i = 0 ; i < precipitation.length ; i++){
            updatedPrecipitation[i] = (precipitation[i] * 2.54);    
            }

        }
       }

        while("c".equalsIgnoreCase(tempLabel)){
           for(int i = 0 ; i < temperature.length ; i++) {
            updatedTemperature[i] = ((temperature[i] - 32) * (5.0/9));
        }


             if("in".equals(precipLabel)){
            for(int i = 0 ; i < precipitation.length ; i++){
            updatedPrecipitation[i] = precipitation[i];    
            }   

        }
        else if("cm".equals(precipLabel)){
            for(int i = 0 ; i < precipitation.length ; i++){
            updatedPrecipitation[i] = (precipitation[i] * 2.54);    
            }

        }
        }




        //Processing - calculate average temperature and total precipitation
        for( int index = 0; index < updatedTemperature.length; index++)
        {
            sum += updatedTemperature[index];
        }
        avg = sum/updatedTemperature.length;

EDIT: Here is all of the code inside of my main method:


        //Declare and initialize variables
        Scanner in = new Scanner(System.in);
        String city = "Orlando";
        String state = "FL";
        double sum = 0;
        double avg = 0;
        boolean optionGate = false;
        boolean optionGate2 = false;

        String tempLabel = "";    //initialize to F
        String precipLabel = ""; //initialize to in

        while(optionGate != true){
        System.out.println("Choose the temperature scale (F = Fahrenheit, C = Celsius:)");
        String scaleT = in.nextLine();

        if("f".equalsIgnoreCase(scaleT)){
            tempLabel = "F";
            optionGate = true;
        }
        else if("c".equalsIgnoreCase(scaleT)){
            tempLabel = "C";
            optionGate = true;
        }
        else{
            System.out.println("Error: not an option. Try again:");
            optionGate = false;
        }
        }

        while(optionGate2 != true){
        System.out.println("Choose the precipitation scale (i = inches, c = centimeters:)"); 
        String scaleP = in.nextLine();

        if("i".equalsIgnoreCase(scaleP)){
            precipLabel = "in";
            optionGate2 = true;
        }
        else if("c".equalsIgnoreCase(scaleP)){
            precipLabel = "cm";
            optionGate2 = true;
            in.close();
        }
        else{
            System.out.println("Error: not an option. Try again:");
            optionGate2 = false;
        }
        }




        String [] month = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
        double [] temperature ={60.9, 62.6, 67.4, 71.5, 77.1, 81.2, 82.4, 82.5, 81.1, 75.3, 68.8, 63.0};     //initialize with Fahrenheit values
        double [] precipitation ={2.4, 2.4, 3.5, 2.4, 3.7, 7.4, 7.2, 6.3, 5.8, 2.7, 2.3, 2.3};      //initialize with inch values

        double[] updatedTemperature;
        updatedTemperature = new double[temperature.length];

        double[] updatedPrecipitation;
        updatedPrecipitation = new double[precipitation.length];


        while("f".equalsIgnoreCase(tempLabel)){
            for(int i = 0 ; i < temperature.length ; i++) {
            updatedTemperature[i] = temperature[i];
        }

             if("in".equals(precipLabel)){
            for(int i = 0 ; i < precipitation.length ; i++){
            updatedPrecipitation[i] = precipitation[i];    
            }

        }
        else if("cm".equals(precipLabel)){
            for(int i = 0 ; i < precipitation.length ; i++){
            updatedPrecipitation[i] = (precipitation[i] * 2.54);    
            }

        }
       }

        while("c".equalsIgnoreCase(tempLabel)){
           for(int i = 0 ; i < temperature.length ; i++) {
            updatedTemperature[i] = ((temperature[i] - 32) * (5.0/9));
        }


             if("in".equals(precipLabel)){
            for(int i = 0 ; i < precipitation.length ; i++){
            updatedPrecipitation[i] = precipitation[i];    
            }   

        }
        else if("cm".equals(precipLabel)){
            for(int i = 0 ; i < precipitation.length ; i++){
            updatedPrecipitation[i] = (precipitation[i] * 2.54);    
            }

        }
        }




        //Processing - calculate average temperature and total precipitation
        for( int index = 0; index < updatedTemperature.length; index++)
        {
            sum += updatedTemperature[index];
        }
        avg = sum/updatedTemperature.length;

        //Output: display table of weather data including average and total
        System.out.println();
        System.out.println("           Weather Data");
        System.out.println("      Location: " + city +", " + state);
        System.out.println("Month     Temperature (" +  tempLabel + ")     Precipitation (" + precipLabel + ")");
        System.out.println();
        System.out.println("***************************************************");
        for( int index = 0; index < temperature.length; index++)
        {
            System.out.println(month[index] + "\t   " + updatedTemperature[index] + "\t\t       " + updatedPrecipitation[index]);
        }
        System.out.println("***************************************************");
        System.out.println("Average: " + avg + "    Total: " + sum);

Your code enter loop at the lines

while("c".equalsIgnoreCase(tempLabel)){

while("f".equalsIgnoreCase(tempLabel)){

and never exit.

You can change while on if construction here

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