使用扫描仪控制台分配变量

时间:2015-01-04 02:14:28

标签: java file variables console expression

此代码查看数百个名称,并查找它们的受欢迎程度和含义。我最终将制作一个显示受欢迎程度的图表,但我不知道如何将流行度作为一个变量进行挑选,这样我就可以在我的changeGraph方法中扫描它并提取特定的数字。

这是控制台输出:

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

名称后面的内容是文件的组成部分。数以百计的名字。

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

public class BabyNames{ //ADD COMMENTS
   public static final int STARTINGYEAR = 1890;
   public static final int WIDTH = 60;
   public static final int HEIGHT = 30; 
   private static String nameFinal;
   public static void main(String[] args) throws FileNotFoundException{
      Scanner console = new Scanner(System.in);
      DrawingPanel panel = new DrawingPanel(780,560);
      Graphics g = panel.getGraphics();
      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);
      nameFinal = nameToLowerCase(console);
      String meanings;
      if(STARTINGYEAR == 1890){
         findingStatistics(console,nameFile); 
      }
      else{
         findingStatistics(console, nameFile2);
      }
      meanings = findingStatistics(console, meaningsFile);
      changingGraph(meanings,g);
   }

   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: ");
   }

   public static String nameToLowerCase(Scanner console){   
      String 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){
      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{
         }
      }
      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,560-HEIGHT,780,HEIGHT);
      g.setColor(Color.BLACK);
      g.drawLine(0,HEIGHT,780,HEIGHT);
      g.drawLine(0,560-HEIGHT,780,560-HEIGHT);
   }

   public static void changingGraph(String meanings, Graphics g){  
     // String meanings = findingStatistics(console,meaningsFile);
      //String popularity = findingStatistics(console,nameFile);
      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),552); //draws decade numbers
      }      
   }
}

另外:如何保存用户输入的原始名称???


0 个答案:

没有答案