Issue
I have this program, I've been working on it a while; I recently changed the programs layout to include two classes instead of just one, originally, I had all my code within the MainActivity class, now I have split the program between two classes, MainActvitiy and ReverseString.
Since I've done this, the program output no longer shows on the button press. It still seems to work just fine, I don't get errors and the program doesn't crash. I assume there is a logical error somewhere in the code but I can't find it.
Here's my two classes.
MainActivity.kt
package com.example.stringreversal
// program imports
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
class MainActivity : AppCompatActivity() {
private val myVariable = ReverseString()
@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// defines button and locates it in activity_main.xml
val button = findViewById<Button>(R.id.useButton)
// sets an OnClickListener to the button, runs code below when clicked / pressed
button.setOnClickListener {
// defines UI elements and locates them in activity_main.xml
val userInput = findViewById<EditText>(R.id.userInput).text.toString()
val exclusion = findViewById<EditText>(R.id.restrictedChars).text.toString()
val output = myVariable.reverse(userInput, exclusion)
val programOutput = findViewById<TextView>(R.id.programOutput)
programOutput.text = output
}
}
}
ReverseString.kt
package com.example.stringreversal
class ReverseString {
fun reverse(userInput: String, exclusion: String): String {
// splits user inputted string by whitespace; gets size of the returned list; defines wordPointer as 0
val wordsInString = userInput.split(" ")
val wordsSize = wordsInString.size
var wordPointer = 0
val list = arrayListOf<String>()
// while wordPointer is less than wordsSize...
while (wordPointer < wordsSize) {
// defines the current word in the string by locating the index at [wordPointer]
val currentWord = wordsInString[wordPointer]
// defines a character array to store the current word, split into characters
val charArray = currentWord.toCharArray()
// defines two more pointers, one starting at index [0] and one starting at the last index
var charPointerOne = 0
var charPointerTwo = currentWord.length - 1
// while charPointerOne is less than charPointerTwo
while (charPointerOne < charPointerTwo) {
// if the currently selected character by charPointerOne is included in the exclusion characters...
if (exclusion.contains(charArray[charPointerOne])) {
// move charPointerOne one place forward
charPointerOne++
// else if the currently selected character by charPointerTwo is included in the exclusion characters...
} else if (exclusion.contains(charArray[charPointerTwo])) {
// move charPointerTwo one place backwards
charPointerTwo--
// else, if both conditions are not true, do...
} else {
// defines the two characters to switch by charPointerOne and charPointerTwo
val charToSwitchOne = charArray[charPointerOne]
val charToSwitchTwo = charArray[charPointerTwo]
// swaps these characters with each other
charArray[charPointerOne] = charToSwitchTwo
charArray[charPointerTwo] = charToSwitchOne
// moves charPointerOne and charPointerTwo one place forward and one place backwards respectively
charPointerOne++
charPointerTwo--
}
}
val result = String(charArray)
list.add(result)
// increase the wordPointer by one so the program moves onto the next word in the string
wordPointer++
// assigns each character array to a variable, converted into a string
}
val string = ""
for (i in list) {
string.plus(i)
}
return string
}
}
Solution
The problem is here at the end of your utility function:
val string = ""
for (i in list) {
string.plus(i)
}
return string
Strings are immutable. The function string.plus()
does not modify the original String. It returns a brand new, different String. Since you are calling string.plus
without assigning the result to anything, you are throwing away all these results. The variable string
is still pointing at the original string ""
and that's what your function is returning.
To fix your code, you need to make the variable a var
and reassign the value each time you want to change it:
var string = ""
for (i in list) {
string = string.plus(i)
// or string += i
}
return string
If you are building up strings repeatedly like this, it's better to use the mutable alternative, StringBuilder so you aren't allocating and copying all the intermediate Strings:
val string = StringBuilder("")
for (i in list)
string.append(i)
}
return string.toString()
There is a shorthand way of using StringBuilder like this:
return buildString {
for (i in list) {
append(i)
}
}
But the simplest of all if you are just combining a collection/Iterable of Strings is to use joinToString
:
return list.joinToString("")
I didn't check anything else in your program for other errors, but this is what is causing empty output.
Answered By - Tenfour04
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.