Code working inside IntelliJ but not outside

Hi,

i have a big Problem. I use IntelliJ with the go-plugin. When I run in IntelliJ following code works everything.

package main

import "fmt"
import "bufio"
import "os"
import "strings"
import "strconv"
import "shuffle"

func createlist(werte[] string) (Slice[] int){
anfang, _ := strconv.Atoi(werte[0])
schluss, _ := strconv.Atoi(werte[1])

zahlenreihe := []int{schluss}

for i := anfang; i < schluss; i++ {
	zahlenreihe = append(zahlenreihe, i)
}
shuffle.Ints(zahlenreihe)
fmt.Printf("%v\n", zahlenreihe)
return zahlenreihe
}

func bubblesort(liste[] int) (Slice[] int){
for k := 1; k < len(liste); k++ {
	for i := 0; i < len(liste) - k; i++ {
		if liste[i] > liste[i + 1] {
			temp := liste[i + 1]
			liste[i + 1] = liste[i]
			liste[i] = temp
		}

	}
}
return liste
}

func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter text: ")
text, _ := reader.ReadString('\n')
text = strings.Trim(text, "\n")
erg := strings.Split(text, ",")


fmt.Printf("%v", bubblesort(createlist(erg)))
} 
`

Example:
Enter text: 11,22
[21 18 13 16 22 19 14 20 12 17 11 15]
[11 12 13 14 15 16 17 18 19 20 21 22]

When I run “go run” without IntelliJ comes this wrong result:
Enter text: 11,22
[0]
[0]

The same wrong result comes at a exe with “go build” generated.

Can anyone tell me what I am doing wrong?

P.S. the shuffle package: github.com/shogo82148/go-shuffle

Not sure which package “shuffle” is. I supposed that it is mixing numbers in your slice. The id “Slice” has no use, i.e. func createlist(werte[] string) []int { is returning the result as a slice.

Hope this helps

Thank @Costa_Konstantinidis for your answer, but it dont fix my Problem. Meanwhile, I was able to narrow the problem.

The Problem are this row in the createlist-Function:

schluss, _ := strconv.Atoi(werte[1])

Outside IntelliJ “schluss” always zero O.o. Any Idea why?

You’re ignoring the error return. Don’t do that, it might tell you what’s wrong.

1 Like

Thanks @calmh simple answer but big result. Inside IntelliJ the “Enter”-Key after input des Value generate a “\n” inside the windows cmd it is a “\r\n”.

So ich change

text = strings.Trim(text, "\n") 

to

text = strings.Trim(text, "\r\n")

and no it works.

1 Like

You should probably handle both cases if you want your code to be portable. CRLF is standard for Windows, but LF is standard for every other common platform.

The bufio.Scanner type does all this for you.

Thanks @dfc. I change the mainfunction to this:

func main() {
      scanner := bufio.NewScanner(os.Stdin)
      fmt.Print("Enter text: ")
      scanner.Scan()
      erg := strings.Split(scanner.Text(), ",")

      fmt.Printf("%v", bubblesort(createlist(erg)))
}

Just a reminder to check the returns from your scanner, number of fields resulting from the split, things like that. It’ll make things easier in the long run, I promise. :wink:

You need to use the api properly and develop good error checking habits.

Do you mean like that?

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    fmt.Print("Enter text: ")
    if scanner.Scan() {
	    scanner.Text()
	    erg := strings.Split(scanner.Text(), ",")
	    if len(erg) == 2 {
		    fmt.Printf("%v", bubblesort(createlist(erg)))
	    }
     }
}

or

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    fmt.Print("Enter text: ")
    for scanner.Scan() {
	    erg := strings.Split(scanner.Text(), ",")
	    if len(erg) == 2 {
		    fmt.Printf("%v", bubblesort(createlist(erg)))
	    }
     }
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.