Kotlin String to Integer And Calculate - kotlin

I am dealing with the following code
fun main()
{
var exp = "11+2"
println(exp) // first statement
println(exp.toInt()) // Second Statement
}
When I run the above code, My Aim to display the output, as
11+2 // here 11+2 is a String
13 // Here the output I should get is 13 by adding 11+2 during run time, since I am converting the 11+2 to Integer, But I am getting error as
11+2
Exception in thread "main" java.lang.NumberFormatException: For input string: "11+2"
at java.lang.NumberFormatException.forInputString (:-1)
at java.lang.Integer.parseInt (:-1)
at java.lang.Integer.parseInt (:-1)
Can anyone Solve or provide solution to add during run time and get 13 as result.

Till date , it is not possible natively in kotlin.
We have to code manually to evaluate arithmetic in string expressions like above. You can refer this link for reference.

Related

Error in inputting array and outputting it | Kotlin

I am new at Kotlin (and my English is terrible :).
I want to define the size of the array and elements in it by inputting it from the keyboard.
fun main() {
val array_size = readLine()!!.toInt()
val arr = IntArray(array_size)
for (i in 0..array_size){
arr[i] = readLine()!!.toInt()
}
for(i in 0..array_size){
println(arr[i])
}
}
[I got this message][1]
[1]: https://i.stack.imgur.com/DRk9F.png
This is my first question in StackOverFlow tho, hope it is understandable for those who want to help.
The NullPointerException is probably because the call to readLine() is returning null and then you're forcing that to non-null using readLine()!! which gives you the NPE.
In recent versions of Kotlin a new method was introduced: readln(). It is recommended to use that instead of the old readLine. If and END OF FILE is found, the new readln method will throw a more descriptive exception, whereas readLine will return null which makes it more difficult to see where you went wrong.
You might get an end-of-file condition if the input is redirected from a file or other source. This often happens if you run your program in an IDE or an online compiler service. If you run your program from the command line, it will work, until you get to enter the last line. This is because for(i in 0..array_size) includes the value array_size which is 1 more than the last index, so you get an out-of-bounds exception.
Instead of using 0..(array_size - 1), it is recommended to use arr.indices which gives you the range of valid indices for the array.
readLine() is returning null, and so when you do readLine!!... you're getting a NullPointerException. Perhaps you want to use readln instead.

convert data type from org.apache.spark.sql.Column to Long in spark window functions

I am performing an operation in scala dataframe using window functions.
But I am getting an error as shown below while processing.
error: type mismatch;
found : org.apache.spark.sql.Column
required: Long
val w4 = Window.partitionBy("ID").orderBy("date").rowsBetween((lit(Window.currentRow)-col("null_count")), Window.currentRow)
Below provided part is causing the issue, rowsBetween function is expecting Long data type as start and end limit parameter.
rowsBetween((lit(Window.currentRow)-col("null_count")), Window.currentRow)
Since the window upper limit is not static, it has to take value from another column (null_count). Is there any way to resolve this issue ?
Any Leads Appreciated !

Error: Kotlin: The floating-point literal does not conform to the expected type Float

I was making a simple maths calculator in kotlin, an error appeared on my screen when I tried to initialize the value of one of the variables used as 0.00 for float integer.
var x:Float= readLine()!!.toFloat()
var y:Float= readLine()!!.toFloat()
var sum:Float=0.00// the error message is showcased in this line
sum=x+y
println("Addition " + sum)
This is a key difference between Java and Kotlin. Kotlin does not do numeric type promotion like Java does. The comments to your question are showing you how to deal with this, by either matching up the two types Double and/or Float to begin with, or by explicitly converting one or the other so that the two types match up.
Your problems goes away if you make use of Kotlin's ability to infer variable types by taking the type specifications off of your variable definitions. The fact that Kotlin infers types is one reason it does not promote numeric types. Mixing the two would lead to a lot of confusion.
Here's an example of how to fix and simplify your code's type mismatch issues using type inference:
var x = readLine()!!.toFloat()
var y = readLine()!!.toFloat()
var sum = x + y
println("Addition " + sum)
I understand that this may be just test code that you're using to understand Kotlin better. With that said, I'll point out that this code will crash if your user types in non-numeric input. You could fix this by putting a try/catch around your input lines, and providing an nice error message. You might want to put each input in a loop, continuing to ask for an input until the user does provide a response that is of the expected format.

adding to mule payload

I need to add a number to one of the payload parameters. If the payload.zone parameter is 45 and embed this #[payload.zone + 1] I get 451. Rather I want the result to be 46. What expression will allow that?
Thanks!
As I can see zone is a String object. For that reason, you are getting 451. To get the expected value you can convert to integer
#[Integer.parseInt(payload.zone) + 1]

WxWidgets using wxString

I'm taking a computer sciences class and we have to make a calculator using a GUI. I have been banging my head into a wall trying to even get input though.
Understand how to use pointers and GetValue(), to take in the input as a wxString but that does me no good, If I can't get the string as a double or integer then I can't perform operations on it.
Does anyone know how to convert wxString to Double? or even int?
Thanks in Advance
For this purpose, there are the two functions ToDouble and ToLong.
For details take a look at the offical documentation:
http://docs.wxwidgets.org/stable/wx_wxstring.html#wxstringtolong
http://docs.wxwidgets.org/stable/wx_wxstring.html#wxstringtodouble
EDIT: Example:
wxTextCtrl ctrl;
// user has entered a number
double number;
if( !ctrl.GetValue().ToDouble( &number ) )
// handle error
else
// continue...
Please note: It will only work if you enter a number. If you enter a term like 2+3 the function should return false. In this case you have to split the string up and interpret all numbers seperately.