0

Does anyone know this solution. Have been trying to solve it for a long time. Your discussion on it will be extremely helpful 🤗

Filtering Names You are given an array of names and need to output only the names that start with the given letter. The letter should be taken from user input. Each of the resulting names should be output on a separate line. fun main(args: Array<String>) { var letter = readLine()!![0] val names = arrayOf("John", "David", "Amy", "James", "Amanda", "Dave", "Bob", "Billy", "Bobby", "Diana", "Lenny", "Gina") }

9th Apr 2021, 5:37 AM
Fardina Kabir
Fardina Kabir - avatar
4 Antworten
+ 4
Q: How to iterate over an array? A: array.forEach() {do something with each} Q: What's Kotlin name for each of the items? A: it Q: How to get first letter of item? A: it[0] Q: How to print line by line? A: println(it[0]) And befor print, you have to compare it[0] with the input. We saw a comparisation like this yesterday 😉
9th Apr 2021, 8:21 AM
Coding Cat
Coding Cat - avatar
+ 1
fun main(args: Array<String>) { var letter = readLine()!![0] val names = arrayOf("John", "David", "Amy", "James", "Amanda", "Dave", "Bob", "Billy", "Bobby", "Diana", "Lenny", "Gina") names.forEach{ //iterating each element in the array if(it[0]==letter){ //comparing the index of each element with the input println(it) //printing the array } } }
27th May 2022, 1:30 PM
UMASHANKAR KRISHNAMOORTHY
+ 1
Certainly! I can help you with that. To filter the names based on the given letter, you can use the following code: fun main(args: Array<String>) { var letter = readLine()!![0] val names = arrayOf("John", "David", "Amy", "James", "Amanda", "Dave", "Bob", "Billy", "Bobby", "Diana", "Lenny", "Gina") for (name in names) { if (name.startsWith(letter, ignoreCase = true)) { println(name) } } } Here's how this code works: The user input is read using readLine() and stored in the letter variable. We use readLine()!![0] to read only the first character of the input string. The !! is used for non-null assertion since readLine() can return null. The names array contains the list of names. We iterate over each name in the names array using a for loop. https://www.marykayintouch.one/ Inside the loop, we use the startsWith() function to check if the name starts with the given letter. The ignoreCase = true parameter ensures that the comparison is case-insensitive. If a name starts with the given letter, it is printed on a separate line using println(). Now, when you run the program, it will prompt you to enter a letter. After entering the letter, it will filter and print the names that start with that letter.
15th May 2023, 5:34 AM
Sarah Ford
0
It seems like you're working on a Kotlin program to filter and display names that start with a specific letter. To complete the code and achieve your goal, you need to loop through the names array, check if each name starts with the given letter, and then print those names on separate lines. Here's how you can do it: fun main(args: Array<String>) { println("Enter a letter: ") var letter = readLine()!![0].toUpperCase() // Convert the input letter to uppercase for case-insensitive comparison val names = arrayOf("John", "David", "Amy", "James", "Amanda", "Dave", "Bob", "Billy", "Bobby", "Diana", "Lenny", "Gina") println("Names starting with '$letter':") for (name in names) { if (name.startsWith(letter)) { println(name) } } } In this code: We prompt the user to enter a letter. We convert the input letter to uppercase using toUpperCase() to make the comparison case-insensitive. We use a loop to iterate through each name in the names array. Inside the loop, we check if the current name starts with the input letter. If the condition is met, https://www.myjdfaccount.net/ we print the name on a separate line. Feel free to add more error handling and refinement as needed, but this should provide you with a basic working solution to your problem. I hope that you like my answer Tank you I always try to give my best.
14th Aug 2023, 8:23 AM
Widere