2017-04-09 130 views
0

代碼 同時服用的輸入 第一輸入工作正常,但第二者拋出一個異常異常在掃描儀輸入

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 

主要投是追着這些異常方法

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方法

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); 

    } 
+0

您是否在WeatherEngine類中創建了'Scanner'的另一個實例,並且有'Scanner#close()'的機會嗎?如果確實如此,請參閱[this](http://stackoverflow.com/a/13042296/6396174)。 – bmdelacruz

+0

我試圖創建另一個實例,但也沒有工作,沒有我沒有關閉掃描儀 – irrelevent

回答

0
if (scan.hasNext()) 
    String str = scan.next(); 
+0

仍然是相同的例外:/ – irrelevent

+0

我的不好,這實際上不是問題,但你使用掃描儀的方式,每次你打電話nextXXX(),你需要調用hasNext()。我已經更新了我的答案。 – Jun

+0

同樣的異常夥計 – irrelevent

0

或本,更簡潔,你不應該使用異常你的方式。

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