BabyNames如何只返回一行

时间:2014-02-27 01:11:30

标签: java line return

我有一项任务,要求使用扫描仪向用户提供婴儿姓名。然后它通过文件names.txt和meanings.txt读取并返回从1890年到2010年的每个十年的名称的流行度,然后它打印出意义。有些名称有多种含义,有些名称用于两种性别。赋值状态仅打印找到名称的第一行。我只是在返回找到名称的第一行时遇到了麻烦。请帮我!

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

public class BabyNames4 {
   public static void main(String[] args) throws FileNotFoundException {

      printIntro();

      Scanner console = new Scanner(System.in);
      System.out.print("Name: ");
      String searchWord = console.next();

      Scanner fileScan = new Scanner(new File("names.txt"));
      String dataLine = find(searchWord, fileScan);
      if (dataLine.length() > 0) {

         while (dataLine.length() > 0) {
            printName(dataLine);
            dataLine = find(searchWord, fileScan);
         }
      }

      Scanner fileScan2 = new Scanner(new File("meanings.txt"));
      String dataLine2 = find(searchWord, fileScan2);
      if (dataLine2.length() > 0) {

         while (dataLine2.length() > 0) {
            printMeaning(dataLine2);
            dataLine2 = find(searchWord, fileScan2);
         }
      }
   }

   public static void printIntro() {   
      System.out.println("This program allows you to search through the");
         System.out.println("dada 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();         
   }

   public static String find(String searchWord, Scanner fileScan) {
      while (fileScan.hasNextLine()) {
         String dataLine = fileScan.nextLine();
         String dataLineLC = dataLine.toLowerCase();

         if (dataLineLC.contains(searchWord.toLowerCase())) {
            return dataLine;
         //} else {              runs a continuous loop                       
            //System.out.println(search" not found.");   
         }
      }
      return "";    
   }

   public static void printName(String dataLine) {
      Scanner lineScan = new Scanner(dataLine);

      String name = lineScan.next();                        
      String gender = lineScan.next();
      String rank = "";
      while (lineScan.hasNext()) {
         rank += lineScan.next() + " "; 
      }                                        
      System.out.println(name + (" ") + gender + (" ") + rank);
   }

   public static void printMeaning(String dataLine2) {
      Scanner lineScan2 = new Scanner(dataLine2);

      String name2 = lineScan2.next();                        
      String gender2 = lineScan2.next();
      String language = lineScan2.next();
      String meaning = "";
      while (lineScan2.hasNext()) {
         meaning += lineScan2.next() + " "; 
      }
      System.out.println(name2 + (" ") + gender2 + (" ") + language + (" ") + meaning);      
   } 
}

2 个答案:

答案 0 :(得分:2)

看起来sushain用他的评论打了它 循环:

while (dataLine2.length() > 0) {
        printMeaning(dataLine2);
        dataLine2 = find(searchWord, fileScan2);
    }

可以更改为:

while (dataLine2.length() > 0) {
    printMeaning(dataLine2);
    break;
}

这样您就找不到第二个定义而不打印它。

答案 1 :(得分:1)

在这个循环中,你不需要找到下一行,对吗?

if (dataLine.length() > 0) {

     while (dataLine.length() > 0) {
        printName(dataLine);
        dataLine = find(searchWord, fileScan);  // remove this line
     }
}

如果您删除了对dataLine的下一个查找并删除了搜索文件的两个实例中的while块,则不需要中断,并且您只会打印一个实例。

这样做:

String dataLine = find(searchWord, fileScan);
if (dataLine.length() > 0) {
    printName(dataLine);
}