chip
(chip)
June 8, 2016, 10:06am
1
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
chip
(chip)
June 8, 2016, 11:09am
3
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?
calmh
(Jakob Borg)
June 8, 2016, 11:45am
4
You’re ignoring the error return. Don’t do that, it might tell you what’s wrong.
1 Like
chip
(chip)
June 8, 2016, 11:50am
5
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
mathew
(mathew)
June 8, 2016, 3:17pm
6
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.
dfc
(Dave Cheney)
June 8, 2016, 11:16pm
7
The bufio.Scanner type does all this for you.
chip
(chip)
June 9, 2016, 8:53am
8
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)))
}
calmh
(Jakob Borg)
June 9, 2016, 8:55am
9
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.
dfc
(Dave Cheney)
June 9, 2016, 9:06am
10
You need to use the api properly and develop good error checking habits.
chip
(chip)
June 9, 2016, 9:30am
11
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)))
}
}
}
dfc
(Dave Cheney)
June 9, 2016, 9:58am
12
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)))
}
}
}
system
(system)
Closed
September 7, 2016, 9:59am
13
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.