0

The code throws these exceptions after being ran while taking an input first input works correctly but the second ones throws an exception

sample output should be like this

program output

Exception in thread "main" java.util.NoSuchElementException
        at java.util.Scanner.throwFor(Scanner.java:862)
        at java.util.Scanner.next(Scanner.java:1371)
        at WeatherEngine.WeatherRecord(WeatherEngine.java:53)
        at WeatherProcessingSystem.main(WeatherProcessingSystem.java:36)
    WARNING: process exited with a(n) Unknown (1) error code

MAIN METHOD

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int choice=0;
    boolean flag = true;
    WeatherEngine weather = new WeatherEngine();
    System.out.println("Welcome to Weather Data Processing System!"
            +"\nYou can process weather information using this system.\n"
            +"\nPlease select a feature you wish to use from the following:\n");
    do{
        System.out.println("1. \t Weather Record"
                +"\n2. \t Averages"
                +"\n3. \t Maximums"
                +"\n4. \t Minimums"
                +"\n5. \t Total Precipitation"
                +"\n6. \t Extremes"
                +"\n0. \t Quit");
        do{
            try{
                flag=false;
                choice = scan.nextInt();
                if(choice<0||choice>6)
                    throw new Exception();
            }catch(Exception e){
                System.out.println("Wrong input \n Enter again");
                scan.next();
                flag=true;
            }
        }while(flag);
        flag = true;
        switch(choice){
        case 1:{
            weather.WeatherRecord();
            break;
        }
        case 2:{
            weather.Average();
            break;
        }
        case 3:{
            weather.Maximum();
            break;
        }
        case 4:{
            weather.Minimum();
            break;
        }
        case 5:{
            weather.TotalPrecipitation();
            break;
        }
        case 6:{
            weather.Extremes();
        }
        default:{
            flag = false;
        }
        }
    }while(flag);
}

weatherRecord method

    public void WeatherRecord(){
    boolean flag=true;
    do{
        try{
            flag=false;
    System.out.println("Enter date in MM/DD/2008");
    String str = scan.next();
    System.out.println(str);
    String [] str2 =str.split("/");
    int a =  Integer.parseInt(str2[0])-1;
    int b = Integer.parseInt(str2[1])-1;    
    for(int i=0;i<4;i++){
        System.out.printf("Loaction: %3s \t\t Date: %2s %2d,2008 \n",wrec[i][a][b].getlocation(),mon[a],(b+1));
        System.out.printf("High Temp: %3d \t\t\t Low Temp: %3d \n",wrec[i][a][b].getHigh(),wrec[i][a][b].getLow());
        System.out.printf("Avg Wind: %3d \t\t\t Max Wind %3d\n",wrec[i][a][b].getWinds(),wrec[i][a][b].getGusts());
        System.out.printf("Precipitaion: %3.2f inches\n \n",wrec[i][a][b].getPrecip());
        }
        }catch(Exception e){
            System.out.println("Wrong input \n Enter again");
            scan.next();
            flag=true;
        }
        }while(flag);   

    }
irrelevent
  • 27
  • 1
  • 6
  • Did you create another instance of a `Scanner` inside the `WeatherEngine` class and did `Scanner#close()` by any chance? Please see [this](http://stackoverflow.com/a/13042296/6396174) if you did. – bmdelacruz Apr 09 '17 at 18:57
  • i tried creating another instance but that also didn't work and no i didn't closed scanner – irrelevent Apr 09 '17 at 18:58

2 Answers2

0
if (scan.hasNext())
  String str = scan.next();
Jun
  • 109
  • 5
0

or this, more concise and you should not use exception the way you did.

do {
    choice = scan.nextInt();
    flag = choice < 0 || choice > 6
    if (flag)
        System.out.println("Wrong input \n Enter again");
} while(flag);
Jun
  • 109
  • 5