Why do I get `ParseIntError { kind: InvalidDigit }` when parsing what seems to be a valid integer string?

  Kiến thức lập trình

I am writing a program that reads a file line-by-line, extracts all digits contained in each line (ignoring any other characters), interprets the resulting digits in each line as an integer, and prints the sum of the integers.

The file is the following:

1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

And the expected output is 12 + 38 + 12345 + 7 = 12402

This is my attempt:

use std::fs;

fn main() {
    let contents = fs::read_to_string("day1.txt").expect("Should have been able to read the file");
    const ARRAY_REPEAT_VALUE: String = String::new();
    let mut number_array: [String; 4] = [ARRAY_REPEAT_VALUE; 4];
    //let mut number_array:[String; 1000] = [String::new(); 1000];
    let mut counter:usize = 0;

    for i in  contents.lines(){
        let mut on_to_next: bool = false;
        let mut num1:char = '';
        let mut num2:char = '';
        for j in i.chars() {
            if j.is_numeric() {
                if on_to_next
                {
                    num2 = j;

                }
                else {
                    num1 = j;
                    on_to_next = true;
                }
            }
        }
        number_array[counter] = format!("{}{}",num1,num2);
        counter += 1;
    }
    //print!("{}", number_array[2]);

    let mut total: i32 = 0;
    for p in number_array{
        print!("-{}-n", p.trim());
        let converted_number: i32 = p.trim().parse().unwrap();
        total += converted_number;
    }
    print!("{}",total);
}

But it fails with:

thread 'main' panicked at src/main.rs:35:54:
called `Result::unwrap()` on an `Err` value: ParseIntError { kind: InvalidDigit }

Why does it happen, and what can I do to fix this problem?

New contributor

Jack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT