0

We have a HashMap Integer/String and in Java we would iterate over the HashMap and display 3 key value pairs at a time with the click of a button. Java Code Below

hm.put(1, "1");
hm.put(2, "Dwight");
hm.put(3, "Lakeside");
hm.put(4, "2");
hm.put(5, "Billy");
hm.put(6, "Georgia");
hm.put(7, "3");
hm.put(8, "Sam");
hm.put(9, "Canton");
hm.put(10, "4");
hm.put(11, "Linda");
hm.put(12, "North Canton");
hm.put(13, "5");
hm.put(14, "Lisa");
hm.put(15, "Phoenix");
onNEXT(null);

public void onNEXT(View view){
    etCity.setText("");
    etName.setText("");
    etID.setText("");

    X = X + 3;

    for(int L = 1; L <= X; L++ ){
        String id = hm.get(L);
        String  name = hm.get(L = L + 1);
        String  city = hm.get(L = L + 1);

        etID.setText(id);
        etName.setText(name);
        etCity.setText(city);

    }

    if(X == hm.size()){
        X = 0;
    }
}

We decoded to let Android Studio convert the above Java Code to Kotlin The converter decide to change the for(int L = 1; L <= X; L++) loop to a while loop which seemed OK at first then we realized the while loop was running for 3 loops with each button click. Also Kotlin complained a lot about these line of code String name = hm.get(L = L + 1); String city = hm.get(L = L + 1);

We will post the Kotlin Code below and ask the question

fun onNEXT(view: View?) {        
    etCity.setText("")
    etName.setText("")
    etID.setText("")

    X = X + 3

    var L = 0

    while (L <= X) {
        val id = hm[L - 2]
        val name = hm.get(L - 1)
        val city = hm.get(L)

        etID.setText(id)
        etName.setText(name)
        etCity.setText(city)

        L++
    }

    if (X == hm.size) {
        X = 0
    }
}

We tried to write a For Next Loop like this for (L in 15 downTo 0 step 1) it seems you can not count upTo so we thought we would use the hm:size for the value 15 and just use downTo

So the questions are

  • How do we use the Kotlin For Next Loop syntax and include the hm:size in the construct?
  • We have L declared as a integer but Kotlin will not let us use L = L + 1 in the While loop nor the For Next Loop WHY ?
  • HERE is the strange part notice we can increment X by using X = X + 3 YES X was declared above as internal var X = 0 as was L the same way
3
  • This seems like a very convoluted solution (and question). Regarding the solution: is there any reason to use a HashMap instead of a list with each element representing the 3 pieces of data you need? Regarding the question: you are asking many things at the same time. In particular, what do you mean by "Kotlin will not let us use L = L + 1 in the While loop nor the For Next Loop WHY ?" What's the error you get? Commented Aug 16, 2018 at 2:41
  • I'd also encourage you to remove all the parts that are unrelated to the question (like the fact that it's Android, or that onNEXT gets a View as param. That way, you'll have a better chance of people responding Commented Aug 16, 2018 at 2:47
  • @marianosimone You are correct in all likely hood we would have a list generated by SQLite. Just wanted to test writing a For Next Loop as for the code L = L + 1 we are informed that arguments are not expressions and only expressions are allowed in this context My guess is we are mixing val and var in the same line of code Thanks for the 2nd comment advice appreciated Commented Aug 16, 2018 at 6:00

3 Answers 3

1

Okay, I'll bite.
The following code will print your triples:

val hm = HashMap<Int, String>()
hm[1] = "1"
hm[2] = "Dwight"
hm[3] = "Lakeside"
hm[4] = "2"
hm[5] = "Billy"
hm[6] = "Georgia"
hm[7] = "3"
hm[8] = "Sam"
hm[9] = "Canton"
hm[10] = "4"
hm[11] = "Linda"
hm[12] = "North Canton"
hm[13] = "5"
hm[14] = "Lisa"
hm[15] = "Phoenix"

for (i in 1..hm.size step 3) {
    println(Triple(hm[i], hm[i + 1], hm[i + 2]))
}

Now let's convert the same idea into a function:

var count = 0
fun nextTriplet(hm: HashMap<Int, String>): Triple<String?, String?, String?> {
    val result = mutableListOf<String?>()
    for (i in 1..3) {
        result += hm[(count++ % hm.size) + 1]
    }
    return Triple(result[0], result[1], result[2])
}
Sign up to request clarification or add additional context in comments.

3 Comments

Great line of code and thinking much appreciated Part of the problem is need to only let the For Next Loop show 3 records in the edit text with each click of the btnNext taming the Loop is on our agenda
WOW I just made the connection with your name. I am only on chapter two. Now how do I get and autographed e-book ha ha
It took a while to wrap my brain around your code I did an EDIT to your answer so others will see how to make this display in the EditText field I knew this answer was correct just needed to own the code on to chapter 3 ha GLAD YOU BIT
1

We used a far from elegant set of code to accomplish an answer to the question.
We used a CharArray since Grendel seemed OK with that concept of and Array

internal var YY = 0
val CharArray = arrayOf(1, "Dwight", "Lakeside",2,"Billy","Georgia",3,"Sam","Canton")

In the onCreate method we loaded the first set of data with a call to onCO(null)

Here is the working code to iterate over the CharArray that was used

    fun onCO(view: View?){

    etCity.setText("")
    etName.setText("")
    etID.setText("")
    if(CharArray.size > YY){
        val id = CharArray[YY]
        val name =  CharArray[YY + 1]
        val city = CharArray[YY + 2]
        etID.setText(id.toString())
        etName.setText(name.toString())
        etCity.setText(city.toString())
        YY = YY + 3
    }else{
        YY = 0
        val id = CharArray[YY]
        val name =  CharArray[YY + 1]
        val city = CharArray[YY + 2]
        etID.setText(id.toString())
        etName.setText(name.toString())
        etCity.setText(city.toString())
        YY = YY + 3
    }

Simple but not elegant. Seems the code is a better example of a counter than iteration.
Controlling the For Next Look may involve less lines of code. Control of the look seemed like the wrong direction. We might try to use the KEY WORD "when" to apply logic to this question busy at the moment

Comments

0

After some further research here is a partial answer to our question This code only show how to traverse a hash map indexing this traverse every 3 records needs to be added to make the code complete. This answer is for anyone who stumbles upon the question. The code and a link to the resource is provide below

fun main(args: Array<String>) {
val map = hashMapOf<String, Int>()
map.put("one", 1)
map.put("two", 2)

for ((key, value) in map) {
    println("key = $key, value = $value")
}
}

The link will let you try Kotlin code examples in your browser LINK

We only did moderate research before asking this question. Our Appoligies. If anyone is starting anew with Kotlin this second link may be of greater value. We seldom find understandable answers in the Android Developers pages. The Kotlin and Android pages are beginner friendlier and not as technical in scope. Enjoy the link Kotlin and Android

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.