Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Check whether input variable is Int in Scala?

Tags:

scala

Hi I am reading through input variables from "args" and I want to check whether the input is integer value. I followed this link

    var param=0
    ...
    args(j) match {
                    ...
                    case args(j): Int => param =args(j)
                    ...
            }

but it gives me an error:

[error] '=>' expected but ':' found.
[error]            case args(j): Int => param =args(j)

Can't figure out what is the problem!

like image 760
Rubbic Avatar asked May 18 '15 23:05

Rubbic


People also ask

How do you check if a value is an integer?

The Number. isInteger() method returns true if a value is an integer of the datatype Number. Otherwise it returns false .

How do you find the type of a variable in Scala?

Use the getClass Method in Scala The getClass method in Scala is used to get the class of the Scala object. We can use this method to get the type of a variable.

How do you know if its string or integer?

We can use the isdigit() function to check if the string is an integer or not in Python. The isdigit() method returns True if all characters in a string are digits. Otherwise, it returns False.

How do you check if a string is a number in Scala?

Using Character.isDigit method returns true if the character is a number.

How to check if a string is a number in Scala?

In this tutorial, we’ll see a few different solutions to check if a given string is a number using Scala. 2. Using Character.isDigit Checking if a String is a number is trickier than it sounds. If we’re sure there’s no decimal part, then we can leverage the Char.isDigit method: The Char.isDigit method returns true if the character is a number.

How to get the type of a variable in Scala?

The getClass method in Scala is used to get the class of the Scala object. We can use this method to get the type of a variable. The output above shows that it prints java.lang.String when it comes to strings because Scala string is nothing but a wrapper around java.lang.String.

How to store integers and decimals in Scala?

Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables. Scala has a different syntax for declaring variables. They can be defined as value, i.e., constant or a variable. Here, myVar is declared using the keyword var.

How to define immutable variables in Scala?

Immutable variables are defined by using the val keyword. The first letter of data type should be in capital letter because in Scala data type is treated as objects. Here, a name is the name of the variable, a string is the data type of variable and geeksforgeeks is the value that store in the memory. Another way of defining variable:


1 Answers

val isInteger = Try(args(j).toInt).isSuccess
like image 110
triggerNZ Avatar answered Sep 29 '22 21:09

triggerNZ