本文介绍了如何读取由空格分隔int和字符串的混合的文本文件,每存入数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约200项号和说明的文本文件,它的格式是这样(没有子弹):

I have a text file with about 200 item numbers and descriptions, which is formatted like this (without the bullets):


  • 1642纯羊毛T恤

  • 613股价红色鞋

  • 3477蓝帽子羽毛

  • ...

我想读的项目编号和描述存储到相应的阵列,这是由空格分隔。我的问题是:

I am trying to read and store the items numbers and descriptions into respective arrays, which is delimited by space. My issues are:


  1. 我想忽略描述的空间。

  2. 当我进行排序或删除项目,我想确保描述被删除。

这是我迄今为止尝试过,但得到ArrayIndexOutOfBoundsException异常错误,我甚至不知道这是否会正确读取的描述:

Here's what I have tried so far but getting ArrayIndexOutOfBoundsException error and I am not even sure if it will read the description properly:

private Scanner file;
private int item = 0;
private String desc = "";
private int[] itemArr = new int[200];
private String[] descArr = new String[200];
int n = 0;


public void openFile(){

    try{
        file = new Scanner(new File("inventory.txt"));
    }

    catch(Exception e){
        System.out.println("file not found");
    }

}

public void readFile(){         

    while(file.hasNextLine()){    
        if (file.hasNextInt()){     
            item = file.nextInt();
        }

        while(!file.hasNextInt() && !file.hasNextLine()){
            desc = desc + file.next() + " ";
        }

        itemArr[n] = item;
        descArr[n] = desc;
        n++;
    }

    for (int i = 0; i < n; i++){
        System.out.println(itemArr[i] + " " + descArr[n] + "\n");
    }
    System.out.println("Total Records (n): " + n);

}

还是有更好的方式来做到这一点?我读过一些关于后模式和正则表达式,但不知道如何使用,要么。

Or is there a better way to do this? I've read some post about Patterns and Regex, but not sure how to use that either.

感谢您!

推荐答案

有是 N 超过 200 。如果在,而循环则更多的迭代200:

There is no protection on n exceeding 200. If there are more that 200 iterations of the while loop then:

itemArr[n] = item;

将抛出一个 ArrayIndexOutOfBoundsException异常

如果在 INT 在每行的开头是独一无二的,你可以使用地图&LT;整数,字符串&GT; 来存储数据。这会不会对可以从文件中读取的项目数 200 的限制,如果你选择了 TreeMap的因为它将对它们进行排序的执行(你可以接受整数或定义你自己的自然顺序比较 )。正如建议你可以使用的BufferedReader 来读取文件

If the int at the beginning of each line is unique you could use a Map<Integer, String> to store the data. This will not place a limit of 200 on the number of items that can be read from the file and if you selected a TreeMap as the implementation it would sort them (you can either accept the natural ordering of Integer or define you own Comparator). As suggested by sethu you could use a BufferedReader to read the file.

例如:

BufferedReader br = new BufferedReader(new FileReader("inventory.txt"));
Map<Integer, String> items = new TreeMap<Integer, String>();

String line;
while (null != (line = br.readLine()))
{
    String[] line_parts = line.split(" ");
    if (line_parts.length > 1)
    {
        StringBuilder desc = new StringBuilder(line_parts[1]);
        for (int i = 2; i < line_parts.length; i++)
        {
            desc.append(line_parts[i]);
        }
        items.put(new Integer(line_parts[0]), desc.toString());
    }
}

for (Integer key: items.keySet())
{
    System.out.println(key + " = " + items.get(key));
}

这篇关于如何读取由空格分隔int和字符串的混合的文本文件,每存入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 01:33