• 14

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

How create operation between to numbers?

In the first I tried to create operation code in kotlin, I mean when run the code in terminal and enter first number 123456 when I press + operator the code read it as string option and it will become 123456+.

The bottom line is: I want to create operator kotlin code can calculating two numbers and when I press + or - or / or * line of the number one must be clean to enter the number two for calculating and so to enter number three and four and five.

And this my code:

fun operation(arr: ArrayList<String?>?) {

    val joo = readLine()!!
    val loo = joo.removeSuffix("+")

    var soo = loo.toInt()

    val voo = joo.lastIndex
        "+" -> arr.last()!!.plus(soo)




I have some time on my hands this morning. This might be a good hello world for someone, so here goes.

To read input and build up state from it, you are going to need:

  • a loop to read each new argument from the user
  • a way of keeping track of the input you have read. This is actually a little state machine. I have implemented mine using an enum and a when expression.

And here it is (probably not exactly what you are looking for, but should give you an idea of possible structure:

import java.util.*

// This gives us the states that our state machine can be in.
enum class State {

fun main() {
    val scanner = Scanner(System.`in`)
    var state = State.WANT_FIRST_OPERAND
    println("Ready to do some maths!")
    var firstOperand = 0.0
    var secondOperand: Double
    var operator = ""
    // This loop will keep asking the user for input and progress through the states of the state machine.
    loop@ while (true) {
        // This when block encapsulates the logic for each state of our state machine.
        when (state) {
            State.WANT_FIRST_OPERAND -> {
                println("Give me your first operand.")
                try {
                    firstOperand = scanner.nextDouble()
                    state = State.WANT_OPERATOR
                } catch (e: Exception) {
                    println("Sorry, that did not work, did you give me a number?")
            State.WANT_OPERATOR -> {
                println("Give me the operator. (+, -, /, *)")
                try {
                    operator = scanner.next("[-*+/]+").trim()
                    state = State.WANT_SECOND_OPERAND
                } catch (e: Exception) {
                    println("Sorry, that did not work.")
            State.WANT_SECOND_OPERAND -> {
                println("Give me your second operand.")
                try {
                    secondOperand = scanner.nextDouble()
                    val answer = when(operator){
                        "+" -> firstOperand + secondOperand
                        "-" -> firstOperand - secondOperand
                        "/" -> firstOperand / secondOperand // You want to do something if second operand is 0 here
                        "*" -> firstOperand * secondOperand
                        else -> {
                            println("Hmmm, something went wrong there I don't know $operator, try again")
                            state = State.WANT_OPERATOR
                    println("The Answer is $answer, lets do another one!")
                    state = State.WANT_FIRST_OPERAND
                } catch (e: Exception) {
                    println("Sorry, that did not work, did you give me a number?")
  • 1
Reply Report
      • 2
    • Thanks you so mach for the answer ,But actually this not exactly what i need ,Look when i add first number i should to press Enter key to add operator key ,this is exactly my issue i wanna would give up Enter key, mean when press some operator key +or- Enter key will Automatically work.
    • @YounTivoli in this case, you do not want "readline" semantics. Because Enter is literally the thing that delimits lines. You would need to implement a far more incremental streamed reader and do evaluation of every new key entered.