简体   繁体   中英

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


}

I got that far, the file has 13 lines, the first number is 12 which indicates how many lines follow. The next line and the lines that follow have random numbers like 12 34 123 453, they are not consistent some lines have just 2 numbers like 15 23 , Basically I'm suppose to add it up.

The problem I have is with converting the ArrayList from String to Integer. When I convert it, the compiler says, when i run it, "File not found"... Why does it say that?? I have tried a billion other ways to fix it but it always says, as soon as i convert it, that file is not found. File looks like this.

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

^ its all single spaced in the actual file

Are you sure your supposed to be using / instead of \\ in the file path? Ran into similar problems when I tried to read a file using C.

Here is a working and tested sample:

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


}


}

assuming that your input file like :

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

output :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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