Increasing numbers in kotlin language

increasing numbers

Good afternoon friends sorry my english if it is not good because i am using google translator
in a few words … even because in the locality where I am
we don’t have a lot of information about kotlin yet.
so I decided to visit this elegant forum because I believe you can give me this strength.
I’m starting in the Kotlin language and I still don’t know enough …
I build my projects with java … But I think I can build solutions
best using kotlin, jse, jfx and other languages …
I believe in that a lot.
one of my doubts is how to receive data from an arraylist
so that the “Int” numbers are printed with increasing numbers,
from smallest to largest
look what I did, will it be if someone can help me? =)
if by chance someone can also implement the result
so that the name and age appear in a growing way will help me a lot in this new beginning

Blockquote

fun main(){
val nome1: String=“Mike”;
val nome2: String=“valentina”;
val nome3:String=“vicente”;
val nome4: String=“carol”;
//idades
val inome1:Int=29;
val inome2:Int=6;
val inome3:Int=1;
val inome4:Int=23;

val nomes= arrayOf(nome1,nome2,nome3,nome4)
val idades= arrayOf(inome1,inome2,inome3,inome4);

System.out.println("quantidade de cadastro" +nomes.size);

System.out.println("pessoas cadastradas: " +
        "\n nome:" +nome1+" idade "+inome1+
        " \n nome: "+nome2+" idade "+inome2+
        " \n nome: "+nome3+" idade "+inome3+
        "\n nome: "+nome4+" idade "+inome4 );

}

Blockquote
tried to use a loop in for as in java … but it didn’t work

Even though no one has answered yet, I find it interesting to book this study block … confess that kotlin is new think I found a solution for there
my question

I still don’t know if this way of printing the data is correct, because because the intention was to maintain an increasing age, that is, smaller or larger …

However, another question arises, and if you want to print the value differently, for example, an age would be higher or lower, is case would be a decreasing result.

look how a question was posted in the first question

Trunks you friends

val nome1: String="Mike";

val nome2: String=“valentina”;
val nome3:String=“vicente”;
val nome4: String=“carol”;
var inome1:Int=29;
var inome2:Int=6;
var inome3:Int=1;
var inome4:Int=23;

fun main(){

val nomes= arrayOf(nome1,nome2,nome3,nome4)
val idades= arrayOf(inome1,inome2,inome3,inome4);

System.out.println("quantidade de cadastro" +nomes.size);

/*
System.out.println(“pessoas cadastradas: " +
“\n nome:” +nome1+” idade "+inome1+
" \n nome: “+nome2+” idade "+inome2+
" \n nome: “+nome3+” idade "+inome3+
"\n nome: “+nome4+” idade "+inome4 );
*/

for ( valor in 0..100) {
  //  println(valor)
    var verificaiades= when {
        inome1 == valor -> "pai sr."+nome1+" idade "+ valor
        inome2 == valor -> "filho sr."+nome2+" idade "+ valor
        inome3 == valor -> "filho sr."+nome3+" idade "+ valor
        inome4 == valor -> "mae sra."+nome4+" idade "+ valor
        else -> "ainda nao encontrado"
    }
    if (valor==inome1){
         println(verificaiades)
        }
    if (valor==inome2){
        println(verificaiades)
        }
    if (valor==inome3){
        println(verificaiades)
    }
    if (valor==inome4){
        println(verificaiades)
    }

}

}
identar texto pré-formatado por 4 espaços

result:

quantidade de cadastro4
filho sr.vicente idade 1
filho sr.valentina idade 6
mae sra.carol idade 23
pai sr.Mike idade 29

Process finished with exit code 0

I’m afraid people may not be answering as they’re having trouble understanding you. I don’t really follow, either (as my Portuguese is even worse than your English :slight_smile: ), but maybe a better solution is to avoid both repeating variables and parallel arrays, and instead create a simple object representing a person. It could hold their name and other info, e.g.:

data class Person(val nome: String, val inome: Int)

(That’s the right approach in Java, too — but that would be so long-winded that most of the time you wouldn’t bother. In Kotlin it’s much easier to do the right thing!)

You can then create a list of them:

val people = listOf(Person("Mike", 29), Person("valentina", 6) /* … */)

And loop through it:

for (person in people)
    println(" nome: ${person.nome} idade ${person.inome}")

Or map or join them in some other way, e.g.:

println(people.joinToString{ "\n nome: ${it.nome} idade ${it.inome}" })

In general, anything you can do in Java can be done in Kotlin too, though Kotlin often gives much easier and/or more powerful options as well.

There’s lots of info on the web about Kotlin (e.g. the official documentation), but much of it is in English, and I don’t know what’s available in your own language. (And since this is an English-language site, you might not find many others here who do.)

you speak Portuguese? then it would be easier hahaha

… well, I found kotlin very cool …
I will test it the way you showed me, I found it interesting
soon show the results thanks

1 Like