5

I tackle with this question.

I need to convert strings to int. In this case, I need to convert "5 2 4 6 1 3" to, for example, [6]int{5,2,4,6,1,3}. I wrote following this code, especially AizuArray(). It seems elements are int here. Would you please let me know if my way is correct? Or could you let me know the better ways? I ask this because I feel my way would be redundant and Java way is much easier. Thank you.

package main

import (
    "fmt"
    "reflect"
    "strconv"
    "strings"
)

func AizuArray(A string, N string) []int {
    a := strings.Split(A, " ")
    n, _ := strconv.Atoi(N) // int 32bit
    b := make([]int, n)
    for i, v := range a {
        b[i], _ = strconv.Atoi(v)
    }
    return b
}

func main() {
    A := "5 2 4 6 1 3"
    N := "6"
    j := strings.Split(A, " ")
    for _, v := range j {
        fmt.Println(reflect.TypeOf(v))
    }
    b := AizuArray(A, N)
    fmt.Println(b)
    for _, v := range b {
        fmt.Println(reflect.TypeOf(v))
    }
}
2
  • 1
    Keep in mind that int size in go depends on the host CPU, it's 32bit on 32bit CPUs and 64bit on well, 64bit CPUs. Commented Jun 11, 2016 at 21:23
  • @OneOfOne Thank you for your comment. Yes, when I used ParseInt(), an error occurred in my netbook. So, I used Atoi instead. Commented Jun 13, 2016 at 12:18

3 Answers 3

9

Would you please let me know if my way is correct?

If you just want to convert string(space separated integers) to []int

func AizuArray(A string, N string) []int {
 a := strings.Split(A, " ")
 n, _ := strconv.Atoi(N) // int 32bit
 b := make([]int, n)
 for i, v := range a {
     b[i], err = strconv.Atoi(v)
     if err != nil {
        //proper err handling
        //either b[i] = -1 (in case positive integers)
     }
 }
 return b
}

then your approach is correct.

I tackle with this question.

In context of this question you want to take input from STDIN so should do,

package main

import (
    "fmt"
)

func insertionSort(arr []int) {
    //do further processing here.
   fmt.Println(arr)
}

func main() {
    var N int
    fmt.Scanf("%d", &N)
    b := make([]int, N)
    for iter:=0;iter<N;iter++ {
        fmt.Scanf("%d",&b[iter])
    }
    insertionSort(b)
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your answer. Ah, I will write error handling later. Thanks. And thank you for your next one. Ah, I will try fmt.Scanf. Thank you so much.
Ah, so is fmt.Scanf() better than scanner = bufio.NewScanner(os.Stdin); scanner.Scan(); N := scanner.Text()?
so if you want to take faster input stackoverflow.com/questions/31333353/faster-input-scanning have a look at this.
Thank you. I use fmt.Scanf() instead.
6

I think you overcomplicating things unless I am missing something.

https://play.golang.org/p/HLvV8R1Ux-

package main

import (
    "fmt"
    "strings"
    "strconv"
)

func main() {
    A := "5 2 4 6 1 3"

    strs := strings.Split(A, " ")
    ary := make([]int, len(strs))
    for i := range ary {
        ary[i], _ = strconv.Atoi(strs[i])
    }

    fmt.Println(ary)    
}

4 Comments

I down voted your answer because I think it is not relevant. First, it adds nothing to the question (you just reuse the code in the question and remove some parts of it). Second avoiding a returned error in go is always a bad advice. Third, according to the link in the question the value N exists and shall be used. And finally, using function is a better way of coding, so no need to remove it.
I answer to the exact question you asked. Or do you want us to solve your problem completely with clean "production" ready code? Stackoverflow is not the place for these kind of questions.
@Darigaaz Thank you for your reply. Yeah, I thought I did make it complicated. So I asked. I am a beginner of Go, so I thought there are something like packages or libraries which I don't know and couldn't find. Ah, I didn't write the comment. I didn't imply your solving the question. I just wanted to ask the part which to convert String to Int.
@Jean-NicolasMoal Thank you for your comment. I will implement the error handling later. Thank you.
1

Here is a simpler example where you would not have to split the string:

str := "123456"
if _, err := strconv.Atoi(str); err != nil {
    // do stuff, in case str can not be converted to an int
}
var slice []int // empty slice
for _, digit := range str {
    slice = append(slice, int(digit)-int('0')) // build up slice
}

Why do you need the int('0')? Because int() will convert the character to the corresponding ASCII code (ascii table here). For 0 that would be 48. So you will have to substract 48 from whatever your digit corresponds to in "ascii decimal".

1 Comment

Can you use int(digit - '0')?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.