-2

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);
  • 1
    Whatever the problem is, it doesn't seem to be in the part of the code you're showing. What happens after that? – Kevin Anderson Nov 03 '19 at 13:13
  • 1
    **Unable to reproduce.** If I copy/paste that into an empty `main` method and run the program, the programs stops when both prompts have been answered correctly. If your program keeps prompting you, it's in the code that follows the code you've shown, and we can't help fixing bugs in unseen code. – Andreas Nov 03 '19 at 13:13
  • I'd recommend adding logs and checking the line at which control is getting stuck. I highly doubt that the problem is indeed with the snippet you've posted. – prakasht Nov 03 '19 at 13:16
  • If the scanner class isn't used anywhere else in the program, could the code following this snippet still be the issue? – typicalmammoth Nov 03 '19 at 13:16
  • Edited to include the code following the previous snippet. – typicalmammoth Nov 03 '19 at 13:20
  • As you said, the rest of the code doesn't use `in`, so no, the program wouldn't still be prompting. Is that the totality of your code, i.e. all that is simply inside a `main` method, with no other code present? – Andreas Nov 03 '19 at 13:23
  • I've now updated the question to include the everything inside of my main method. – typicalmammoth Nov 03 '19 at 13:26

1 Answers1

0

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

hujakhujak
  • 28
  • 1
  • 5