【问题标题】:Reading text files into array将文本文件读入数组
【发布时间】:2017-07-16 14:10:07
【问题描述】:

我正在尝试将女孩和男孩的名字存储到一个数组中。

除了将文件存储到数组中之外,我得到了大部分代码。

这就是 girls.txt 的样子

艾玛 125125

伊莱娜 415545

金 545454

Boys.txt:

德文 45645

汤姆 4545

克里斯 4879797

我需要帮助将文件中的名称和数字存储到数组 boynames 数组和 girlnames 数组中。我在代码中展示了我需要 cmets 帮助的地方

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Project1Names {
   public static void main(String[] args) {
    Scanner inputStream = null;
    String[][] boynames = new String[1000][2];
    String[][] girlnames = new String[1000][2];
    String line = null;
    boolean isFoundB = false;
    boolean isFoundG = false;
    try {
        inputStream = new Scanner(new FileInputStream("boys.txt"));
    } catch (FileNotFoundException e) {
        System.out.println("Problem opening file boys.txt");
        System.exit(0);
    }

    Scanner inputStreamGirls = null;
    try {
        inputStreamGirls = new Scanner(new FileInputStream("girls.txt"));
    } catch (FileNotFoundException e) {
        System.out.println("Problem opening file girls.txt");
        System.exit(0);
    }
       int count = 0;
        while (count < 1000){
            inputStream =  boynames[count][0]; //Error here
            inputStream =  boynames[count][1]; //here
            count++;
        }

        count = 0;

        while (count < 1000 ){
            inputStreamGirls = girlnames[count][0]; //here
            inputStreamGirls = girlnames[count][1]; //here
            count++;
        }
      Scanner keyboard = new Scanner(System.in);
      System.out.println("Enter the first name that you would like to find the popularity of.\n Be sure to capitalize the first letter of the name.\n");
      String answer = keyboard.next(); 

        count = 0;
        while(count < 1000){
            if (boynames[count][0] == answer){
                System.out.println(boynames[count][0] + " is ranked " + count + " among boys with " +  boynames[count][1] +  " namings");
                isFoundB = true;
            }
            if (girlnames[count][0] == answer){
                System.out.println(girlnames[count][0] +  " is ranked " + count +  " among girls with " + girlnames[count][1] + " namings");
                isFoundG = true;
            }
            count++;
        }

        if(isFoundB == false){
            System.out.println(answer + " is not ranked among the top 1000 boy names.");
        } 
        if(isFoundG == false){
            System.out.println(answer + " is not ranked among the top 1000 girl names.");
        }

    inputStreamGirls.close();
    inputStream.close();
    keyboard.close();
}
}

【问题讨论】:

  • 这里没有问题。

标签: arrays java-io


【解决方案1】:

您需要调用扫描仪方法才能真正从输入文件中读取数据。

scanner.next() 从输入中读取一个字符串标记。

所以不是这部分:

inputStream =  boynames[count][0]; //Error here
inputStream =  boynames[count][1]; //here

你会这样做:

boynames[count][0] = inputStream.next();
boynames[count][1] = inputStream.next();

【讨论】:

  • 谢谢。不知道为什么我没有想到这一点。这就是我将任何内容存储到变量的方式。不知道为什么我的想法不同。对于任何查看此代码以供将来参考的人,必须添加它看起来像这样。 while (count
  • 一个问题是它总是返回 false 并且找不到名称。知道为什么吗?
  • 您使用的是nextLine 而不是next,这使您可以将整行保存在一个变量中而不拆分它,因此boynames[count][0] 将等于Devan 45645,而不仅仅是Devan。您应该在我的答案中使用 next 示例。
  • 我改变了它,但它仍然总是返回 false 它跳过第二个 while 循环并跳转到 if 语句说它没有找到
  • 我认为问题出在boynames[count][0] == answer。您不能使用 == 来比较字符串,您需要使用 boynames[count][0].equals(answer) 代替。请参阅stackoverflow.com/questions/513832/… 了解更多信息。
猜你喜欢
  • 2014-09-24
  • 2017-12-23
  • 1970-01-01
  • 1970-01-01
  • 2015-08-18
  • 2013-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多