#Need help with code

19 messages · Page 1 of 1 (latest)

shadow notch
#

I need help, I've got 3 numbers from a string, which i need to do calculations with, but i dont know how to use them.
This is what i've got:

package main

import (
    "bufio"
    "fmt"
    "os"
    "regexp"
    "strconv"
)

var afstand int
var tijd int
var Snelheid int

func main() {

    file, ferr := os.Open("DataG.txt")
    if ferr != nil {
        panic(file)

    }

    scanner := bufio.NewScanner(file)
    scanner.Split(bufio.ScanWords)

    for scanner.Scan() {

        re := regexp.MustCompile("[0-9]+")
        re.FindAllString(scanner.Text(), 2)
        strconv.Atoi(scanner.Text())
        if s, err := strconv.Atoi(scanner.Text()); err == nil {
            fmt.Printf("%v \n", s)

        }
    }

}

And this is the result:

PS C:\Users\Home> go run gebouwen.go
100 
150 
80

Now my problem is that i dont know how to use these numbers for math.

grave glade
#

assign them to a variable outside of the if statement

shadow notch
grave glade
#

do you know how to assign things to variables?

shadow notch
#

yeah, i just dont know how to assign the given numbers

grave glade
#

try it without a for loop

shadow notch
grave glade
#

try writing it for only one number, scanning and assigning it to a variable

shadow notch
#

im giving up

grave glade
#

show your code

shadow notch
#

well, not alot changed tbh

shadow notch
glass ocean
#

wydm

#

you can do like

#
package main

import (
    "bufio"
    "fmt"
    "os"
    "regexp"
    "strconv"
)

var afstand int
var tijd int
var Snelheid int

func main() {

    file, ferr := os.Open("DataG.txt")
    if ferr != nil {
        panic(file)

    }

    scanner := bufio.NewScanner(file)
    scanner.Split(bufio.ScanWords)
    
    var nums []int

    for scanner.Scan() {

        re := regexp.MustCompile("[0-9]+")
        re.FindAllString(scanner.Text(), 2)
        strconv.Atoi(scanner.Text())
        if s, err := strconv.Atoi(scanner.Text()); err == nil {
            fmt.Printf("%v \n", s)
            nums = append(nums, s)
        }
    }

}
grave glade
#

that doesn’t compile

glass ocean
#

what am i missing

#

oh