Java 如何从数据文件中计算每个国家的城市数?
如何从数据文件中计算每个国家的城市数?我还想显示值占总数的百分比Java 如何从数据文件中计算每个国家的城市数?,java,arrays,bufferedreader,filereader,Java,Arrays,Bufferedreader,Filereader,如何从数据文件中计算每个国家的城市数?我还想显示值占总数的百分比 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[] latDe
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 !!!");
}
}
}
import java.util.StringTokenizer;
导入java.io.*;
公营城市
{
公共静态void main(字符串[]args)
{
字符串[]城市=新字符串[120];
字符串country=null;
字符串[]latDegree=新字符串[120];
字符串lonDegree=null;
字符串latMinute=null;
字符串lonMinute=null;
字符串latDir=null;
字符串lonDir=null;
字符串时间=空;
字符串amORpm=null;
尝试
{
File myFile=新文件(“citylongandat.txt”);
FileReader fr=新的FileReader(myFile);
BufferedReader br=新的BufferedReader(fr);
字符串行=null;
int位置=0;
内纬度=0;
而((line=br.readLine())!=null)
{
//系统输出打印项次(行);
StringTokenizer st=新的StringTokenizer(行,“,”);
而(st.hasMoreTokens())
{
城市[位置]=圣奈克特肯();
国家=st.nextToken();
纬度[纬度]=st.nextToken();
latMinute=圣奈克特肯();
latDir=圣奈克特肯();
lonDegree=st.nextToken();
lonMinute=st.nextToken();
lonDir=st.nextToken();
时间=st.nextToken();
阿莫普姆=圣奈克特肯();
}
如果(城市长度)
“以下是我的程序中数据文件的一些输出”
Aberdeen Scotland 57 2[Ljava.lang.String;@33906773 9 N[Ljava.lang.String;@4d77c977 9 W 05:00澳大利亚阿德莱德34 138[Ljava.lang.String;@33906773 55 S[Ljava.lang.String;@4d77c977 36 E凌晨02:30…
获取该输出的原因是,您正试图打印数组对象latDegree
String[] latDegree
...
System.out.print(latDegree + "\t");
另外,您有latitude=0;
但您从未增加它,因此它将始终使用数组的索引0。您需要增加它,就像您增加position++
一样
因此,对于print语句,打印索引latitude
处的值,而不是整个数组
试试这个
System.out.print(latDegree[lattitude] + "\t");
...
lattitude++;
如果出于某种原因,您确实想打印数组,那么请使用Arrays.toString(array);
或只是在其中进行迭代
创建一个hashMap对象,其中键是一个字符串(国家),值是一个整数(为该国家找到的城市数),因此
Map countryResultsFoundMap=新HashMap()
简而言之,对于每一行,您都会选择国家(我建议您先选择.trim()和.toLowerCase()值),并检查它是否存在于hashMap中,如果不存在,则添加类似countryResultsFoundMap.put(country,0)的条目,否则,如果国家/地区已存在,则从hashMAp中选取值并将+1添加到其整数值
最终,您将拥有地图中存储的所有值,并且您可以访问这些数据进行计算
希望这对我有所帮助。我也会从一张地图开始,用一张地图按国家对城市进行分组
Map<String,<List<String>>
地图您能给我们展示一个在文本文件中如何布局数据的示例吗?@MartijnCourteaux是的。已实现。城市与国家
:)我需要从数据文件中计算与每个国家相匹配的城市数您知道如何做吗@sᴜʀᴇsʜᴀᴛᴛᴀint m=city[0].length();int position=0;for(int k=0;km){m=city[k].length();position=k;}@PakkuDon@MartijnCourteaux@SURESHATTA}@user3135233一个简单的建议在finally{}中关闭您的BufferedReader br
block。不确定此方法使用什么代码。您能给我提供更多信息吗?@spindizyi添加了一个简短示例以获取更多信息。希望对您有所帮助。
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;
}