将信息读入单独的数组中

时间:2014-05-06 05:31:46

标签: java arrays eclipse

我在将文件中的信息读入单独的数组时遇到了一些困难。文件中的信息示例如下:

14 Barack Obama:United States  
17 David Cameron:United Kingdom  
27 Vladimir Putin:Russian Federation  
19 Angela Merkel:Germany  

虽然我可以将整数分成一个数组,但是我无法为这些国家的名称和数组创建数组。到目前为止,这是我的代码:

import java.util.*;
import java.io.*;

public class leadRank {

public static void main(String[] args) throws FileNotFoundException {
    int size;
    Scanner input = new Scanner(new File("names.txt"));
    size = input.nextInt();
    int[] rank = new int[size];
    for (int i = 0; i < rank.length; i++) {
        rank[i] = input.nextInt();
        input.nextLine();
    }
    String[] name = new String[size]; 
    for (int i = 0; i <name.length; i++) { 
        artist[i] = 

我认为我必须在行中读取字符串并使用indexOf来查找冒号以启动新数组,但我不确定如何执行该操作。

2 个答案:

答案 0 :(得分:0)

我只是试图以我的方式解决你的问题。这只是一段时间。希望这可以帮助你。

import java.util.*;
import java.io.*;

public class leadRank {

public static void main(String[] args) throws FileNotFoundException {
    int size;
    File file = new File("names.txt");
    FileReader fr = new FileReader(file);
    String s;

    LineNumberReader  lnr = new LineNumberReader(new FileReader(file));
    lnr.skip(Long.MAX_VALUE);
    size = lnr.getLineNumber()+1;
    lnr.close();

    int[] rank = new int[size];
    String[] name = new String[size];
    String[] country = new String[size];

    try {
        BufferedReader br = new BufferedReader(fr);
        int i=0;
        while ((s = br.readLine()) != null) {
            String temp = s;
            if(temp.contains(":")){
                String[] splitres = temp.split(":");
                String sub = splitres[0];

                rank[i] = Integer.parseInt(sub.substring(0,sub.indexOf(" "))); // Adding rank to array rank[]
                name[i] = sub.substring(sub.indexOf(" "), sub.length()-1);  // Adding name to array name[]
                country[i] = splitres[1]; // Adding the conutries to array country[]

            }
            i++;
        }
    }
    catch(Exception e) 
    {
        e.printStackTrace();
    }
}  
}

答案 1 :(得分:0)

这样效率更高一些,因为它只通过文件一次。

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

    // create an array list because the size of the array is still not know
    ArrayList<Integer> ranks = new ArrayList<Integer>();
    ArrayList<String> names = new ArrayList<String>();
    ArrayList<String> countries = new ArrayList<String>();

    // read the input file
    Scanner input = new Scanner(new File("names.txt"));

    // read each line
    while (input.hasNext()) {
        String wholeLine = input.nextLine();

        // get the index of the first space
        int spaceIndex = wholeLine.indexOf(" ");

        // parse the rank
        int rank;
        try {
            rank = Integer.parseInt(wholeLine.substring(0, spaceIndex));
        } catch (NumberFormatException e) {
            rank = -1;
        }

        // parse the name & country
        String[] tokens = wholeLine.substring(spaceIndex + 1).split(":");
        String name = tokens[0];
        String country = tokens[1];

        // add to the arrays
        ranks.add(rank);
        names.add(name);
        countries.add(country);
    }

    // get your name and country arrays if needed
    String[] nameArr = names.toArray(new String[]{});
    String[] countryArr = countries.toArray(new String[]{});

    // the rank array has to be created manually
    int[] rankArr = new int[ranks.size()];
    for (int i = 0; i < ranks.size(); i++) {
        rankArr[i] = ranks.get(i).intValue();
    }
}