如何从数据文件中计算每个国家/地区的城市数量?我还希望将值显示为总数的百分比。
import java.util.StringTokenizer;
import java.io.*;
public class city
{
public static void main(String[] args)
{
String[] city = new String[120];
String country = null;
String[] latDegree =new String[120];
String lonDegree =null;
String latMinute =null;
String lonMinute =null;
String latDir = null;
String lonDir = null;
String time = null;
String amORpm = null;
try
{
File myFile = new File("CityLongandLat.txt");
FileReader fr = new FileReader(myFile);
BufferedReader br = new BufferedReader(fr);
String line = null;
int position =0;
int latitude=0;
while( (line = br.readLine()) != null)
{
// System.out.println(line);
StringTokenizer st = new StringTokenizer(line,",");
while(st.hasMoreTokens())
{
city[position] = st.nextToken();
country = st.nextToken();
latDegree[latitude] =st.nextToken();
latMinute =st.nextToken();
latDir = st.nextToken();
lonDegree =st.nextToken();
lonMinute =st.nextToken();
lonDir = st.nextToken();
time = st.nextToken();
amORpm = st.nextToken();
}
if(city.length<8)
{
System.out.print(city[position] + "\t\t");
}
else
{
System.out.print(city[position] + "\t");
}
if(country.length()<16)
{
System.out.print(country +"\t\t");
}
else
{
System.out.print(country);
}
System.out.print(latDegree + "\t");
System.out.print(latMinute + "\t");
System.out.print(latDir + "\t");
System.out.print(lonDegree + "\t");
System.out.print(lonMinute + "\t");
System.out.print(lonDir + "\t");
System.out.print(time + "\t");
System.out.println(amORpm + "\t");
position++;
}
br.close();
}
catch(Exception ex)
{
System.out.println("Error !!!");
}
}
}
答案 0 :(得分:0)
“这是我的程序”
中数据文件的一些输出
Aberdeen Scotland 57 2 [Ljava.lang.String;@33906773 9 N [Ljava.lang.String;@4d77c977 9 W 05:00 p.m. Adelaide Australia 34 138 [Ljava.lang.String;@33906773 55 S [Ljava.lang.String;@4d77c977 36 E 02:30 a.m...
获得该输出的原因是因为您正在尝试打印数组对象latDegree
。
String[] latDegree
...
System.out.print(latDegree + "\t");
此外,你有lattitude = 0;
,但你永远不会增加它,因此它将始终使用索引0作为数组。你需要增加它,就像你position++
一样。
因此对于print语句,打印打印索引lattitude
的值,而不是整个数组
试试这个
System.out.print(latDegree[lattitude] + "\t");
...
lattitude++;
如果由于某种原因你做想要打印数组,那么使用Arrays.toString(array);
或者只是迭代它
答案 1 :(得分:0)
我想到的一个简单方法如下......
创建一个hashMap对象,其中键是一个字符串(国家/地区),值是一个整数(为该国家找到的城市数量),所以它就像
Map countryResultsFoundMap = new HashMap&lt;字符串,整数&GT;();
简而言之,对于你要选择国家的每一行,(我建议首先使用.trim()和.toLowerCase()值)并检查它是否存在于hashMap中,如果没有,添加条目例如countryResultsFoundMap.put(country,0),否则,如果该国家已经存在,则从hashMAp中选择值并将+1添加到其整数值。
最终,您将拥有存储在地图中的所有值,并且您可以访问该数据以进行计算。
希望有所帮助
答案 2 :(得分:0)
我也会先从地图开始,然后按地图将城市分组。
Map<String,<List<String>>
密钥是国家/地区,价值是此国家/地区的城市列表。使用size()方法,您可以按国家/地区执行操作城市和总数百分比。
当您阅读一行时,检查密钥(国家/地区)是否已存在,如果不是,则创建新列表并添加城市,否则仅将城市添加到现有列表。
作为初学者,您可以使用以下代码段。但是,此示例假定已经读取了文件的内容,并将其作为方法的参数提供。
Map<String,List<String>> groupByCountry(List<String> lines){
Map<String,List<String>> group = new HashMap<>();
for (String line : lines) {
String[] tokens = line.split(",");
String city = tokens[0];
String country = tokens[1];
...
if(group.containsKey(country)){
group.get(country).add(city);
}else{
List<String> cities = new ArrayList<>();
cities.add(city);
group.put(country, cities);
}
}
return group;
}