# ERROR: LoadError: BoundsError: attempt to access 10-element Vector{Int64} at index \[11\]

**URL:** <https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114>\
**Category:** New to Julia\
**Created:** [April 16, 2025, 7:13am UTC](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114 "2025-04-16T07:13:57Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [April 16, 2025, 7:13am UTC](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114/1 "2025-04-16T07:13:57Z")

</div>

Hello,  
The contents of the `file.txt` file is as follows:

```julia
1 2 3 4 5 6 7 8 9 10

```

I wrote the following code to put the numbers in the file into an array:

```julia
function arrayandfile()
    array = fill(0,10)
    inn = open("file.txt","r")
    counter = 1
    while !eof(inn)
        c = read(inn,Char)
        if c != ' '
            array[counter] = parse(Int,c)
            counter +=1
        end
    end
    return(array)
end
println(arrayandfile())

```

Which part of the code is wrong?

Thank you.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [April 16, 2025, 7:23am UTC](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114/2 "2025-04-16T07:23:14Z")

</div>

Probably the part that thinks that “10” is one character and not a “1” followed by a “0”.

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [April 16, 2025, 7:41am UTC](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114/3 "2025-04-16T07:41:16Z")

</div>

Hello,  
Thank you so much for your reply.  
The `read` function interprets the number `10` as `1` and `0` instead of `10`.  
What is the solution?

---

<div class="post-metadata">

**Author:** ![karei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karei/32/214809_2.png) [@karei](https://discourse.julialang.org/u/karei)\
**Post date:** [April 16, 2025, 7:41am UTC](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114/4 "2025-04-16T07:41:21Z")

</div>

[GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback) is right. In this case, I would recommend that you just read in the entire txt file and then process certain lines in it as needed. That’s how I got it when I did Atcoder.

```julia
data_str = read(joinpath(@ __DIR__ , "file.txt"), String)
lines = split(data_str, "\n", keepempty=false)
array = parse.(Int, split(lines[1]))

```

When dealing with data like `1 2 3 4 5 6 7 8 9 10`, first read the line in its entirety and then separate all the numbers with the `split()`, which is the Julia style of writing. Your writing style is somewhat like C.

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [April 16, 2025, 7:46am UTC](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114/5 "2025-04-16T07:46:03Z")

</div>

> [@karei](#):
>
> ```julia
> data_str = read(joinpath(@ __DIR__ , "file.txt"), String)
> lines = split(data_str, "\n", keepempty=false)
> 
> ```

You could also use `readlines` (or `eachline`).

* * *

By the way, the title of the thread is wrong/misleading: you’re attempting to access an `X`-element `Vector{Int64}` at index `[X + 1]` (for `X == 10`). As Julia is 1-indexed, accessing at index `X == length(...)` is completely fine.

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [April 16, 2025, 7:57am UTC](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114/6 "2025-04-16T07:57:00Z")

</div>

Hello,  
Thank you so much for your reply.  
I changed the code as follows:

```julia
function arrayandfile()
    array = fill(0,10)
    counter = 1
    for line in eachline("file.txt")
        if line != ' '
            array[counter] = parse.(Int, split(line))
            counter +=1
        end
    end
    return(array)
end
println(arrayandfile())

```

I got:

```julia
ERROR: LoadError: MethodError: Cannot `convert` an object of type Vector{Int64} to an object of type Int64

```

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [April 16, 2025, 8:33am UTC](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114/7 "2025-04-16T08:33:17Z")

</div>

What’s happening here is that `parse.(Int, split(line))` gives you a `Vector{Int}` (namely, `[1, 2, ..., 10]`), which you’re trying to assign to the single `Int` at `array[counter]`.

You could use

```julia
function arrayandfile()
    array = fill(0, 10) # where 10 is the total number of values in the file
    counter = 0 # number of values actually put in array so far
    for line in eachline("file.txt")
        line_values = parse.(Int, split(line))
        array[counter + 1 : counter + length(line_values)] .= line_values
        counter += length(line_values)
    end
    
    return array
end

```

I’ve removed the `line != ' '` check, as

1. it’s no longer necessary as `split` takes care of the spaces
2. `line`, being a `String`, will never equal `' '` (also note that `" " != ' '`).

* * *

You could also go full-on vectorisation and write this as a one-liner

```julia
reduce(vcat, parse.(Int, split(line)) for line in eachline("file.txt"))

```

E.g.

```julia-repl
julia> reduce(vcat, parse.(Int, split(line)) for line in eachline(IOBuffer("1 2 3\n4 5 6 7 8\n9 10")))
10-element Vector{Int64}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

```

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [April 16, 2025, 9:38am UTC](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114/8 "2025-04-16T09:38:02Z")

</div>

Thanks again.  
I’m a newbie and your code is a little difficult for me to understand.

1- If `counter=1`, then the line `array[counter] = parse.(Int, split(line))` places the number `1` in cell `1` of the array. Am I right?

2- I changed the code as follows, removed the (`counter`) and the problem was fixed:

```julia
function arrayandfile2()
    array = fill(0,10)
    for line in eachline("file.txt")
        if line != ' '
            array = parse.(Int, split(line))
        end
    end
    return(array)
end
println(arrayandfile())

```

Why?

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [April 16, 2025, 10:25am UTC](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114/9 "2025-04-16T10:25:12Z")

</div>

> [@hack3rcon](#):
>
> 1- If `counter=1`, then the line `array[counter] = parse.(Int, split(line))` places the number `1` in cell `1` of the array. Am I right?

No. The dot at the end of `parse.` means we are [broadcasting](https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting) the `parse` statement, resulting in a `Vector`. So the left-hand side of the assignment is an `Int`, but the right-hand side is a `Vector{Int}`.

One of the neat aspects of Julia is its interactivity in the REPL. You could debug by just checking all variables involved, via

```julia-repl
julia> array = fill(0,10)
10-element Vector{Int64}:
 0
 0
 0
 0
 0
 0
 0
 0
 0
 0

julia> counter = 1
1

julia> line = first(eachline(IOBuffer("1 2 3 4 5 6 7 8 9 10"))) # or your file
"1 2 3 4 5 6 7 8 9 10"

julia> line != ' '
true

julia> array[counter] = parse.(Int, split(line))
ERROR: MethodError: Cannot `convert` an object of type Vector{Int64} to an object of type Int64
The function `convert` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  convert(::Type{T}, ::T) where T<:Number
   @ Base number.jl:6
  convert(::Type{T}, ::T) where T
   @ Base Base.jl:126
  convert(::Type{T}, ::AbstractChar) where T<:Number
   @ Base char.jl:185
  ...

Stacktrace:
 [1] setindex!(A::Vector{Int64}, x::Vector{Int64}, i::Int64)
   @ Base .\array.jl:987
 [2] top-level scope
   @ REPL[5]:1

julia> array[counter]
0

julia> split(line)
10-element Vector{SubString{String}}:
 "1"
 "2"
 "3"
 "4"
 "5"
 "6"
 "7"
 "8"
 "9"
 "10"

julia> parse.(Int, split(line))
10-element Vector{Int64}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

julia> typeof(array[counter])
Int64

julia> typeof(parse.(Int, split(line)))
Vector{Int64} (alias for Array{Int64, 1})

```

Alternatively, you could also of course use something like Debugger.jl, though I find it a bit harder to get used to.

> [@hack3rcon](#):
>
> the problem was fixed:

Now within each iteration step you’re creating a _new_ variable called `array` holding our `Vector{Int}` from the `parse.`. This will work in this case, but not if you have multiple lines. Only the value in the last iteration step will be returned.

---

<div class="post-metadata">

**Author:** ![karei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karei/32/214809_2.png) [@karei](https://discourse.julialang.org/u/karei)\
**Post date:** [April 16, 2025, 10:41am UTC](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114/10 "2025-04-16T10:41:21Z")

</div>

Good questions.

`for line in eachline("file.txt")` in each iteration, `line` is a string of all the characters in that line, a string that`"1 2 3 4 5 6 7 8 9 10"`,

then `split(line)` gives you

```julia
10-element Vector{SubString{String}}:
 "1"
 "2"
...
 "9"
 "10"

```

`parse.(Int, split(line))` gives you

```julia
10-element Vector{Int64}:
  1
  2
...
  9
 10

```

This is a vector of length 10, instead of receiving it with `array[counter]`, use `array = parse.(Int, split(line))`. What you wrote in the second question is correct, but limited to the file being only one line.

Want to know if a file has multiple lines?  
If there’s something else you don’t understand, go ahead and ask a question.

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [April 16, 2025, 12:11pm UTC](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114/11 "2025-04-16T12:11:18Z")

</div>

Thanks again.  
Your answer raised a few questions for me:

1- Does the following method fix broadcasting?

```julia
save = parse.(Int, split(line))
array[counter] = save

```

2- Can you explain `Vector` to me in simple terms?

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [April 16, 2025, 12:30pm UTC](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114/12 "2025-04-16T12:30:03Z")

</div>

Hello,  
Thank you so much for your reply.  
I can’t understand `Vector`!

---

<div class="post-metadata">

**Author:** ![karei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karei/32/214809_2.png) [@karei](https://discourse.julialang.org/u/karei)\
**Post date:** [April 16, 2025, 1:26pm UTC](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114/13 "2025-04-16T13:26:52Z")

</div>

A _vector_ is an elementary mathematical concept that represents a _directed line segment_. The mathematical notation is denoted as _ **v** _.

The length of the projection of a vector on each axis is the vector’s component. e.g. _ **v** _ = [1,2,3].  
Clearly, the components of a vector are _scalars_.

What is called the dimension of a vector in mathematics refers in programming to the length of the vector, i.e., the number of components of the vector. `length(v)=3`

Here are some common uses of vectors in julia.

```julia
v=[1,2,3] # typeof(v)=Vector{Int64}
v[1]=10 # now v=[10,2,3]
v[2:3]=[4,6] # now v=[10,4,6]
v2=dot(v,v) # cross product of vectors
v3=v.*v # dot product of vectors

```

---

<div class="post-metadata">

**Author:** ![TZ.Neumann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tz.neumann/32/34438_2.png) [@TZ.Neumann](https://discourse.julialang.org/u/TZ.Neumann)\
**Post date:** [April 16, 2025, 1:44pm UTC](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114/14 "2025-04-16T13:44:19Z")

</div>

Vector is just one-dimensional array. see doc [Base.Vector](https://docs.julialang.org/en/v1/base/arrays/#Base.Vector) e.g., Vector{T} is alias for Array{T, 1} where T is type of elements it holds and 1 means it’s one dimensional.

```Julia
# Creating vectors
numbers = [1, 2, 3, 4, 5] # Using square brackets
empty_vector = Vector{Int}(undef, 3) # Creates uninitialized vector of 3 integers
zeros_vector = zeros(5) # Creates [0.0, 0.0, 0.0, 0.0, 0.0]
string_vector = ["apple", "banana", "cherry"]

```

you can try with Julia one-liner style:

```Julia
arrayandfile(filename) = parse.(Int, split(read(filename, String))) # Read entire file content and return the vector

```

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [April 16, 2025, 2:08pm UTC](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114/15 "2025-04-16T14:08:59Z")

</div>

> [@hack3rcon](#):
>
> Can you explain `Vector` to me in simple terms?

> [@karei](#):
>
> A _vector_ is an elementary mathematical concept that represents a _directed line segment_.

I doubt invoking linear algebra is the easiest way to explain the notion of `Vector` in Julia 🙂. (Also, I’d define a mathematical vector simply as an element of a vector space, which in turn is a triple consisting of a set and two operations, satisfying certain properties.)

If you insist on referring to mathematics, a `Vector{Int}` of length N \in \mathbb{N} is just an element of \mathbb{Z}^N (if you ignore the differences between \mathbb{Z} and the set of `Int`s).  
By the way, note that \mathbb{Z}^N is _not_ a vector space, though it is a subset (and subgroup) of the vector space \mathbb{R}^N. Something like a `Vector{String}` clearly also does not correspond to some vector space.

* * *

I would just explain a `Vector` as a finite sequence of ‘things’. A `Vector{Int}` is a finite sequence of `Int`s, a `Vector{String}` a finite sequence of `String`s, a `Vector{Any}` a finite sequence of whatever your heart desires.

> [@karei](#):
>
> ```julia
> v2=dot(v,v) # cross product of vectors
> v3=v.*v # dot product of vectors
> 
> ```

- Obviously `dot(v, v)` is the dot (inner) product of `v` with itself (a.k.a. `v ⋅ v` ). The result is a scalar (`Int` here). To be able to use `dot` and `⋅` you first need to import `LinearAlgebra`.
- `v .* v` is the elementwise product of `v` with itself. The result is a `Vector` satisfying `(v .* v)[i] == v[i] * v[i]` for i \in \{1, 2, 3\}.
- `cross(v, v)` (`v × v`) is the cross product of `v` with itself. In general, for two `Vector`s `v` and `w` _of length 3_, `v × w` is another `Vector` of length 3. But `v × v` consists of zeros. You need to import `LinearAlgebra` to use `cross` and `×`.

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [April 16, 2025, 2:29pm UTC](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114/16 "2025-04-16T14:29:14Z")

</div>

> [@hack3rcon](#):
>
> 1- Does the following method fix broadcasting?
> 
> ```julia
> save = parse.(Int, split(line))
> array[counter] = save
> 
> ```

Again no. (Also, broadcasting is a proper, awesome feature, not a bug. It does not need fixing 🙂 .)  
You can view

```julia
save = parse.(Int, split(line)) # Note the dot .

```

as shorthand for

```julia
splt = split(line)
save = Vector{Int}(undef, length(splt))
for i = 1:length(splt)
    save[i] = parse(Int, splt[i]) # Note there's no dot
end

```

Then `array[counter] = save` attempts to put this entire `Vector{Int}` `save` (a sequence of 10 `Int`s) into `array[counter]` where we have room for only a single `Int`. So this will cause an error.

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [April 16, 2025, 6:30pm UTC](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114/17 "2025-04-16T18:30:38Z")

</div>

Hello,  
Thank you so much for your reply.  
Do you mean that `10` is not a single `Int`?

What is the solution?

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [April 16, 2025, 6:33pm UTC](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114/18 "2025-04-16T18:33:21Z")

</div>

Hello,  
When I define an array using `array = fill(0,10)`, what is the maximum number each cell of the array can hold?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [April 16, 2025, 7:00pm UTC](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114/19 "2025-04-16T19:00:27Z")

</div>

The word “ **ten** ” has three letters (or characters) in it; t, e, n. The text “ **10** ” has two characters: 1, 0. The roman numeral “ **X** ” is just one letter. All are ways of writing down and communicating a particular idea to humans — once we read it we get the concept of a single number in our minds.

Julia — and nearly all programming languages — have a distinction between strings (sequences of characters like `"ten"` and `"10"`) and numbers (without quotes: `10`). When you parse a bit of text (a String) into an `Int`, you’re doing the work to interpret that text as a number. It’s a single value at that point, and it could be as large as a few quintillions (or negative, too).

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [April 16, 2025, 7:47pm UTC](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114/20 "2025-04-16T19:47:05Z")

</div>

Hello,  
Thank you so much.  
The result of `parse.(Int, split(line))` is:

```julia
10-element Vector{Int64}:
  1
  2
...
  9
 10

```

Here, the number `10` is of type `Int`. Here `10` should be `10`, not `1` and `0`. I don’t understand why it doesn’t fit in the array cell!

[Next page](https://discourse.julialang.org/t/error-loaderror-boundserror-attempt-to-access-10-element-vector-int64-at-index-11/128114.md?page=2)
