Scala pass input data as arguments

0 votes

I am trying to write a program to accept 5 employee IDs and the corresponding names and their salaries from the user and store them in three arrays. Pass these arrays to a function display() as arguments. This display() will display the content of the arrays. So i wrote below code, but it showing some error, could you please look into this.

package com.sundogsoftware.spark


object example1 {

  

  def main(args:Array[String]) {

    val id = new Array[Int](5)

    

    for (i <- 0 to 4) {

      id(i) = scala.io.StdIn.readInt()

    }

    



    

    val name = new Array[String](5)

    

    for (i<-0 to 4){  

    name(i) = scala.io.StdIn.readLine()

    }

    


  val salary = new Array[Int](5)


  for (i<-0 to 4){

    salary(i)= scala.io.StdIn.readInt()

  }

  

  

    

 def display(id:Array[Int],name:Array[String],salary:Array[Int]) = {

      

     id.foreach(println)

     name.foreach(println)

     salary.foreach(println)

      

    }   

  

  

  } 

}
Jun 19, 2019 in Apache Spark by Manasa
2,485 views

1 answer to this question.

0 votes

Please refer to the below code as per your requirement:

object example1 {

  def main(args:Array[String]) {

    val id = new Array[Int](5)

    println("Enter id")

    for (i <- 0 to 4) {

      id(i) = scala.io.StdIn.readInt()

    }

    val name = new Array[String](5)

    println("Enter name")

    for (i<-0 to 4){

    name(i) = scala.io.StdIn.readLine()

    }

  val salary = new Array[Int](5)

  println("Enter salary")

  for (i<-0 to 4){

    salary(i)= scala.io.StdIn.readInt()

  }

  display(id, name, salary)

   def display(id:Array[Int],name:Array[String],salary:Array[Int]) = {

     id.foreach(println)

     name.foreach(println)

     salary.foreach(println)

    }

  }

}


Hope this helps!

To know more about Spark Scala, It's recommended to join Spark training online today.

Thanks!!

answered Jun 19, 2019 by Lisa

Related Questions In Apache Spark

0 votes
1 answer

Spark comparing two big data files using scala

Try this and see if this does ...READ MORE

answered Apr 2, 2019 in Apache Spark by Omkar
• 69,220 points
7,239 views
0 votes
1 answer

Scala: Add user input to array

You can try this:  object printarray { ...READ MORE

answered Jun 19, 2019 in Apache Spark by Dinesha
4,774 views
0 votes
1 answer

Scala join comma delimited file as tables

Dataframe creation commands:​ Now we will register them ...READ MORE

answered Jul 9, 2019 in Apache Spark by Suraj
982 views
+1 vote
1 answer

Scala: CSV file to Save data into HBase

Check the reference code mentioned below: def main(args: ...READ MORE

answered Jul 25, 2019 in Apache Spark by Hari
1,513 views
0 votes
1 answer

Copy all files from local (Windows) to HDFS with Scala code

Please try the following Scala code: import org.apache.hadoop.conf.Configuration import ...READ MORE

answered May 22, 2019 in Apache Spark by Karan
4,231 views
0 votes
1 answer

Doubt in display(id, name, salary) before display function

The statement display(id, name, salary) is written before the display function ...READ MORE

answered Jun 19, 2019 in Apache Spark by Ritu
697 views
0 votes
1 answer

How to calculate the result of formula with Scala?

Hi, You can use a simple mathematical calculation ...READ MORE

answered Jul 1, 2019 in Apache Spark by Gitika
• 65,770 points
1,300 views
0 votes
2 answers

How to execute a function in apache-scala?

Function Definition : def test():Unit{ var a=10 var b=20 var c=a+b } calling ...READ MORE

answered Aug 5, 2020 in Apache Spark by Ramkumar Ramasamy
1,087 views
0 votes
1 answer

At which point of time Scala will take default arguments?

Hi, If we omit an argument in a ...READ MORE

answered Jul 25, 2019 in Apache Spark by Gitika
• 65,770 points
537 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP