scannerexample.java
来自「JAVA SE6 全方位学习 朱仲杰 编著 机械工业出版社出版」· Java 代码 · 共 53 行
JAVA
53 行
package java2.G;
import java.io.*;
import java.util.*;
import static java.lang.System.*;
public class ScannerExample
{
public static void main(String[] args) throws IOException
{
//Variables declare
int totalAge = 0;
double totalHeight = 0.0;
double totalWeight = 0.0;
int totalPerson = 0;
Map<String, Integer> cities = new HashMap<String, Integer>();
//Read data from file
Scanner sc = new Scanner(new FileReader("d:\\java2\\G\\info.txt"));
sc = sc.useDelimiter(",|\n");
//Parsing data
while(sc.hasNextInt())
{
totalPerson++;
int id = sc.nextInt();
String name = sc.next();
totalAge += sc.nextInt();
totalHeight += sc.nextDouble();
totalWeight += sc.nextDouble();
String tel = sc.next();
String city = sc.next();
if (cities.containsKey(city))
{
int count = cities.get(city)+1;
cities.put(city, count);
}
else
cities.put(city, 1);
}
//Output result
out.printf("Average age is %d.%n", totalAge/totalPerson);
out.printf("Average height is %.2f.%n", totalHeight/totalPerson);
out.printf("Average weight is %.2f.%n", totalWeight/totalPerson);
Set<String> keys = cities.keySet();
for(String city : keys)
out.printf("%d persons in %S.%n", cities.get(city), city);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?