将文本文件读入数组

MoneyMonster

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

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

这就是girls.txt的样子

艾玛 125125

伊莱娜 415545

金 545454

男孩.txt:

德文 45645

汤姆 4545

克里斯 4879797

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

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();
}
}
赫沙姆·阿提亚

您将需要调用扫描仪方法来实际读取输入文件。

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

所以而不是这部分:

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

你会这样做:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将数据从文本文件读入VBA数组

来自分类Dev

将文本文件读入char数组

来自分类Dev

将多节文本文件读入多个数组

来自分类Dev

如何将文本文件读入对象数组

来自分类Dev

将本地文本文件读入JavaScript数组

来自分类Dev

Python将文本文件读入单词数组

来自分类Dev

将文本文件读入Typescript中的对象数组

来自分类Dev

将整数从文本文件读入数组的最佳方法

来自分类Dev

将文本文件读入2D数组

来自分类Dev

将数字从文本文件读入数组

来自分类Dev

将文本文件读入数组并进行修改

来自分类Dev

将文本文件读入2D数组

来自分类Dev

Java sentinel - 将文本文件读入数组

来自分类Dev

将文本文件读入字符串数组

来自分类Dev

将文本文件中的整数读入数组列表

来自分类Dev

将文本文件读入arraylist

来自分类Dev

将文本文件读入python

来自分类Dev

将文本文件读入结构

来自分类Dev

将文本文件读入char数组,然后读入char链表

来自分类Dev

将文本文件中的数字数组读入C语言的数组中

来自分类Dev

将文本文件中的数字数组读入 numpy 数组 - python

来自分类Dev

如何将这个文本文件读入以下类数组?

来自分类Dev

Swift 5将文本文件读入Int / Doubles的二维数组

来自分类Dev

尝试将文本文件读入C ++中的结构数组

来自分类Dev

Java-将文本文件中的单词读入数组时出现撇号错误

来自分类Dev

VB正在将文本文件读入2D数组吗?

来自分类Dev

将格式化的文本文件读入数组列表JAVA

来自分类Dev

C-如何将文本文件中的单词读入字符串数组

来自分类Dev

Javascript:将文本文件读入数组,按正则表达式拆分