I'm trying to adapt a simple example of tflite and android.
val inputArray = arrayOf(initInputArray(bitmap))
val outputMap = initOutputMap(getInterpreter())
getInterpreter().runForMultipleInputsOutputs(inputArray, outputMap)
Since my model only has one output. I change the outputMap into an outputArray
val inputArray = arrayOf(initInputArray(bitmap))
val outputArray = Array(1){ Array(height) { Array(width) { FloatArray( channels }}}
getInterpreter().run(inputArray, outputArray)
However when feeding it to the inference method I get:
DataType error: cannot resolve DataType of [Ljava.nio.ByteBuffer;
Any idea what's going wrong?
Found the error:
val inputArray = arrayOf(initInputArray(bitmap))
was the culprit. When using run instead of runForMultipleInputsOutputs this cannot be an array! So changing the line to
val inputArray = initInputArray(bitmap)
fixes the issue.
A good documentation of the required data formats would have helped...
run -> requires "DirectByteBuffer" as input
runForMultipleInputsOutputs -> requires "ByteBuffer" as input
Related
while building a simple currency converter app in android kotlin I got the following error at * operator ? what could be reason for the error.
You cannot use round() function with nullable Any and Float variables. Also you need to convert Any to Float.
Try to convert them with this example:
var rate : Any? = 5
var fromAmount : Float? = 3.5f
val result = round(rate!!.toString().toFloat() * fromAmount!!)
Seems that rate variable is nullable (float? I suppose), while multiplying operation work with non null values only.
Also smart cast wasn't used here, so rate variable seems to be changeable. So I recommend to make it unchangeable (val variable), or use buffer variable like following:
val bufferRate = rate
if(bufferRate == null) {
...
} else {
val convertedCurrency = fromAmount * bufferRate //here `bufferRate` should be smart casted to non nullable type
}
I am trying to find the proper syntax to initialize "Map<,<List<List>>>" in kotlin. I am a bit new to kotlin. I have variables initialized in my class as this:
var jitterMs: Double = 0.0
The way I am trying to initialize is this:
val bandMap: Map<Int,<List<List<String>>>> = emptyMap()
It gives me an error that a proper getter or setter is expected, and I think that's because it just doesn't understand what I am trying to do. What is wrong with this syntax?
Thanks,
Edit: As Tenfour pointed out, I actually want a mutable map. This is what I have now as an error saying there is a type error:
val bandMap: MutableMap<Int,List<List<String>>> = emptyMap()
This is how you would do it:
val bandMap: Map<Int, List<List<String>>> = emptyMap()
For a MutableMap write the following:
val bandMap: MutableMap<Int, List<List<String>>> = mutableMapOf()
Or, for the most idiomatic way to make a mutableMap would be:
val bandMap = mutableMapOf<Int, List<List<String>>>()
val bandMap: Map<Int,List<List<String>>> = emptyMap()
This will work. You added extra < >
There are a lot of similar questions, but I still can't find out right answer.
In Jawa square brackets works i think, but in Kotlin?
data class source (
val npk : Int = 0,
val name : String = "",
val coa : String = ""
)
fun main() {
var sourceList : MutableList<source> = mutableListOf(source(1, "Uno", "one"),
source(2, "Dues", "two"),
source(3, "Tres", "three"))
sourceList.forEach { source -> println(source.name)} // haw to use variable instead "name"?
val variable = "name"
// sourceList.forEach { source -> println(source.$variable)} Is there construction like this possible in KOTLIN?
Without code changes on your class it's only possible via reflection API. Not usually recommended as it can be slower, and is more error prone.
See this answer for an example on how reflection can be used to achieve that : https://stackoverflow.com/a/35539628/3801327
I'd recommend you to go with the solution that CommonsWare suggested in the comment ( adding get operator ) instead
I am facing an issue where I need to do some calculations with a number like for example 5000,00 multiplied it by (1,025^3).
So in this case 5000,00 * (1,025^3) = 5385,45
So my question is, how can I format the number 5385,45 to be like 5.385,45 using decimal format maybe?
I tried by myself and I did this piece of code that outputs 5385,45 in the app but not 5.385,45
var interestValue = (5000,00*(Math.pow(1.025,yearValue)))
val number = java.lang.Double.valueOf(interestValue)
val dec = DecimalFormat("#,00")
val credits = dec.format(number)
vValueInterest.text = credits
This is the format you need:
val dec = DecimalFormat("#,###.##")
will print:
5.384,45
if you need always exactly 2 digits after the decimal point:
val dec = DecimalFormat("#,###.00")
val num = 1.34567
val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.CEILING
println(df.format(num))
When you run the program, the output will be:
1.34
Check:
https://www.programiz.com/kotlin-programming/examples/round-number-decimal
The "most Kotlin-esque" way I found to do this sort of formatting is:
"%,.2f".format(Locale.GERMAN, 1234.5678) // => "1.234,57"
"%,.2f".format(Locale.ENGLISH, 1234.5678) // => "1,234.57"
"%,.2f".format(1234.5678) // => "1,234.57" for me, in en_AU
Note though that even though this is Kotlin's own extension method on String, it still only works on the JVM.
For those looking for a multiplatform implementation (as I was), mp_stools is one option.
Used:
%.numberf
fun main(args: Array<String>) {
var A: Double
A = readLine()!!.toDouble()
var bla = A*A
var calculator = 3.14159 * bla
println("A=%.4f".format(calculator))
}
Try val dec = DecimalFormat("#.###,00"). For examples of DecimalFormat check this link.
I'm trying to convert a HashMap of elements into a JSON string. I'm using the method used in this link.
val elementsNew: HashMap<String, Element> = HashMap(elements)
val type = Types.newParameterizedType(Map::class.java, String::class.java, Element::class.java)
var json: String = builder.adapter(type).toJson(elementsNew)
But this gives the following error
Error:(236, 40) Type inference failed: Not enough information to infer
parameter T in fun adapter(p0: Type!): JsonAdapter!
Please specify it explicitly.
Can any one tell me where's the fault? Is it because of Kotlin?
Looking at the signature of the adapter() method, it can't infer its type parameter from the argument:
public <T> JsonAdapter<T> adapter(Type type)
Hence you have to provide the type explicitly:
var json = builder.adapter<Map<String, Element>>(type).toJson(elementsNew)
or alternatively:
val adapter: JsonAdapter<Map<String, Element>> = builder.adapter(type)
var json = adapter.toJson(elementsNew)