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
64 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)

    }

  }

}
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
• 68,840 points
1,141 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
71 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
54 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
213 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
609 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
37 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
• 25,440 points
63 views
0 votes
1 answer

How to execute a function in apache-scala?

Hi, Here is a simple example of how ...READ MORE

answered Jul 1, 2019 in Apache Spark by Gitika
• 25,440 points
56 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
• 25,440 points
35 views
0 votes
1 answer