-1

Here is the code. n continually outputs 50 and not 2:

import java.util.*
    fun main(args: Array<String>) {
        val scanner = Scanner(System.`in`)
        val n = scanner.next().first().toInt()
        val array1 = readLine()!!.split(" ").map { it.toInt() }
        var product:Int=0
        println(n)
        println(array1[0])
        println(array1[1])
        if (n ==2) {
            product = array1[0] * array1[1]
        }
        println(product)
    }

Sample Input:

2
5 3

Output:

2
5 3
50
5
3
0

How do I use scanner in kotlin to read in 2 lines?

Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
  • newbie here. here is the code and n continually outputs 50 and not 2 import java.util.* fun main(args: Array) { val scanner = Scanner(System.`in`) val n = scanner.next().first().toInt() val array1 = readLine()!!.split(" ").map { it.toInt() } var product:Int=0 println(n) println(array1[0]) println(array1[1]) if (n ==2) { product = array1[0] * array1[1] } println(product) } – JerryG1112 Jul 08 '20 at 09:12
  • 1
    Does this answer your question? [Reading console input in Kotlin](https://stackoverflow.com/questions/41283393/reading-console-input-in-kotlin) – deHaar Jul 08 '20 at 09:15
  • my problem is in trying to input 2 separate lines and put the first line into an integer and the 2nd line into an array. The first line seems to hold the wrong value every time. val n = scanner.next().first().toInt() the 2nd line is working fine val array1 = readLine()!!.split(" ").map { it.toInt() } – JerryG1112 Jul 08 '20 at 09:23
  • Hey @JerryG1112 it'd be great if you could properly format the code in your question and move your description in the earlier comment up there too, so that other people can easily tell if this question is relevant to them :) – Caelum Jul 08 '20 at 09:55
  • Maybe your issue is related to those described on https://stackoverflow.com/questions/5784540/problems-with-scanner-java ? – Caelum Jul 08 '20 at 09:57
  • I solve the problem after reading the article deHaar suggested – JerryG1112 Jul 08 '20 at 10:52

1 Answers1

0

Short form

Use scanner.nextInt() instead of scanner.next().first().toInt().

Explanation

By calling scanner.next() you receive the next complete token of the Scanner as a String. Then you take the first character using first(). The problem is, that calling toInt() on a Char will not parse the string as an integer value but return the ASCII char code of the char.

Example: '2'.toInt() returns 50 and not 2 since the ASCII char code of 2 is 50. (See https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html)

Conclusion: In your case, directly read the integer value from the Scanner using scanner.nextInt(), but if you want to convert a char to an integer by "parsing", convert it to a string first: '2'.toString().toInt() will return an integer with the value 2

Addition

It is probably nice to know that you can also use this the other way round: 50.toChar() returns a character with the value '2'.

theincxption
  • 189
  • 1
  • 2
  • 7