简体   繁体   English

当我必须使用文件阅读器Java时,如何从txt文件填充数组?

[英]How do i populate an array from a txt file when i have to use file reader Java??

I have to populate an array with integers that are in a text file, i need the file reader to take an integer from each line and put into an array but it cant put duplicates in to the array aswell which is making it even more complex, and the duplicates, i have to write them to another text file eg:sorted.txt , i cant figure it out how to do all that im only in my first year i college. 我必须使用文本文件中的整数填充数组,我需要文件阅读器从每一行中取一个整数并放入一个数组但是它不能将重复项放入数组中,这使得它更加复杂,和重复,我必须把它们写到另一个文本文件,例如:sorted.txt,我无法弄清楚如何在我大学的第一年里做所有这些。 if someone could help would be gratefully appreciated. 如果有人可以提供帮助,我将非常感激。 Thank you in advance 先感谢您

here is what i got so far in my Method 这是我到目前为止在我的方法中得到的

public static void readFromfile()throws IOException {
    List<String> lines = new ArrayList<String>();
    BufferedReader reader = null;
    try {
     reader = new BufferedReader(new FileReader("file.txt"));
     String line = null;
     while ((line = reader.readLine()) != null) {
        lines.add(line);
    }
} finally {
    reader.close();
}
int[] array = lines.toArray();// i keep getting incopatible type error in this line
awell

for the last 6 days iv bein doing this and thats how far i got :( 在过去的6天iv bein这样做,那就是我有多远:(

 int[] array = lines.toArray();// i keep getting incopatible type error in this line 

Of course you do, List<String>#toArray returns an Object[] , not an int[] . 你当然可以, List<String>#toArray返回一个Object[] ,而不是一个int[] :-) :-)

Ideally, you'd be able to do this by declaring your list as List<int> (or List<long> if the numbers will be really big). 理想情况下,您可以通过将列表声明为List<int> (或List<long>如果数字非常大)来实现此目的。 Unfortunately, at least in Java 6, you can't do that, you have to use List<Integer> / List<Long> instead. 不幸的是,至少在Java 6中,你不能这样做,你必须使用List<Integer> / List<Long> So that's your starting point. 所以这是你的出发点。

Then parse the numbers from the strings (eg, from line ) as you go. 然后在你去的时候解析字符串中的数字(例如,从line )。 Integer.parseInt (or Long.parseLong ) can do the parsing for you. Integer.parseInt (或Long.parseLong )可以为您解析。 Their results are int and long respectively, but they'll get auto-boxed when added to the list. 它们的结果分别是intlong ,但是当它们被添加到列表中时它们会被自动装箱。

Alternately, you might look at the Scanner class , which is "...A simple text scanner which can parse primitive types and strings using regular expressions..." 或者,您可以查看Scanner ,它是“......一个简单的文本扫描程序,它可以使用正则表达式解析基本类型和字符串......”

To get the final array of int[] (for instance) from the List<Integer> list is a bit of a pain. List<Integer>列表中获取int[] (例如)的最终数组有点痛苦。 If you can use an Integer[] instead (and you mostly can, thanks to autoboxing / unboxing), that's easy: Integer[] numbers = yourList.toArray(new Integer[yourList.size()]); 如果您可以使用Integer[] (并且您通常可以使用自动装箱/取消装箱),那很简单: Integer[] numbers = yourList.toArray(new Integer[yourList.size()]);

If you really need an int[] instead, you'll have to write a loop to copy it, or use something like the Apache Commons toPrimitive method . 如果你真的需要一个int[] ,你必须编写一个循环来复制它,或者使用类似Apache Commons toPrimitive方法的东西。

Using the Scanner class will make things easier. 使用Scanner类可以简化操作。

List<Integer> numbers = new ArrayList<Integer>();
Scanner s = new Scanner(new FileInputStream("file.txt"));

while (s.hasNextInt()) {
   numbers.add(s.nextInt());
}

Your problem is that you have a List of Strings and your trying to turn that into an int array . 你的问题是你有一个List of Strings并试图将其转换为一个int array

As TJ Crowder pointed out, BUT you cannot have a List<int> - you must use the wrapper class Integer . 正如TJ Crowder指出的那样,但是你不能拥有List<int> - 你必须使用包装类Integer

So change your list to List<Integer> , and then it will be lines.add(Integer.parseInt(line)); 所以将列表更改为List<Integer> ,然后它将是lines.add(Integer.parseInt(line));

I recommend to use Scanner class, it is easier than what you are doing. 我建议使用Scanner类,它比你正在做的更容易。 The error is due to assigning object to integer type. 该错误是由于将对象分配给整数类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM