0

I've tested this out many times but I can't figure out what's wrong. Every time I compile it asks for the range like it's supposed to but after the upperRange is set it keeps on asking for more inputs and never gets to the rest of the program.
Here's the top segment:

   Scanner in = new Scanner(System.in);

    System.out.printf("%30s%n","Range of Years");
    System.out.println("Enter the lowest year of the range:");
    int lowerRange = in.nextInt();
    System.out.println("Enter the highest year of the range:");
    int upperRange = in.nextInt();

    if(lowerRange>upperRange){
    System.out.println("Please make sure the upper range is later than the lower range.");
    System.out.println("Enter the lowest year of the range:");
    lowerRange = in.nextInt();
    System.out.println("Enter the highest year of the range:");
    upperRange = in.nextInt();
}
   else if(lowerRange<1995){
    System.out.println("Sorry, no data on hurricanes from before 1995. Please try 1995 or later.");
    System.out.println("Enter the lowest year of the range:");
    lowerRange = in.nextInt();
    System.out.println("Enter the highest year of the range:");
    upperRange = in.nextInt();
}
  else if(upperRange>2015){
    System.out.println("Sorry, no data on hurricanes from after 2015. Please try 2015 or earlier.");
    System.out.println("Enter the highest year of the range:");
    upperRange = in.nextInt();
}

    String token = "";
    File hurricaneData = new File("hurricanedata.txt");
    Scanner inFile = new Scanner(hurricaneData);


int counter = 0;
while(inFile.hasNextLine()){
    counter++;
    inFile.hasNextLine();
}
  • 5
    Your final loop never consumes any data. You need to call `nextLine()`. – shmosel Oct 25 '18 at 22:50
  • 1
    What do you mean by "it keeps on asking for more inputs"? It seems to me that this program ought to just freeze when it gets to the `while` loop, because if `hasNextLine()` returns true once, it will keep on returning `true` until you consume some lines from the file. – Dawood ibn Kareem Oct 25 '18 at 22:54
  • try `Integer.parseInt(in.nextLine())` instead of `in.nextInt()` – RAZ_Muh_Taz Oct 25 '18 at 22:55
  • 1
    This is not a duplicate, as `inFile` is a different scanner. Reopened. – shmosel Oct 25 '18 at 23:02

0 Answers0