0

I'm a beginner in scala, without java background. I don't understand the import system. I have my application, where i use the import

import Array._
import List._
import Controller.api
object scalaStart{
    def main(args: Array[String]){
            var apiCtrl = new api()
            apiCtrl.getById(1)
            println(apiCtrl.title)
    }

}

And this is the class:

package Controller
 class api {
  var id:Int
  var title:String
  var description:String
  def getById(id:Int){
    if(id = 1){
      this.id           = 1
      this.title        = s"Title Nummer ${this.id}"
      this.description  = s"Description Nummer ${this.id}" 
    }else{
      this.id           = 1
      this.title        = s"Artikel mit der ID: ${this.id} existiert nicht."
      this.description  = s"Kein Eintrag mit der ID: ${this.id}" 
    }
  }
}

i checked also only import api and import controller and wildcards controller._ controller.api._.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Citrullin
  • 2,269
  • 14
  • 29
  • What's your directory layout? Are you using sbt? – Daenyth Feb 28 '16 at 00:49
  • No, i don't using sbt. GettingStart/src/controller/api.scala and GettingStart/src/GettingStarted/GettingStarted.scala. It's automaticly build with Netbeans and the scala Plugin. – Citrullin Feb 28 '16 at 00:56
  • Upper/lower case can matter in package names on case-sensitive file systems. Idiom is that packages are lower case, classes started with Upper. – som-snytt Feb 28 '16 at 04:23

1 Answers1

1

Philipp, your code actually does not compile. In the line if(id=1){ it should be if(id==1). Try to correct this and rebuild your project. You will notice that compiler will give you another error:

Error:(5, 7) class api needs to be abstract, since: it has 3 unimplemented members. /** As seen from class api, the missing signatures are as follows. * For convenience, these are usable as stub implementations. */ def description_=(x$1: String): Unit = ??? def id_=(x$1: Int): Unit = ??? def title_=(x$1: String): Unit = ??? class api { ^

This is because in Scala you can't leave variable declarations abstract as you could do in Java. Instead of var id:Int you need to put something liek var id:Int = 0 and the same for other declared variables.

Alexander Arendar
  • 3,365
  • 2
  • 26
  • 38
  • Thanks. Big stupid failure. I initialized also the Strings: `var id:Int = 0 var title:String = "" var description:String = "" `Then i compiled the controller package and it works. But i can't use String Interpolation like this: `this.title = s"Title Nummer {this.id}" ` – Citrullin Feb 28 '16 at 16:16
  • You are welcome. For me, after I fixed all these problems it gives me following when I run: `Title Nummer 1` Is it the same for you? – Alexander Arendar Feb 28 '16 at 16:41
  • I got error: ';' expected but string literal found. this.title = s"Title Nummer ${this.id}" I have Scala Version 2.9 – Citrullin Feb 28 '16 at 16:56
  • No. I checked it without the s. I got this result: Title Nummer ${this.id} Need i import something for interpolation? – Citrullin Feb 28 '16 at 17:02
  • Look here: http://stackoverflow.com/questions/7917462/string-interpolation-in-scala Seems like string interpolation introduced in Scala 2.10. So upgrade and you will be lucky :) – Alexander Arendar Feb 28 '16 at 17:03
  • I have to read the doc: Starting in Scala 2.10.0, ... tanks :D – Citrullin Feb 28 '16 at 17:05