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(System.in);
LineNumberReader lines = new LineNumberReader(read);
lines.skip(Long.MAX_VALUE);
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();
if(ans.equals("y")){
String[] getData = myReader.nextLine().split(" ");
System.out.println(getData[0]);
System.out.println("************");
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");
System.out.println("*********************");
count--;
}
else if(ans.equals("n")){
stop = true;
}
else{
System.out.println("invalid input");
stop = true;
}
}
}
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
Please login or Register to submit your answer