Why is string template from readLine not working in Kotlin? [closed] - kotlin

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
My code:
fun main() {
println("What's your name?")
val name= readLine()
println("Hello $name")
}
Output:
What's your name?
Abhas
Hello
My question is: Why $name is not getting printed? After taking input ' Abhas ' it doesn't show anything and then after I pressed Enter and it prints ' Hello ' only.

I'm not sure why it's not working for you - it is reading some input or it wouldn't get to the println statement, and it is printing the value of name (which has a value, otherwise it would say null).
At a guess, it's something to do with the way you're providing input, where the first line it reads is empty, so you get an empty string (""). You said your output is like this:
What's your name?
Abhas
Hello
Are you hitting enter/return to get to the next line before you type Abhas? That does nothing in the REPL I'm using, but it might count as an (empty) input line in your console

Related

What does ++num1var[num2var] mean? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
Page 109 of The AWK Programming Language book has a statement to create a 2-D array named attr:
attr[nrel, $1] = ++nattr[nrel]
nrel = an integer representing the number of relations (tables)
nattr = an integer representing the number of attributes (table columns)
Substituting country for $1, 1 for nrel, and 1 for nattr we have:
attr[1, country] = 1[1]
What does the right-hand side of that statement mean? It appears to be referencing subscript 1 of array 1. Can an array be named 1? Would you explain what that expression means, please?
Page 109(...)
I have look into archive.org's version and line
attr[nrel, $1] = ++nattr[nrel]
is sole line referencing nattr and therefore this is where nattr is created, as you are asking about value under key it will be array. You might check that by substituting all but nattr as proposed and using typeof function
awk 'BEGIN{attr[1, "France"] = ++nattr[1];print typeof(nattr)}' emptyfile
gives output
array
therefore shown line does firstly increase value in array nattr under key nrel and then assign such changed value to array attr under key nrel, $1.
(tested in gawk 4.2.1)

Kotlin extract letters and keep only numbers in a String [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a string that is "M456456" for example, and i need to keep only the numbers. So the ouput has to be "456456"
How can i achieve this in Kotlin?
"M456456".filter(Char::isDigit)
"M456456".filter {it in '0'..'9'}
Use a regex replacement and remove all non digit characters:
val regex = """[^0-9]""".toRegex()
val input = "M456456"
val output = regex.replace(input, "")
println(output) // 456456

How to name boolean variable that indicates whether to do something? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
As far I as my experience tells me, boolean variables usually named in format - is/has+noun/adj/verb, e.g isValid, isButton, hasClickedLink
But say there is the case where we have some flag which straightly tells whether to do something, like for example, clean to indicate that cleanup function should be automatically called in the end.
How to name such a booleans? Same clean - is ambiguous, looks like a method name more, but naming it toClean is, I don't know, too weird. Or should I name it like callCleanup?
Thanks in advance!
In this case i usually append the word wanted, which makes cleanWanted. In general, for boolean variables I also prefer to always let the last word be an adjective. This makes it very clear that it represents a truth value. The is/has prefix is often superfluous, as in hasClickedLink which is more concisely communicated with linkClicked.
methods are usually one word adjectives with a capitol at the start
maybe create a method that sets the flag
for example
void Clean(){
clean = True;
}

How to idiomatically format .apply{} in Kotlin? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
This might seem a little trivial, but since we read more code than we write, I want to know which of these versions looks nicer and more readable for you
private val VALUES by lazy {
mutableListOf<Value>().apply {
add(VALUE_1)
add(VALUE_2)
add(VALUE_3)
}
}
OR
private val VALUES by lazy {
mutableListOf<Value>()
.apply {
add(VALUE_1)
add(VALUE_2)
add(VALUE_3)
}
}
In other words, should we care that the method (.apply) be on the same line as the caller, or the ending curly bracket to be aligned with the (.apply) method?
As per https://kotlinlang.org/docs/reference/coding-conventions.html
Chained call wrapping
When wrapping chained calls, put the . character or the ?. operator on the next line, with a single indent:
val anchor = owner
?.firstChild!!
.siblings(forward = true)
.dropWhile { it is PsiComment || it is PsiWhiteSpace }
The first call in the chain usually should have a line break before it, but it's OK to omit it if the code makes more sense that way.
So it's up to you :)

Convert UID null-termintated string binary to hex [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Here is the question which is Re Phrased.
Here is the raw binary data, hex encoded, which i need as a output:
040000008200E00074C5B7101A82E0080000000000000000000000000000000000000000310000007643616C2D5569640100000033353335324538372D343338462D343444362D413432462D37393942423334313033333800
I can extract some part of the raw data i.e.(040000008200E00074C5B7101A82E0080000000000000000000000000000000000000000310000007643616C2D55696401000000) from the object which i am getting from the micro soft outlook.
The Rest of the part that is
33353335324538372D343338462D343444362D413432462D373939424233343130333338 is the conversion of the UID : 373D06E9-587E-4930-B846-12500FF1AC2F.
So My question here is how to convert the above UID i.e 373D06E9-587E-4930-B846-12500FF1AC2F to this format 33353335324538372D343338462D343444362D413432462D373939424233343130333338 using objective C or cocoa.
Thanks in Advance.
You should look here:
https://developer.apple.com/library/mac/documentation/corefoundation/Reference/CFUUIDRef/Reference/reference.html#//apple_ref/c/func/CFUUIDCreateFromString
CFUUIDCreateFromString() appears to be the API you are looking for. Assuming that CFUUID is the "binary format" you are searching for.
If you then want "raw bytes", look at CFUUIDGetUUIDBytes()