I built a calculator with Kotlin, but I have a problem when I want to logarithm and get ln
I get the following error when I run my program and want to get my logarithm
invalid Double :
this my code
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="70dp"
android:textSize="30sp"
android:textColor="@color/black"
android:gravity="start"
android:maxLines="1"/>
<TextView
android:id="@+id/tvExpress"
android:layout_width="match_parent"
android:layout_height="90dp"
android:textSize="50sp"
android:textColor="@color/black"
android:gravity="start"
android:maxLines="1"/>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardCornerRadius="4dp"
app:cardElevation="2dp"
app:cardBackgroundColor="@color/black">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="7"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="4"
android:orientation="horizontal">
<TextView
android:id="@+id/tvClear"
style="@style/ButtonConverter"
android:text="AC" />
<TextView
android:id="@+id/tvBackspace"
style="@style/ButtonConverter"
android:text="C" />
<TextView
android:id="@+id/tvOpen"
style="@style/ButtonConverter"
android:text="(" />
<TextView
android:id="@+id/tvClose"
style="@style/ButtonConverter"
android:text=")" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="5"
android:orientation="horizontal">
<TextView
android:id="@+id/tvSinus"
style="@style/ButtonConverter"
android:text="Sin" />
<TextView
android:id="@+id/tvCosine"
style="@style/ButtonConverter"
android:text="Cos" />
<TextView
android:id="@+id/tvTangent"
style="@style/ButtonConverter"
android:text="Tan" />
<TextView
android:id="@+id/tvLog"
style="@style/ButtonConverter"
android:text="Log" />
<TextView
android:id="@+id/tvLn"
style="@style/ButtonConverter"
android:text="ln" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="5"
android:orientation="horizontal">
<TextView
android:id="@+id/tvSquared"
style="@style/ButtonConverter"
android:text="x!" />
<TextView
android:id="@+id/tvSquare"
style="@style/ButtonConverter"
android:text="x²" />
<TextView
android:id="@+id/tvRadical"
style="@style/ButtonConverter"
android:text="√" />
<TextView
android:id="@+id/tvInv"
style="@style/ButtonConverter"
android:text="1/x" />
<TextView
android:id="@+id/tvDivide"
style="@style/ButtonConverter"
android:text="÷" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="4"
android:orientation="horizontal">
<TextView
android:id="@+id/tvSeven"
style="@style/ButtonNumber"
android:text="7" />
<TextView
android:id="@+id/tvEight"
style="@style/ButtonNumber"
android:text="8" />
<TextView
android:id="@+id/tvNine"
style="@style/ButtonNumber"
android:text="9" />
<TextView
android:id="@+id/tvMul"
style="@style/ButtonConverter"
android:text="*" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="4"
android:orientation="horizontal">
<TextView
android:id="@+id/tvFour"
style="@style/ButtonNumber"
android:text="4" />
<TextView
android:id="@+id/tvFive"
style="@style/ButtonNumber"
android:text="5" />
<TextView
android:id="@+id/tvSix"
style="@style/ButtonNumber"
android:text="6" />
<TextView
android:id="@+id/tvMinus"
style="@style/ButtonConverter"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="4"
android:orientation="horizontal">
<TextView
android:id="@+id/tvOne"
style="@style/ButtonNumber"
android:text="1" />
<TextView
android:id="@+id/tvTwo"
style="@style/ButtonNumber"
android:text="2" />
<TextView
android:id="@+id/tvThree"
style="@style/ButtonNumber"
android:text="3" />
<TextView
android:id="@+id/tvPlus"
style="@style/ButtonConverter"
android:text="+" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/tvP"
style="@style/ButtonNumber"
android:text="π" />
<TextView
android:id="@+id/tvZero"
style="@style/ButtonNumber"
android:text="0" />
<TextView
android:id="@+id/tvDot"
style="@style/ButtonNumber"
android:text="." />
<TextView
android:id="@+id/tvEquals"
style="@style/ButtonConverter"
android:text="=" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
and
MainActivity.kt
class MainActivity : AppCompatActivity() {
@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//numbers
tvZero.setOnClickListener { appOnExpression("0" , true) }
tvOne.setOnClickListener { appOnExpression("1" , true) }
tvTwo.setOnClickListener { appOnExpression("2" , true) }
tvThree.setOnClickListener { appOnExpression("3" , true) }
tvFour.setOnClickListener { appOnExpression("4" , true) }
tvFive.setOnClickListener { appOnExpression("5" , true) }
tvSix.setOnClickListener { appOnExpression("6" , true) }
tvSeven.setOnClickListener { appOnExpression("7" , true) }
tvEight.setOnClickListener { appOnExpression("8" , true) }
tvNine.setOnClickListener { appOnExpression("9" , true) }
tvDot.setOnClickListener { appOnExpression("." , true) }
//unit converter
tvPlus.setOnClickListener { appOnExpression("+" , false) }
tvMinus.setOnClickListener { appOnExpression("-" , false) }
tvMul.setOnClickListener { appOnExpression("*" , false) }
tvDivide.setOnClickListener { appOnExpression("/" , false) }
tvP.setOnClickListener { appOnExpression("π" , false) }
tvP.setOnClickListener {
if (tvExpress.text.toString().isEmpty()) {
tvExpress.text = tvExpress.text.toString()+3.14
}else {
tvExpress.text = "3.14"
}
}
tvSinus.setOnClickListener {
tvExpress.text = tvExpress.text.toString()+"sin"
}
tvCosine.setOnClickListener {
tvExpress.text = tvExpress.text.toString()+"cos"
}
tvTangent.setOnClickListener {
tvExpress.text = tvExpress.text.toString()+"tan"
}
tvLog.setOnClickListener {
tvExpress.text = tvExpress.text.toString()+"log"
}
tvLn.setOnClickListener {
tvExpress.text = "ln"
}
tvInv.setOnClickListener {
tvExpress.text = tvExpress.text.toString()+"^"+"(-1)"
}
tvSquare.setOnClickListener {
if (tvExpress.text.toString().isEmpty()) {
Toast.makeText(applicationContext , "عدد مورد نظر را وارد کنید" , Toast.LENGTH_LONG).show()
}else {
val d = tvExpress.text.toString().toLong()
val square = d * d
tvExpress.text = square.toString()
tvResult.text = "$d²"
}
}
tvSquared.setOnClickListener {
if (tvExpress.text.toString().isEmpty()) {
Toast.makeText(applicationContext , "لطفا عدد مورد نظر خود را وارد کنید" , Toast.LENGTH_LONG).show()
}else {
fun factorial(n: Int) : Int {
return if (n == 0 || n == 1) 1 else n * factorial(n - 1)
}
val value : Int = tvExpress.text.toString().toInt()
val fact : Int = factorial(value)
tvExpress.text = fact.toString()
tvResult.text = "$value'!"
}
}
tvRadical.setOnClickListener {
if (tvExpress.text.toString().isNotEmpty()) {
Toast.makeText(applicationContext , "عدد مورد نظر را وارد کنید" , Toast.LENGTH_LONG).show()
}else {
val str = tvExpress.text.toString()
val r = sqrt(str.toDouble())
val unit = r.toLong()
val result = unit.toString()
tvExpress.text = result
}
}
tvBackspace.setOnClickListener {
val strNumber = tvExpress.text.toString()
if (strNumber.isNotEmpty()) {
tvExpress.text = strNumber.substring(0, strNumber.length - 1)
}
}
tvClear.setOnClickListener {
tvResult.text = ""
tvExpress.text = ""
}
tvEquals.setOnClickListener {
val express = ExpressionBuilder(tvExpress.text.toString()).build()
val result = express.evaluate()
val longResult = result.toLong()
if (result == longResult.toDouble()) {
try {
tvExpress.text = longResult.toString()
}catch (e: Exception) {
Log.e("check" , e.message.toString())
}
}else {
tvExpress.text = result.toString()
}
}
}
private fun appOnExpression(str: String , bool: Boolean) {
if (tvResult.text.isNotEmpty()) {
tvExpress.text = ""
}
if (bool) {
tvResult.text = ""
tvExpress.append(str)
}else {
tvExpress.append(tvResult.text)
tvExpress.append(str)
tvResult.text = ""
}
}
}
I used this library
dependencies {
implementation 'net.objecthunter:exp4j:0.4.8'
}
How can I define a value so that logarithm and ln are received in my calculator and I can get the value I enter in the calculator?