简体   繁体   English

我在将文本读入数组时遇到麻烦,我需要在此代码中进行哪些更改?

[英]I am having troubles reading text into an array, what do I need to change in this code?

This is my current text file that I am trying to read from: 这是我尝试从中读取的当前文本文件:

Buffalo
Montreal
Boston
Ottawa
Toronto

Whenever I run this code, my 5 elements of my array print as "null", so somehow I am not storing the information into the array properly. 每当我运行此代码时,我数组中的5个元素都会显示为“ null”,因此我不知何故没有将信息正确存储到数组中。 Here is the code I have so far! 这是我到目前为止的代码! (I am using the Ready To Program IDE) (我正在使用Ready To Program IDE)

// The "Hockey" class.
import java.awt.*;
import hsa.Console;
import java.io.*;

public class Hockey
{
    static Console c;           // The output console

    public static void main (String[] args) throws IOException
    {
        c = new Console ();

        //setting up file reading
        FileReader fr = new FileReader ("cities.txt");
        BufferedReader br = new BufferedReader (fr);

        //initialising array
        String cities[] = new String [5];

        //loop to read in the 5 entries
        for (int i = 0 ; i > cities.length ; i++)
        {
            cities [i] = br.readLine ();
        }

        //loop to print all elements of "cities" to the console
        for (int i = 0 ; i < cities.length ; i++)
        {
            c.println (cities [i]);
        }
            // Place your program here.  'c' is the output console
    } // main method

} // Hockey class

Thanks for all of your help! 感谢您所有的帮助!

Your for loop has an incorrect condition. 您的for循环条件不正确。

//loop to read in the 5 entries
for (int i = 0 ; i > cities.length ; i++) //reads as (i greater than cities.length)
{                ^^^^^^^^^^^^^^^^^
    cities [i] = br.readLine ();
}

The above should be: i < cities.length; 上面应该是: i < cities.length; (i less than cities.length) (我小于city.length)

The loop isn't running, which explains why your array isn't populating. 循环未运行,这说明了为什么未填充阵列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在编写测试驱动程序时遇到了麻烦 - I am having troubles writing a test driver 如果我有1个服务器和多个客户端,是否需要SocketServerChannel? - Do I need SocketServerChannel if I am having 1 server and multiple clients? 我在安装Movilizer eclipse插件时遇到麻烦 - I am having troubles installing the Movilizer eclipse plugin 我在将View作为ToS的弹出窗口显示时遇到麻烦 - I am having troubles displaying a View as a popup window for a ToS 我需要在第二个“if”语句中更改什么? - What do I need to change in the second “if” statement? 我在使用FileReader将txt文件写入数组(Java)时遇到问题,我做错了什么? - I am having trouble using FileReader to write a txt file to an array (Java), what am I doing wrong? 如果我只从集合中读取,我是否需要担心线程安全? - Do I need to worry about thread safety if I am only reading from a collection? 我在代码中需要什么来检查数组中的元素是否与之​​前的相同 - What do I need here in my code to check if an element in an array is the same with the previous 如果需要更改库功能该怎么办 - What do i do if need to change a libraries functionality 当我在 Java 中读取 API 时,我在字符串中使用 ÃŽ 代替 Î。 我该怎么办? - When I am reading a API in java I get ÃŽ in place of Î in a string. What should I do?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM