Garmaine Staff asked 1 year ago

i have an if statement that checks the input of a user, while the input is = "yes" the while loop runs if the input is "no" then the while loop stops, the if condition works properly but the else if doesn't work as expected.

  try {
        File myObj = new File("temperatures.txt");
        FileReader read = new FileReader(myObj);
        Scanner myReader = new Scanner(myObj);
        Scanner input = new Scanner(;
        LineNumberReader lines = new LineNumberReader(read);
        int len = lines.getLineNumber();
        int count = len;
        String ans;
        boolean stop = false;
        while(!stop && count > 0){
            for (int i = 0; i < len;i++){
                System.out.println(count+" Cities temperature left!");
                System.out.print("Do you want to check temperature details; enter y or n: ");
                ans = input.nextLine();
                    String[] getData = myReader.nextLine().split(" ");
                    String[] days = {"Mon", "Tue", "Weds", "Thurs", "Fri", "Sat", "Sun"};
                    int[] temp = new int[days.length];
                    for (int j = 0; j < days.length; j++){
                        System.out.println(days[j]+": "+getData[j + 1]);
                        temp[j] = Integer.parseInt(getData[j + 1]);
                    System.out.println("The average temperature: "+avgTemp(temp)+"*C");
                    System.out.println("The warmest temperature is: "+warmestTemp(temp)+"*C");
                    System.out.println("The coldest temperature is: "+coldestTemp(temp)+"*C");
                    System.out.println("The average temperature excluding coldest and warmest is: "+avgTempWithoutTwoExtremes(temp)+"*C");
                else if(ans.equals("n")){
                    stop = true;
                    System.out.println("invalid input");
                    stop = true;
    } catch (FileNotFoundException e) {
        System.out.println("An error occurred.");