此代码查看了几个文件,以查找您键入的任何名称的流行度和含义。我几乎没有启动它的图形部分,这就是drawGraph方法看起来很奇怪的原因。 我想知道如何获得它,所以用户只需输入一次名称。我知道有String originalName = console.next();在nameToLowerCase的第一行是问题,我只是不知道在哪里放它。我已经尝试将它放在介绍但是没有用,因为那时我必须使介绍等于名称,每当我想要名字时,整个介绍会再次重复。 谢谢,这是我的代码和输出: 输出:
This program allows you to search through the
data from the Social Security Administration
to see how popular a particular name has been
since 1890
Name: aaron
Aaron f 0 0 0 0 0 0 0 0 0 883 0 0 0
aaron
aaron
aaron
AARON m English, Biblical From the Hebrew name ??????? ('Aharon) which is most likely of unknown Egyptian origin.
aaron
aaron
aaron
aaron
aaron
aaron
aaron
aaron
aaron
aaron
aaron
(它使用户一次又一次地输入名称)。 代码:
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;
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();
if(STARTINGYEAR == 1890){
findingStatistics(console,nameFile);
drawGraph(g);
}
else{
findingStatistics(console, nameFile2);
drawGraph(g);
}
findingStatistics(console,meaningsFile);
}
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 1890" );
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){
String nameFinal = nameToLowerCase(console);
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 drawGraph(Graphics g){
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0,0,780,30);
g.fillRect(0,530,780,30);
}
}
答案 0 :(得分:0)
如果我理解正确,您希望用户只需提供一次搜索字词。
一种解决方案是轻微的重构。只需在类级别声明nameFinal
变量,并在调用intro()
后读取该变量的输入。
这看起来像这样:
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;
// CLASS LEVEL VAR TO HOLD THE SEARCH TERM
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();
// READ THE SEARCH HERE ONLY ONCE
nameFinal = nameToLowerCase(console);
if(STARTINGYEAR == 1890){
findingStatistics(console,nameFile);
drawGraph(g);
}
else{
findingStatistics(console, nameFile2);
drawGraph(g);
}
findingStatistics(console,meaningsFile);
}
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 1890" );
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){
// NOW THE nameFinal VAR ALREADY HAS A VALUE
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 drawGraph(Graphics g){
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0,0,780,30);
g.fillRect(0,530,780,30);
}
}