如何读取多行字符串的txt文件

发布于 2024-12-09 20:32:36 字数 1209 浏览 0 评论 0原文

我正在尝试使用来自 txt 文件的名称列表创建一个字符串数组。 例如:如果我有 string[] 名称 = {txtfile 中的所有名称(每行一个名称)} 我想将“名称”传递到一个方法中,该方法接受像“名称”这样的数组(我上面制作的那个)。然后,该方法将通过另一个 for 循环运行名称并创建一个链接列表。我真的对此感到困惑,并尝试了很多方法,但似乎没有任何效果。现在它会正确打印出第一个名字,但之后的每个名字都显示为空。所以我打印出了大约 70 个空值。

         public static void main(String[] args) {


        //String[] names = {"Billy Joe", "Alan Bowe", "Sally Mae", "Joe Blow", "Tasha Blue", "Malcom Floyd"}; // Trying to print theses names..Possibly in alphabetical order

        BigNode x = new BigNode(); 

        Scanner in = new Scanner(System.in);
        System.out.println("Enter File Name: ");
        String Finame = in.nextLine();
        System.out.println("You Entered " + Finame);

        try {File file = new File(Finame);
        BufferedReader readers = new BufferedReader(new FileReader(file));
       // String nameLine = ;
        String[] name;
        name = new String[73];
        String[] nameTO;
        String nameLine;
      //  while ((nameLine = readers.readLine()) != null) {
        for (int i = 0; i < name.length; i++){


        name[i] = readers.readLine();


        x.populateNodes(name);

        } //}
        } catch(IOException e) {

        }

I'm trying to make an array of strings using a list of names coming from a txt file.
So for example: If I have string[] names = {all the names from the txtfile(one name perline)}
I want to pass "names" into a method that takes in a array like "names"(the one I made above). The method will then run the names through another for loop and create a linked list. I'm really confusing myself on this and tried number of things but nothing seems to work. Right now It'll print out the first name correctly but every name after that just says null. So I have about 70 nulls being printed out.

         public static void main(String[] args) {


        //String[] names = {"Billy Joe", "Alan Bowe", "Sally Mae", "Joe Blow", "Tasha Blue", "Malcom Floyd"}; // Trying to print theses names..Possibly in alphabetical order

        BigNode x = new BigNode(); 

        Scanner in = new Scanner(System.in);
        System.out.println("Enter File Name: ");
        String Finame = in.nextLine();
        System.out.println("You Entered " + Finame);

        try {File file = new File(Finame);
        BufferedReader readers = new BufferedReader(new FileReader(file));
       // String nameLine = ;
        String[] name;
        name = new String[73];
        String[] nameTO;
        String nameLine;
      //  while ((nameLine = readers.readLine()) != null) {
        for (int i = 0; i < name.length; i++){


        name[i] = readers.readLine();


        x.populateNodes(name);

        } //}
        } catch(IOException e) {

        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

踏月而来 2024-12-16 20:32:36

为什么 x.populateNodes(name) 在循环内?填充数组后您不会填充它吗?

由于我不知道 BigNode 是什么,我认为它应该是以下之一
循环内的 x.populateNodes(name[i]) 或循环外的 x.populateNodes(name)

Why is x.populateNodes(name) inside the loop? Wouldn't you be populating it after filling your array?

Since I've no idea what BigNode is, I assume it should be one of the following
x.populateNodes(name[i]) inside the loop or x.populateNodes(name) outside the loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文