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. 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. 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.
嗨我目前正在尝试做一个hackerearth挑战中位数的总和,它涉及我从文本文件中读取并将值存储在数组中。第一个值必须存储在我能够做的变量N中,但其余的值必须存储在一个数组中。这就是我陷入困境的地方。我必须逐行读取每个值,然后将其存储在数组中。这是我的代码,我一直试图让它工作,但我只是不知道我哪里出错了。
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/
4 个解决方案
#1
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
#2
0
Since you are only reading 1 line therefore I suspect it to be a single line delimited by colon/semicolon or other character.. try looking into StringTokenizer and Scanner classes
因为你只读了1行所以我怀疑它是由冒号/分号或其他字符分隔的单行。试着查看StringTokenizer和Scanner类
#3
0
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
然后排序
#4
0
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);
}
}
}
#1
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
#2
0
Since you are only reading 1 line therefore I suspect it to be a single line delimited by colon/semicolon or other character.. try looking into StringTokenizer and Scanner classes
因为你只读了1行所以我怀疑它是由冒号/分号或其他字符分隔的单行。试着查看StringTokenizer和Scanner类
#3
0
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
然后排序
#4
0
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);
}
}
}