简体   繁体   English

从文本文件读入数组

[英]Reading from a text file into an array

Hi im currently trying to do a hackerearth challenge sum of medians and it involves me reading from a text file and storing the values in an array. 大家好,我目前正在尝试做一个中位数的hackerearth挑战总和,这涉及到我从文本文件读取并将值存储在数组中。 The first value has to be stored in a variable N which i am able to do but the the remaining values have to be stored in an array. 第一个值必须存储在我能够执行的变量N中,但其余值必须存储在数组中。 This is where i become stuck. 这就是我陷入困境的地方。 i have to read each value line by line and then store it in the array . 我必须逐行读取每个值,然后将其存储在数组中。 this is my code that i have been trying to get it working on but i just cant see where im going wrong. 这是我一直在尝试使其工作的代码,但我看不出即时通讯出现了问题。

import java.io.BufferedReader; 
import java.io.InputStreamReader; 

class TestClass { 
 public static void main(String args[] ) throws Exception { 

 // read number of data from system standard input. 
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
 String line = br.readLine(); 
 int N = Integer.parseInt(line); 
 int i = 1;
 int[] myIntArray = new int[N];
  // median sum 
 long SumMedians = 0; 
 int median = 0;


 while (i<N)

     //read one line file and parse as an integer
     //store the value in an array
 { 

     myIntArray [i] = Integer.parseInt(line);

 i = i + 1; // increment i so i is the total numbers read
 }

so as i said i must increment through the text file storing each value on the line in an array. 因此,正如我所说,我必须遍历文本文件,将每个值存储在数组的行中。 Any help would be amazing thanks 任何帮助将是惊人的谢谢

The text file will look like this 文本文件将如下所示

5

10 10

5

1 1

2 2

15 15

one string per line, which i have to pass into an integer. 每行一个字符串,我必须将其传递为整数。 what i will be doing is after i store the value from the line into the array i will be sorting it and finding its medium and then repeat this process until all the values from the text file have been read. 我要做的是将行中的值存储到数组中之后,我将对其进行排序并找到其媒介,然后重复此过程,直到已读取文本文件中的所有值。

The problem which i am trying to do is this one 我想做的问题是这个

http://www.hackerearth.com/problem/algorithm/sum-of-medians-1/ http://www.hackerearth.com/problem/algorithm/sum-of-medians-1/

If you're reading from a text file (and not from standard input which is what you're doing at the moment) then you want something like: 如果您正在从文本文件中读取(而不是从当前正在执行的标准输入中读取),则需要以下内容:

// Warning: this could fail if the filename is invaild.
BufferedReader br = new BufferedReader(new FileReader("inputFileName.txt"));

To then read in each line, you can use the following in the while loop: 然后在while循环中使用以下内容来读取每一行:

// Warning: this will crash the program if the line contains anything other than integers.
myIntArray[i] = Integer.parseInt(br.readLine())
 i = i + 1; // increment i so i is the total numbers read

You should also close the reader at the end: 最后,您还应该关闭阅读器:

try{
  br.close();
} catch (IOException e)
{
  System.out.println("Error, program exit!");
  System.exit(1);
}

The import should be swapped from import java.io.InputStreamReader to: import java.io.FileReader 导入应从import java.io.InputStreamReader交换为: import java.io.FileReader

由于您只读取1行,因此我怀疑它是由冒号/分号或其他字符分隔的一行。尝试查看StringTokenizer和Scanner类

N = the number from parsing a string to a number In the first part of your program it N = 5 N =从解析字符串到数字的数字在程序的第一部分,N = 5

Why are you using while(i<5)? 为什么要使用while(i <5)?

If anything you should be 如果有的话

r = number of lines in text file;

while (i< r)
{

readline;

parseline;

store in array;

}

and then sort 然后排序

Adapting the example they gave you 改编他们给你的例子

import java.io.BufferedReader;
import java.io.InputStreamReader;

class TestClass {
    public static void main(String args[] ) throws Exception {
        /*
         * Read input from stdin and provide input before running
         */

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        int N = Integer.parseInt(line);

        //create storage array
        int[] myIntArray = new int[N];

        //read remainder of file
        for (int i = 0; i < N; i++) {
            String line = br.readLine();
            myIntArray[i] = Integer.parseInt(line);
        }
        // close file
        br.close();


        //Perform median calculations
        int median = 0;
        ...
        System.out.println(median);
}

} }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM