繁体   English   中英

从文件将ArrayList字符串转换为ArrayList整数

[英]Converting ArrayList String to ArrayList Integer from a file

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collection;


public class Challenge16 {



public static void main(String [] args)
{
    try {
        FileReader file = new FileReader("C:/Users/Mo/Desktop/Challenge16.txt");


    BufferedReader reader = new BufferedReader(file);

    String text = "";
    String line = reader.readLine();// readLine reads line after line of the stream given aka "file"
    ArrayList<String> textArr = new ArrayList<String>();

    while(line!=null)
    {
        //text = text + "\n" + line;
        textArr.add(line);

        line = reader.readLine();
    }

    //System.out.println(textArr.get(0));

    ArrayList<Integer> numArr = new ArrayList<Integer>();
    for(String number : textArr)
    {
        numArr.add(Integer.parseInt(number));
    }


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


}

我知道了,文件有13行,第一个数字是12,表示接下来有几行。 下一行和随后的行具有随机数,例如12 34 123 453,它们不一致,某些行只有2个数字,例如15 23,基本上我想将其相加。

我的问题是将ArrayList从String转换为Integer。 当我转换它时,编译器说,当我运行它时,“找不到文件” ...为什么这么说? 我尝试了十亿种其他方法来修复它,但它总是说,一旦我将其转换,就找不到该文件。 文件看起来像这样。

12

3621 6076 1329 13501 8180 2960 6567 10251 8663 13215 16302 9248 9848 0

1759 176 1724 226 1560 1162 534 451 0

809194412362 1010 314589 366 397 408 0

1637 987 199 1241 977 1355 424 1575 226 134 1751 1950 359 1262 0

1712 71 1571 1281 458 345 2004 430 973 1132 0

15578 6926 7053 12286 14821 8639 5824 6249 3089 9210 2460 0

20 1027 3287 2543 3152 977 1871 3293 0

7 45 16 61 166 0

4183 4614 1852 7709 2565 1070 3837 5477 4194 5381 1890 0

14057 10925 3379 10820 4710 15986 14725 12191 12773 14806 13527 13220 0

716 11885 8157 13877 3866 0

46 96 53 166 11 0

^其在实际文件中的所有单行间距

您确定在文件路径中使用/而不是\\吗? 当我尝试使用C读取文件时遇到类似的问题。

这是一个经过测试的有效示例:

public static void main(String [] args)
{
    try {
        FileReader file = new FileReader("/var/www/alpha/src/alpha/Challenge16.txt");


    BufferedReader reader = new BufferedReader(file);

    String text = "";
    String line = reader.readLine();// readLine reads line after line of the stream given aka "file"
    ArrayList<String> textArr = new ArrayList<String>();

    while(line!=null)
    {
        //text = text + "\n" + line;
        textArr.add(line);

        line = reader.readLine();
    }

    //System.out.println(textArr.get(0));

    ArrayList<Integer> numArr = new ArrayList<Integer>();
    for(String number : textArr)
    {
        for(String num: number.split(",")){
            if(num != null)
            numArr.add(Integer.parseInt(num));
        }
    }

    for(Integer x : numArr)
        System.out.println(x);

    }catch (NumberFormatException e) {
        System.out.println("error "+ e.getMessage());

    }catch(Exception e){
        System.out.println("File not found \n");
    }


}


}

假设您的输入文件如下:

12 3621,6076,1329,13501,8180,2960,6567,10251,8663,13215,16302,9248,9848,0 1759,176,1724,226,1560,1162,534,451,0 809,194,412,362,1010,314,589,366,397,408,0 1637,987,199,1241,977,1355,424,1575,226,134,1751,1950,359,1262,0 1712,71,1571,1281,458,345,2004,430,973,1132,0 15578,6926,7053,12286, 14821,8639,5824,6249,3089,9210,2460,0 20,1027,3287,2543,3152,977,1871,3293,0 7,45,16,61,166,0 4183,4614,1852,7709,2565 ,1070,3837,5477,4194,5381,1890,0 14057,10925,3379,10820,4710,15986,14725,12191,12773,14806,13527,13220,0 716,11885,8157,13877,3866,0 46,96,53,166,11,0

输出:

12 3621 6076 1329 13501 8180 2960 6567 10251 8663 13215 16302 9248 9848 0 1759 176 1724 226 1560 1162 534 451 0 0809194 412 362 1010 314 589 366 397 408 0 1637 987 199 1241 977 1355 424 1575 226 134 1751 1950 359 1262 0 1712 71 1571 1281 458 345 2004 430 973 1132 0 15578 6926 7053 12286 14821 8639 5824 6249 3089 9210 2460 0 20 1027 3287 2543 3152 977 1871 3293 0 7 45 16 61166166 0 4183 4614 1852 7709 2565 1070 3837 5477 4194 5381 1890 0890 14057 10925 3379 10820 4710 15986 14725 12191 12773 14806 13527 13220 0 716 11885 8157 13877 3866 0 46 96 53 166 11 0

暂无
暂无

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

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