2015-04-02 139 views
-1

我想写一个程序,它使用ArrayLists和文件输入输出为一个简单的程序。 程序应读取文本文件并将每个字段存储在一个数组列表中,然后执行计算。该文件包含城市名称,人口和国家名称。即(伦敦,7500000,英国) 当我使用命令行参数运行程序时,它应该打印声明,说明有多少城市拥有更多的人口,等于命令行参数。数据结构文件输入输出

我希望有人能引导我走向正确的方向。谢谢。

+0

创建一个主要方法,然后创建'Scanner' /'BufferedReader'对象读取一个文件,然后用'分裂它,'。继续将它们存储在你的'ArrayList'中,最后通过arraylist循环并打印出来:) – Parth 2015-04-02 11:27:22

回答

0

这是一个很好的锻炼的学校:-)

假设你的输入文件C:\ TEMP \ cities.txt包含:

London,83000000,UK 
Paris,22000000,France 
Madrid,32000000,Spain 

下面的类Test.java:

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Iterator; 

public class Test { 

    private static ArrayList<City> loadDataFromFile() throws IOException { 

    ArrayList<City> list = new ArrayList<City>(); 
    BufferedReader br = null; 
    String sCurrentLine; 
    br = new BufferedReader(new FileReader("C:\\temp\\cities.txt")); 

    while ((sCurrentLine = br.readLine()) != null) { 
     String[] splittedLine = sCurrentLine.split(","); 
     City city = new City(splittedLine[0], Integer.parseInt(splittedLine[1]), splittedLine[2]); 
     list.add(city); 
    } 

    br.close(); 
    return list; 
    } 

    public static void main(String[] args) throws IOException { 

    ArrayList<City> list = loadDataFromFile(); 
    int input = Integer.parseInt(args[0]); 
    Iterator<City> it = list.iterator(); 

    while (it.hasNext()) { 
     City currentCity = it.next(); 
     if (currentCity.getPopulation() > input) { 
     System.out.println("Cities with population greater than " + input + " : " + currentCity.getName() + "," + currentCity.getCountry()); 
     } else if (currentCity.getPopulation() == input) { 
     System.out.println("City with population equals to " + input + " : " + currentCity.getName() + "," + currentCity.getCountry()); 
     } else { 
     System.out.println("Cities with population lower than " + input + " : " + currentCity.getName() + "," + currentCity.getCountry()); 
     } 
    } 

    } 
} 

和一个简单的POJO:

public class City { 

    private String name; 

    private int population; 

    private String country; 

    public City(String name, int population, String country) { 
    this.name = name; 
    this.population = population; 
    this.country = country; 
    } 

    public String getName() { 
    return name; 
    } 

    public void setName(String name) { 
    this.name = name; 
    } 

    public int getPopulation() { 
    return population; 
    } 

    public void setPopulation(int population) { 
    this.population = population; 
    } 

    public String getCountry() { 
    return country; 
    } 

    public void setCountry(String country) { 
    this.country = country; 
    } 

} 

会给你:

java Test 3200000 

Cities with population greater than 32000000 : London,UK 
Cities with population lower than 32000000 : Paris,France 
City with population equals to 32000000 : Madrid,Spain 
+0

这不是一个正确的方向...... – Stefan 2015-04-02 12:38:43

+0

谢谢:)我会尝试了解现在的代码。 – jeeps95 2015-04-02 13:25:00

0

没有给人一种“准备运行”的答案,我认为它应该是这个样子(伪代码)

public static void main(String[] args) { 
    convert first argument to integer 
    set up a reader for your file 
    while (another line is found) { 
     split the line by comma 
     convert second item in array to an integer 
     if integer >= integer passed in args 
       print current line 
    } 
    } 

请记住,你所要做的异常处理(的IOExceptions,NumberFormatException异常等)。我故意不包括保存到数组列表中,因为那意味着你必须循环两次(一次覆盖文件,一次覆盖列表),这是没有必要的。