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 in Apache Spark by Manasa
22 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 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 in Apache Spark by Omkar
• 67,120 points
91 views
0 votes
1 answer

Scala: Add user input to array

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

answered Jun 19 in Apache Spark by Dinesha
19 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 in Apache Spark by Suraj
15 views
0 votes
1 answer

Can I read a CSV represented as a string into Apache Spark?

You can use the following command. This ...READ MORE

answered May 3, 2018 in Apache Spark by kurt_cobain
• 9,240 points
46 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 in Apache Spark by Karan
37 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 in Apache Spark by Ritu
14 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 in Apache Spark by Gitika
• 19,720 points
27 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 in Apache Spark by Gitika
• 19,720 points
23 views
0 votes
1 answer

Multidimensional Array in Scala

Multidimensional array is an array which store ...READ MORE

answered Feb 11 in Apache Spark by Omkar
• 67,120 points
65 views
0 votes
1 answer