为什么我在线程" main"?中得到输入不匹配异常?

时间:2015-01-04 20:47:18

标签: java file exception graphics inputmismatchexception

我标记了我得到例外的地方,但我不知道它为什么会发生。此代码查找用户键入的名称的流行度和含义。它也制作了一个图表(但它没有工作) 文件太长了,不能放在这里,但在线示例是:names.txt: Brenda f 0 0 0 0 917 42 14 13 40 115 123 172 426(人气),meanings.txt: BRITTANY f English来自法国西北部地区的名称,名称为法国布列塔尼。(意思) 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);
         }
      }
   }
}

0 个答案:

没有答案