0

I marked where I got the exception and I don't know why it's happening. This code finds the popularity and meaning of a name that the user types in . It also makes a graph(but it doesn't work) The files are too long to put on here but on line examples are: names.txt: Brenda f 0 0 0 0 917 42 14 13 40 115 123 172 426 (popularity), meanings.txt: BRITTANY f English From the name of the region in the northwest of France, called in French Bretagne.(meaning) 1. Why am I getting this exception? 2. How do I fix the exception? 3. Why isn't my graph drawing the bars (in method changingGraph)

Example Console Output:

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:

   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)

my code:

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);
         }
      }
   }
}
Marina Claire
  • 103
  • 1
  • 1
  • 11
  • 1
    `Graphics g = panel.getGraphics();` is not how custom painting is done in Swing. See [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) and [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) for more details – MadProgrammer Jan 04 '15 at 20:49
  • @MarinaClaire `stop == false`, `gender == true`??? Who taught you doing such matches on boolean types? – Jagger Jan 04 '15 at 20:51
  • @MarinaClaire It is hard to execute this code without the resources `names.txt`, `meanings.txt` and `names2.txt`. – Jagger Jan 04 '15 at 20:53
  • the files are very long so here is and example from each: names.txt:Aliana f 0 0 0 0 0 0 0 0 0 0 0 0 80. meanings.txt: ALTAIR m Astronomy Means "the flyer" in Arabic. – Marina Claire Jan 04 '15 at 20:58
  • I do stop==false and gender ==true to stop the while loop once it has found what it needs. – Marina Claire Jan 04 '15 at 21:00
  • @MarinaClaire Swing is the GUI framework you are using, all be it poorly – MadProgrammer Jan 04 '15 at 21:10
  • Put a `System.out.println(popularityData);` before the `ranking = popularityData.nextInt(); //EXCEPTION HERE` line, I suspect your expectations of your data don't meet reality – MadProgrammer Jan 04 '15 at 21:18
  • Can you provide example data for `names.txt`, `meanings.txt` and `names2.txt`? – MadProgrammer Jan 04 '15 at 21:21
  • With that, I get this exception "Exception in thread "main" java.util.InputMismatchException" – Marina Claire Jan 04 '15 at 21:25
  • I strongly recommend stepping through this with a debugger. I'm sure you'd find the problem in a few seconds if you did so. – Dawood ibn Kareem Jan 04 '15 at 21:29
  • I printed out my popularity data to see what I was scanning through and instead of the numbers I got this: "java.util.Scanner[delimiters=\p{javaWhitespace}+][position=5][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E] " – Marina Claire Jan 04 '15 at 21:35

0 Answers0