0

例外が発生した場所をマークしましたが、なぜそれが起こっているのかわかりません。このコードは、ユーザーが入力した名前の人気と意味を調べます。グラフも作成します (ただし、機能しません) ファイルは長すぎてここに置くことはできませんが、オンラインの例は次のとおりです。 , means.txt: BRITTANY f 英語 フランス語でブルターニュと呼ばれる、フランス北西部の地域の名前から。 (意味) 1. なぜこの例外が発生するのですか? 2. 例外を修正するにはどうすればよいですか? 3. グラフがバーを描画しないのはなぜですか (メソッド changingGraph で)

コンソール出力の例:

Name: aaron
Aaron f 0 0 0 0 0 0 0 0 0 883 0 0 0
AARON m English, Biblical From the Hebrew name ??????? ('Aharon) which is most likely of unknown Egyptian origin.

例外:

   Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at BabyNames.changingGraph(BabyNames.java:131)
    at BabyNames.main(BabyNames.java:32)

私のコード:

import java.util.*;
import java.io.*;
import java.awt.*;

public class BabyNames{ 
   public static final int STARTINGYEAR = 1890;
   public static final int WIDTH = 60;
   public static final int HEIGHT = 30; 
   private static String nameFinal;
   private static String originalName;
   public static int HEIGHTOFPANEL = 500 + (HEIGHT * 2);
   public static void main(String[] args) throws FileNotFoundException{
      Scanner console = new Scanner(System.in);
      DrawingPanel panel = new DrawingPanel(780,HEIGHTOFPANEL);//DOES PANEL SIZE CHANGE????!?!?!?!??!?!?!?
      Graphics g = panel.getGraphics();              //does there always need to be 500 free in the middle???
      Scanner nameFile = new Scanner(new File("names.txt"));
      Scanner meaningsFile = new Scanner(new File("meanings.txt"));
      Scanner nameFile2 = new Scanner(new File("names2.txt"));
      intro();
      fixedGraph(g);
      String nameFinal;
      nameFinal = nameToLowerCase(console, originalName);//changes to correct capitalization
      String meanings = "";
      String popularity = "";
      if(STARTINGYEAR == 1890){
         popularity = findingStatistics(console,nameFile, nameFinal); 
      } 
      else{
         popularity = findingStatistics(console, nameFile2, nameFinal);
      }
      meanings = findingStatistics(console, meaningsFile, nameFinal);
      changingGraph(meanings,g,popularity); //EXCEPTION HERE
   }

   //prints introduction to what the program does
   public static void intro(){ 
      System.out.println("This program allows you to search through the");
      System.out.println("data from the Social Security Administration");
      System.out.println("to see how popular a particular name has been");
      System.out.println("since" + STARTINGYEAR );
      System.out.println();
      System.out.print("Name: ");
   }

   //Converts what the user types in so the first letter is 
   //capitalized and the rest is lower case
   public static String nameToLowerCase(Scanner console, String originalName){  
      originalName = console.next();
      String name = "" ;
      int lengthOfName = originalName.length();
      String beginingOfName = originalName.substring(0,1).toUpperCase();
      String endOfName = originalName.substring(1,lengthOfName).toLowerCase();
      name = beginingOfName + endOfName;
      return name;
   }
   public static String findingStatistics(Scanner console, Scanner data, String nameFinal){
      boolean goesThroughOnce = false; //
      String statistics = "";
      String currWord = "";
      String currLine = "";
      while (data.hasNext() && goesThroughOnce == false){ 
         currLine = data.nextLine();
         Scanner lineBeingRead = new Scanner(currLine); //make other scanners?? for each file
         currWord = lineBeingRead.next(); //

         if (currWord.equals(nameFinal) || currWord.equals(nameFinal.toUpperCase())){   //         
            statistics = currLine;
            goesThroughOnce = true;
            System.out.println(statistics);
         }
         else{
         }
      }
      if(goesThroughOnce == false){
         System.out.print(originalName + " not found"); //ASK ABOUT THE EXCEPTION
      }
      return statistics;

   }

   public static void fixedGraph(Graphics g){ //Draws fixed things such as gray blocks and black lines
      g.setColor(Color.LIGHT_GRAY);
      g.fillRect(0,0,780,HEIGHT);
      g.fillRect(0,HEIGHTOFPANEL-HEIGHT,780,HEIGHT);
      g.setColor(Color.BLACK);
      g.drawLine(0,HEIGHT,780,HEIGHT);
      g.drawLine(0,HEIGHTOFPANEL-HEIGHT,780,HEIGHTOFPANEL-HEIGHT);
   }

   public static void changingGraph(String meanings, Graphics g, String popularity){ 
      g.drawString("" + meanings,0,16); //draws meaning text
      int startingYear = STARTINGYEAR;
      int amountOfDecades = 0;
      if(startingYear == 1890){
         amountOfDecades = 13;
      }
      else{
         amountOfDecades = 8;
      }
     // g.drawString("" + startingYear,0,552); //fencepost 
      for(int i=0; i<=amountOfDecades;i++){ 
         int year = startingYear + (10 * i);
         g.drawString("" + year,(WIDTH*i),HEIGHTOFPANEL-8); //draws decade numbers

      }      
      Scanner popularityData = new Scanner(popularity);
      Scanner meaningsData =  new Scanner(meanings);
      String currChar = popularityData.next();
      boolean gender = false; //if it is a boys name
      boolean stop = false;


      while(meaningsData.hasNext() && stop == false){//determines which color the bars will be    
         if(currChar.equals("f")){
            gender = true;
         }
         if(gender == true){
            g.setColor(Color.PINK);        
            stop = true;
         }                              
         else{
            currChar = meaningsData.next();
         }
      }   
      if(stop ==false){
         g.setColor(Color.BLUE);
      }
      int ranking;
      while(popularityData.hasNext() && stop == false){ //EXCEPTION HERE
         ranking = popularityData.nextInt();
         for(int i=0; i<=amountOfDecades;i++){
            g.fillRect(WIDTH*i,(ranking/2)+30,WIDTH/2,(560-HEIGHT*2)-((ranking/2)+30) );
            g.drawString("" + ranking, WIDTH*i, (ranking/2)+30);
         }
      }
   }
}
4

0 に答える 0