I'm currently in the middle of a computing program in which I have to calculate and print data for hurricanes given a certain range of years. I know how to put all the information into arrays, but I don't know how to get a certain range of years from user input.
For example, the user types in 1995-2000, I can say that the first year is 1995, and the second year is 2000, I have to find all the values inclusive between those two numbers. I am looking for anywhere within the range of 1995-2015.
My main question comes from the second and third while statements. If I were to print the result of them, both of them equal zero. I assume that this means that the loop doesn't run through at all, but what am I doing wrong?
import java.util.Scanner;
import java.io.IOException;
import java.io.File;
import java.io.PrintWriter;
public class HurricaneSelector
{
public static void main (String []args)throws IOException{
File fileName = new File("hurricanedata.txt");
Scanner in = new Scanner(System.in);
Scanner inFile = new Scanner(fileName);
PrintWriter outFile = new PrintWriter(new File("hurricaneanalysis.txt"));
int j = 0;
int numLines = 1000;
int totalTokens = 0;
String countLine = " ";
boolean isFirstYear = false;
boolean isSecondYear = false;
boolean notSecondYear = false;
int firstYearCounter = 0;
int secondYearCounter = 0;
double minimum = Double.MAX_VALUE;
double maximum = Double.MIN_VALUE;
double categoryAverage = 0;
int categoryMin = Integer.MAX_VALUE;
int categoryMax = Integer.MIN_VALUE;
double pressureAverage = 0;
double pressureMin = Double.MAX_VALUE;
double pressureMax = Double.MIN_VALUE;
double windAverage = 0;
double windMin = Double.MAX_VALUE;
double windMax = Double.MIN_VALUE;
int category1 = 0;
int category2 = 0;
int category3 = 0;
int category4 = 0;
int category5 = 0;
int[] year = new int[numLines];
String[] month = new String[numLines];
int[] pressure = new int[numLines];
double[] windSpeed = new double[numLines];
String[] name = new String[numLines];
int[] category = new int[numLines];
File fileName2 = new File("hurricanedata.txt");
Scanner inFile2 = new Scanner(fileName2);
while (inFile2.hasNext()){
year[j] = inFile2.nextInt();
month[j] = inFile2.next();
pressure[j] = inFile2.nextInt();
windSpeed[j] = inFile2.nextInt();
windSpeed[j]*=1.15078;
name[j] = inFile2.next();
if(windSpeed[j] > 74 && windSpeed[j] < 96){
category[j] = 1;
} else if(windSpeed[j] > 96 && windSpeed[j] < 110){
category[j] = 2;
} else if(windSpeed[j] > 111 && windSpeed[j] < 129){
category[j] = 3;
} else if(windSpeed[j] > 130 && windSpeed[j] < 156){
category[j] = 4;
} else{
category[j] = 5;
}
j++;
}
System.out.println("Please type a range of years from 1995-2015 using this format (XXXX-XXXX)");
String userInput = in.next();
String firstYear = userInput.substring(0, 4);
String secondYear = userInput.substring(5);
int firstYearInt = Integer.parseInt(firstYear);
int secondYearInt = Integer.parseInt(secondYear);
while(isFirstYear = false){
if(firstYearInt == (year[firstYearCounter])){
isFirstYear = true;
}else {
firstYearCounter++;
}
}
while(isSecondYear = false){
if(secondYearInt > (year[secondYearCounter])){
isSecondYear = true;
} else{
secondYearCounter++;
}
}
System.out.println(firstYearCounter);
System.out.println(secondYearCounter);
firstYearCounter-=1;
secondYearCounter-=1;
totalTokens = secondYearCounter - firstYearCounter;
for (int i = firstYearCounter; i < secondYearCounter; i++) {
pressureAverage+=pressure[i];
if(category[i] == 1){
categoryAverage+=category[i];
category1++;
} else if(category[i] == 2){
categoryAverage+=category[i];
category2++;
} else if(category[i] == 3){
categoryAverage+=category[i];
category3++;
} else if(category[i] == 4){
categoryAverage+=category[i];
category4++;
} else{
categoryAverage+=category[i];
category5++;
}
}
for (int i = firstYearCounter; i < secondYearCounter; i++) {
if (pressure[i] > pressureMax) {
pressureMax = pressure[i];
}
if (pressure[i] < pressureMin) {
pressureMin = pressure[i];
}
if (windSpeed[i] < windMin) {
windMin = windSpeed[i];
}
if (windSpeed[i] > windMax) {
windMax = windSpeed[i];
}
if (category[i] > categoryMax) {
categoryMax = category[i];
}
if (category[i] < categoryMin) {
categoryMin = category[i];
}
}
categoryAverage = categoryAverage / totalTokens;
pressureAverage = pressureAverage / totalTokens;
windAverage = windAverage / totalTokens;
System.out.println(" Hurricanes " + firstYear + " - " + secondYear);
System.out.println();
System.out.println("Year Hurricane Category Pressure (mb) Wind Speed (mph)");
System.out.println("=====================================================================");
for (int i = firstYearCounter; i < secondYearCounter; i++) {
System.out.printf("%d%13s%10d%16d%18d\n" , year[i] , name[i] , category[i] , pressure[i] , (int)windSpeed[i]);
outFile.printf("%d%13s%10d%16d%18d\n" , year[i] , name[i] , category[i] , pressure[i] , (int)windSpeed[i]);
}
System.out.println("=====================================================================");
System.out.printf("%s%20f%16f%18f\n" , "Average" , categoryAverage , pressureAverage , windAverage);
System.out.printf("%s%20d%16f%18f\n" , "Maximum" , categoryMax , pressureMax , windMax);
System.out.printf("%s%20d%16f%18f\n" , "Minimum" , categoryMin , pressureMin , windMin);
System.out.println();
System.out.println("Number of Category 1: " + category1);
System.out.println("Number of Category 2: " + category2);
System.out.println("Number of Category 3: " + category3);
System.out.println("Number of Category 4: " + category4);
System.out.println("Number of Category 5: " + category5);
}
}
Any help is appreciated!
Here
while(isFirstYear = false)
=
assigns false
to isFirstYear
and your loop always evaluates to false. comparison is done like this
while(isFirstYear == false)
You made same mistake in your second loop.
you can shorten your code if you like
while(!isFirstYear)
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.