[英]Java find two median in a substring
new to java here and I have a nooby question. Java的新功能,我有一个nooby问题。
I was recently given this txt file: 我最近得到了这个txt文件:
Albuquerque, New Mexico, 5352 feet, 202.3°
新墨西哥州阿尔伯克基,5352英尺,202.3°
Denver, Colorado, 5280 feet, 202.4°科罗拉多州丹佛,5280英尺,202.4°
Minneapolis, Minnesota, 841 feet, 210.5°明尼苏达州明尼阿波利斯,841英尺,210.5°
Birmingham, Alabama, 644 feet, 210.8°阿拉巴马州伯明翰,644英尺,210.8°
Detroit, Michigan, 639 feet, 210.8°密歇根州底特律,639英尺,210.8°
Miami, Florida, 11 feet, 211.9°佛罗里达州迈阿密,11英尺,211.9°
Burj Khalifa, Dubai, 2717 feet, 207.1°哈利法塔,迪拜,2717英尺,207.1°
CN Tower, Toronto, 1815 feet, 208.7°加拿大国家电视塔,多伦多,1815英尺,208.7°
and was ask to find and printout the two median number. 并被要求查找并打印出两个中位数。 Problem is I keep getting error when I try to use
Arrays.sort()
. 问题是,当我尝试使用
Arrays.sort()
时,我总是收到错误消息。
public static void main(String[] args) {
File mf = new File("C:\\file.txt");
ArrayList<String> inData = new ArrayList<String>();
String strline;
try {
FileInputStream fstream = new FileInputStream(mf);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
while ((strline = br.readLine()) != null) {
strline = strline.trim();
if ((strline.length() != 0)) {
inData.add(strline);
}
}
br.close();
} catch (Exception e) {
System.err.println("Error CANNOT FIND FILE!");
}
//Calc median start
double median;
for (int i = 0; i < inData.size(); i++) {
String[] word = inData.get(i).split(", ");
double m = Double.parseDouble(word[3].substring(0, 5));
Arrays.sort(m);
if (m.length % 2 == 0) {
double middleNumOne = m[m.length / 2 - 0.5]
double middleNumTwo = m[m.length / 2 + 0.5]
System.out.println("Median:" + middleNumOne + "and" + middleNumTwo);
}
}
}
this is what i have so far. 这是我到目前为止所拥有的。 Can someone please help me, and tell me what am I missing?
有人可以帮我,告诉我我想念什么吗?
Just to spell it out for you, this is what you need to change: 只是为您说明一下,这就是您需要更改的内容:
double[] median = new double[inData.size()];
for (int i=0; i < inData.size(); i++) {
String[] word = inData.get(i).split(", ");
double m = Double.parseDouble(word[3].substring (0,5));
median[i] = m;
}
Arrays.sort(median);
And then every reference to m
after this point in your code should be changed to median
. 然后,在代码中此点之后对
m
每个引用都应更改为median
。
You can't use Arrays.sort(m);
您不能使用
Arrays.sort(m);
the m
var is a double
i guess you mean Array.sort(word)
and so on in the rest of the code... m
var是一个double
Array.sort(word)
我猜你的意思是Array.sort(word)
等等,在其余的代码中...
If you need to create a double
array to calculate the median, check this question 如果您需要创建一个
double
精度数组来计算中位数, 请检查此问题
List<Double> altitudes = new ArrayList<Double>();
for (int i = 0; i < inData.size(); i++) {
String[] word = inData.get(i).split(", ");
double m = Double.parseDouble(word[3].substring(0, 5));
altitudes.add(m);
}
Double[] array = altitudes.toArray(new Double[] {});
Arrays.sort(array);
for (Double double1 : array) {
System.out.println(" " + double1);
}
if (array.length % 2 == 0) {
double middleNumOne = array[(int) (array.length / 2 - 0.5)];
double middleNumTwo = array[(int) (array.length / 2 + 0.5)];
System.out.println("Median: " + middleNumOne + " and " + middleNumTwo);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.