Converting Char type to Int type

Hello,
I have a file similar to the following:

1 2 3 4 5 6 7 8 9 10

I wrote the following program to read this file character by character and convert its numbers to Int type:

function summer()
    inn = open("input.txt","r")
    c = 0 
    while !eof(inn)
        c = read(inn, Char)
        if c != ' '
             println(parse(Int, c))
        end
    end
end

summer()

I have two questions:
1- Is this program correct?

2- I got the following error:

ERROR: LoadError: ArgumentError: invalid digit: '\n'
Stacktrace:
  [1] parse(::Type{Int64}, c::Char; base::Int64)
    @ Base ./parse.jl:44
  [2] parse
    @ ./parse.jl:41 [inlined]
  [3] summer()
    @ Main /Download/file.jl:341
  [4] top-level scope
    @ /Download/file.jl:345
  [5] include(fname::String)
    @ Base.MainInclude ./client.jl:489
  [6] run(debug_session::VSCodeDebugger.DebugAdapter.DebugSession, error_handler::VSCodeDebugger.var"#3#4"{String})
    @ VSCodeDebugger.DebugAdapter ~/.vscode-oss/extensions/julialang.language-julia-1.127.2-universal/scripts/packages/DebugAdapter/src/packagedef.jl:122
  [7] startdebugger()
    @ VSCodeDebugger ~/.vscode-oss/extensions/julialang.language-julia-1.127.2-universal/scripts/packages/VSCodeDebugger/src/VSCodeDebugger.jl:45
  [8] top-level scope
    @ ~/.vscode-oss/extensions/julialang.language-julia-1.127.2-universal/scripts/debugger/run_debugger.jl:12
  [9] include(mod::Module, _path::String)
    @ Base ./Base.jl:495
 [10] exec_options(opts::Base.JLOptions)
    @ Base ./client.jl:318
 [11] _start()
    @ Base ./client.jl:552

Why?

Thank you.

That kind of depneds on what you want to do and how to treat (2) depends on that answer.
Do you want to convert Chars to the number they represent (so '1' becomes 1) or to their value if you interpreted the actual bits of the char as number (i.e. it’s codepoint IIRC such that '1' becomes 49)?

The error (2) is caused because to try to convert the final newline character '\n' to the numeric value it represents. That ofc fails because that char does not represent a number.
If you want to former option (convert to value the Char represents), then you need to skip newlines as well (in addition to the spaces you already skip). If you want the codepoints, then you need to use convert(Int, char) instead of parse(Int, char)

2 Likes

If the file is not enormous such that memory is not a problem, just read it as a single string, split and parse it…

Adding to already great explanations, assuming you want to convert string numbers to Int numbers you can slightly modify your code and do this:

function summer()
           inn = open("input.txt","r")
           c = 0 
           while !eof(inn)
               c = readuntil(inn, Char(' '))
               println(parse(Int, c))
           end
       end
1 Like

Hello,
Thanks for all the answers.
I changed the program as below and it worked:

function summer()
    inn = open("input.txt","r")
    c = 0 
    while !eof(inn)
        c = read(inn, Char)
        if c != ' ' && c != '\n'
             println(parse(Int, c))
        end
    end
end

summer()

Depending on what you’re really trying to do, you might want to look at isdigit or isnumeric.

3 Likes

And isspace

1 Like