Skip to content
Related Articles

Related Articles

Scala Console | println, printf and readLine
  • Last Updated : 10 Apr, 2019

Console implements functions for displaying the stated values on the terminal i.e, with print, println, and printf we can post to the display. It is also utilized in reading values from the Console with the function from scala.io.StdIn. It is even helpful in constructing interactive programs.
Let’s discuss it in detail and also lets see some examples related to it.

  • println:
    It is utilized in putting down values to the Console and moreover computes a trailing newline. we can pass any types as argument to it.
  • Print:
    It is equivalent to println but it does not computes any trailing line. It puts down the data to the beginning of the line.
  • printf:
    This is helpful in writing format strings and also places extra arguments.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Scala program of print
    // functions
      
    // Creating an object
    object GfG
    {
      
        // Main method
        def main(args: Array[String]) 
        {
      
            // Applying console with println
            Console.println("GeeksfoGeeks")
      
            // Displays output with no
            // trailing lines
            print("CS")
            print("_portal")
      
            // Used for a newline
            println()
      
            // Displays format string
            printf("Age = %d", 24)
        }
    }

    chevron_right

    
    

    Output:

    GeeksfoGeeks
    CS_portal
    Age = 24
    

    The println and Console.println both are equivalent.

  • readLine():
    It is a method in which user gives inputs in the pattern of String from the keyboard.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Scala program of readLine()
    // method
      
    // Creating an object
    object GfG
      
    {
    // Main method
    def main(args: Array[String]) 
      
    {
    // Applying a loop
    while (true) {
      
        // Reads the line from the Console
        val result = scala.io.StdIn.readLine()
      
        // Displays the string that is 
        // given by the user
        printf("Enter the String: %s", result)
        
        //prints newline
        println()
         }
       }
    }

    chevron_right

    
    

    Output:

    //giving user defined inputs
    GfG 
    //output by readline
    Enter the String: Gfg 
    //again giving inputs
    Nidhi 
    //output
    Enter the String: Nidhi 
    

    Here, the while loop is infinite in nature and after giving user inputs the variable will contains that (GfG) string and if again we given any input then the variable will contain that (Nidhi) input.




My Personal Notes arrow_drop_up
Recommended Articles
Page :