⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scannerexample.java

📁 JAVA SE6 全方位学习 朱仲杰 编著 机械工业出版社出版
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -