CanOfBees
05/26/2022, 3:39 PMval scanner = Scanner(System.`in`)
val s1 = scanner.nextLine()
val s2 = scanner.nextLine()
val num = scanner.nextInt()
The answers are roughly:
a )
ABC DEF
123
b )
ABC
DEF 123
c)
10
ABC
3
d)
ABC DEF 123
Looking the behavior in kotlin file locally, the only answer that doesn't throw an error is a
, but not according to the course page. Is this a Kotlin version issue? A JVM issue? Apparently the correct answer is c
but the first Int is confusing me.
Any hints or clarity would be most appreciated! Thank you!Joffrey
05/26/2022, 3:42 PMreadLine()
can read any text, it doesn't have to be letters. Since we don't see the rest of the program it could really be anythingCanOfBees
05/26/2022, 3:45 PMreadLine()
but nextLine()
only seems to applicable to strings?Joffrey
05/26/2022, 3:47 PMnextLine
. Strings can be anything including numbersphldavies
05/26/2022, 3:47 PM"10"
is still a string 😉CanOfBees
05/26/2022, 3:50 PM10
and "10"
- first is an Int and the second is a String, but that's wrong? 10
is a string, unless it is explicitly typed?Vampire
05/26/2022, 3:52 PMnextLine
reads a line, nextInt
reads an int, so it has to be three lines and the only option with that is C.10
vs "10"
, coming as input the first is a two character string, the latter is a four character string. The quotes are just significant when writing a string literal in source codeCanOfBees
05/26/2022, 3:57 PMnextLine
talks about strings. Glad to learn that quotes are only significant in literals in source code - not in input for the scanner
example.phldavies
05/26/2022, 3:58 PMScanner
. in this case, the input stream is the following:
10\n
ABC\n
3
(I’ve added \n
to highlight the line endings). Scanner.nextLine()
is reading the characters if the input stream until the next \n
and returning the string value. In this case it’s returning the String
of "10"
- this is still nothing to do with the Kotlin representation of string vs integer in the code. The two calls to nextLine()
each advance the scanner to after the \n
.
Starting with
|10\n
ABC\n
3
then after one call to nextLine()
10\n
|ABC\n
3
and finally after the second call to nextLine()
10\n
ABC\n
3
Then the call to nextInt()
reads the next token (by default delimited by whitespace) and then interprets this value as an integer.CanOfBees
05/26/2022, 3:59 PMimport java.util.Scanner
fun main() {
println("hello - let's go explore!")
val scanner = Scanner(System.`in`)
val s1 = scanner.nextLine()
val s2 = scanner.nextLine()
val num = scanner.nextInt()
println(s1)
println(s2)
println(num)
}
I'm running this in IntelliJ 2022.1.1, with OpenJDK 11, and Kotlin 221-1.6.21-release.
I can enter:
10\n
abc\n
and then an error is thrown:
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at ExploreKt.main(explore.kt:9)
at ExploreKt.main(explore.kt)
Youssef Shoaib [MOD]
05/28/2022, 4:16 PM<http://System.in|System.in>
, those bytes are interpreted as Strings.
So when you call nextInt
, the Scanner interprets the bytes as a string first, checks that they're a valid int, and if that string parses to a valid int, it is simply returned to you.nextInt
is similar to next
followed by a Integer.parseInt
, and so every integer you read from a console, file, or any other ASCII-formatted thing will be first and foremost a valid string before it is an intVampire
05/28/2022, 5:38 PM