简体   繁体   中英

ArrayList String conversion to ArrayList Integer [on hold]

    ArrayList<String> str = new ArrayList<String>();
    ArrayList<Integer> results = new ArrayList<Integer>();
    File file1 = new File("file3.txt");

    try
    {
        Scanner s = new Scanner(file1);
        s.useDelimiter("\\*-\\*");
        str.add(s.next());
    } catch (FileNotFoundException e)
    {
        System.out.println("File Not Found");
        e.printStackTrace();
    }

    try
    {
        for (String num : str)
        {
            results.add(Integer.parseInt(num));
            System.out.println(results);
        }
    } catch (NumberFormatException nfe)
    {

        System.out.println("Number Format is Not Correct");
        nfe.printStackTrace();
    }           

str is ArrayList of String

AND

results is ArrayList of Integer

I am trying to convert ArrayList of String into ArrayList of Integer but getting NumberFormatException .

In String ArrayList I add elements (numbers as string) from a sequence file.

I am supposing that what you have is a List<String> and you want to convert that into a List<Integer> . I suppose you can already read the list from your file so I am going to skip that part.

public List<Integer> convertToIntegerList(List<String> inputs) {
    if (inputs == null) {
       return Collections.EMPTY_LIST;
    }
    List<Integer> converted = new LinkedList<>();
    for (String input : inputs) {
        try {
           converted.add(Integer.parseInt(input));
        } catch (NumberFormatException ex) {
           // Note that Integer.parseInt() will throw a NumberFormatException
           // if the argument doesn't represent a valid Integer.
           // How you handle that is on you. For this one we are just going to ignore the invalid number and print it.
           System.out.println("Failed to convert " + input);
           continue;
        }
    }
     return converted;
}

Update: As per the updated question it looks like your file is not properly formatted and your inputs contains invalid values. I would begin by printing out every element of the list and confirming that it is actually a valid input (the file reading was successful)

Update #2:

There are a few things that seem wrong but without having a sample file I can't really tell what exactly, only guess. So to begin with you are creating a Scanner from a file which is right. Note that Scanner.useDelimiter accepts a String that is converted to a Pattern as a regex. Your given regex \\*-\\* translates that the delimiter that you are going to use is literally any character followed by a - and followed by any character .

I believe your file looks more something like this

 1-2-3-4...

The correct delimiter to use for that input would be scanner.useDelimiter("-");

This will split the input on the - and return tokens. Now from your code you only read one token, the very first one and you discard the others.

I would convert that to

while (scanner.hasNext()) {
    inputs.add(scanner.next());
}

Again all of that is speculation, adding an input file would clear the confusion.

The only possible reason is "num" in this line is not a string representing a valid number:

results.add(Integer.parseInt(num));

try to System.out.println(num) before this line to double check. for example:

"123" this is string which can be parsed to number " 123" this is NOT. you need to remove the leading characters

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