【问题标题】:Reading in information into separate arrays将信息读入单独的数组
【发布时间】:2014-05-06 05:31:45
【问题描述】:

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

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 查找冒号才能开始一个新数组,但我不确定如何执行它。

【问题讨论】:

  • 如果我理解正确你的问题,你需要逐行读取文件,在空格处拆分并提取整数,然后在冒号上拆分并制作相应的数组?

标签: java arrays eclipse


【解决方案1】:

我只是试图以我的方式解决您的问题。这只是一段时间的过去。希望对您有所帮助。

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();
    }
}  
}

【讨论】:

    【解决方案2】:

    这样效率更高一点,因为它只遍历文件一次。

    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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-09-13
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2022-09-30
      • 1970-01-01
      • 2014-08-22
      • 2016-06-16
      相关资源
      最近更新 更多