Kotlin - Problems with while()

Migaloco

I was making a calculator in Kotlin and I'm having trouble solving an issue that I'm having with while().On this particular part of the code, I'm trying to find the first operator in the equation, but I need to exclude the ones that indicate whether a number is negative - (or positive +, optional), which need to be indicated between parentheses like so: (-5)

var charay = charArrayOf('+', '-', '*', '/')

var op = 0

var reference = 0

var bol = false

while( bol == false && op != -1){

    println(op)
    println(bol)
    println(bol == false && op != -1)

    op = input.indexOfAny(charay, reference)

    if (!input.get(op - 1).equals('(')){

        bol = true

    }else{

        reference = op + 1
    }

    println(op)
    println(bol)
    println(bol == false && op != -1)
}

To test a normal equation I entered the equation 4+4 and the console looks like this:

0
false
true
1
true
false
0
false
true
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: 
String index out of range: -2
    at java.lang.String.charAt(String.java:658)
    at CalculatorKt.CalculateValue(Calculator.kt:67)
    at CalculatorKt.CalculateValue(Calculator.kt:108)
    at CalculatorKt.main(Calculator.kt:119)

Like I suspected, for some reason, the variables reset at the end of the while(), which is the reason why it never leaves said while(). Can anyone tell me why?

Andy Ray

Read the error. You're trying to read the character of a string at an index that doesn't exist with this statement:

input.get(op - 1)

You need to check what op is first to make sure it is found. indexOfAny returns -1 if not found in the string. Because we can't see what charay is, we can't help you further.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Kotlin build problems with Android

From Dev

&& and || in while loop problems

From Dev

Problems with "While" of SQL Server

From Dev

Regex problems while submitting

From Dev

Problems while plotting on Matplotlib

From Dev

Problems while importing pyaudio

From Dev

Problems with while loop on XPathNodeIterator

From Dev

&& and || in while loop problems

From Dev

kotlin: some problems with arrays in annotations

From Dev

Problems while installing pg gem

From Dev

Rendering Problems while changing the theme

From Dev

Problems with basic while loop in Python

From Dev

Problems while importing an image asset

From Dev

alignment problems in php while loop

From Dev

Problems while installing pg gem

From Dev

Problems while installing Google chrome

From Dev

Permission problems while copying files

From Dev

Problems with usage of while statement in R

From Dev

Encoding problems while extracting NSDictionary

From Dev

git merging problems while pulling

From Dev

Problems with variable changes in while loop

From Dev

Kotlin - NoSuchMethodError while instantiating Throwable

From Dev

Problems while extractiong association rules with Orange?

From Java

Problems with Java Generics while building a monad

From Dev

Problems while creating bigquery table schema

From Dev

Problems with encoding while parsing html document with lxml

From Dev

facing problems while linking thumbnails to modals

From Dev

Encounter problems while compiling kerTeX on Mac OSX

From Dev

Problems while switching activity using intent