Android StudioでのKotlinを用いたプログラミングについて、
以下のサンプルコードを打ち込んでctrl+Enterを押したところ、赤字のようなエラー文が出てきました。
エラー文の一番上の行をインターネットで検索してみたところどうやらファイルを読み込む際のエラーであるようなのですが、プロジェクトを起動した直後のサンプルプログラムであり参考にしているテキストにも読み込むためのファイルを用意する旨は特に記載されていないためどういうことなのかわからず困っています。
検索した記事の中にUTF-8というワードがあったのですが、参考書に従ってファイルのエンコード形式をすべてUTF-8に変更しているのですが、この操作が何かエラーの原因になっていたりするのでしょうか?
よろしければお教えください。
Console.kt
1/* 2 * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license 3 * that can be found in the license/LICENSE.txt file. 4 */ 5 6@file:JvmName("ConsoleKt") 7 8package kotlin.io 9 10import kotlin.text.* 11import java.io.InputStream 12import java.nio.Buffer 13import java.nio.ByteBuffer 14import java.nio.CharBuffer 15import java.nio.charset.Charset 16import java.nio.charset.CharsetDecoder 17 18/** Prints the given message to the standard output stream. */ 19@kotlin.internal.InlineOnly 20public actual inline fun print(message: Any?) { 21 System.out.print(message) 22} 23 24/** Prints the given message to the standard output stream. */ 25@kotlin.internal.InlineOnly 26public inline fun print(message: Int) { 27 System.out.print(message) 28} 29 30/** Prints the given message to the standard output stream. */ 31@kotlin.internal.InlineOnly 32public inline fun print(message: Long) { 33 System.out.print(message) 34} 35 36/** Prints the given message to the standard output stream. */ 37@kotlin.internal.InlineOnly 38public inline fun print(message: Byte) { 39 System.out.print(message) 40} 41 42/** Prints the given message to the standard output stream. */ 43@kotlin.internal.InlineOnly 44public inline fun print(message: Short) { 45 System.out.print(message) 46} 47 48/** Prints the given message to the standard output stream. */ 49@kotlin.internal.InlineOnly 50public inline fun print(message: Char) { 51 System.out.print(message) 52} 53 54/** Prints the given message to the standard output stream. */ 55@kotlin.internal.InlineOnly 56public inline fun print(message: Boolean) { 57 System.out.print(message) 58} 59 60/** Prints the given message to the standard output stream. */ 61@kotlin.internal.InlineOnly 62public inline fun print(message: Float) { 63 System.out.print(message) 64} 65 66/** Prints the given message to the standard output stream. */ 67@kotlin.internal.InlineOnly 68public inline fun print(message: Double) { 69 System.out.print(message) 70} 71 72/** Prints the given message to the standard output stream. */ 73@kotlin.internal.InlineOnly 74public inline fun print(message: CharArray) { 75 System.out.print(message) 76} 77 78/** Prints the given message and newline to the standard output stream. */ 79@kotlin.internal.InlineOnly 80public actual inline fun println(message: Any?) { 81 System.out.println(message) 82} 83 84/** Prints the given message and newline to the standard output stream. */ 85@kotlin.internal.InlineOnly 86public inline fun println(message: Int) { 87 System.out.println(message) 88} 89 90/** Prints the given message and newline to the standard output stream. */ 91@kotlin.internal.InlineOnly 92public inline fun println(message: Long) { 93 System.out.println(message) 94} 95 96/** Prints the given message and newline to the standard output stream. */ 97@kotlin.internal.InlineOnly 98public inline fun println(message: Byte) { 99 System.out.println(message) 100} 101 102/** Prints the given message and newline to the standard output stream. */ 103@kotlin.internal.InlineOnly 104public inline fun println(message: Short) { 105 System.out.println(message) 106} 107 108/** Prints the given message and newline to the standard output stream. */ 109@kotlin.internal.InlineOnly 110public inline fun println(message: Char) { 111 System.out.println(message) 112} 113 114/** Prints the given message and newline to the standard output stream. */ 115@kotlin.internal.InlineOnly 116public inline fun println(message: Boolean) { 117 System.out.println(message) 118} 119 120/** Prints the given message and newline to the standard output stream. */ 121@kotlin.internal.InlineOnly 122public inline fun println(message: Float) { 123 System.out.println(message) 124} 125 126/** Prints the given message and newline to the standard output stream. */ 127@kotlin.internal.InlineOnly 128public inline fun println(message: Double) { 129 System.out.println(message) 130} 131 132/** Prints the given message and newline to the standard output stream. */ 133@kotlin.internal.InlineOnly 134public inline fun println(message: CharArray) { 135 System.out.println(message) 136} 137 138/** Prints a newline to the standard output stream. */ 139@kotlin.internal.InlineOnly 140public actual inline fun println() { 141 System.out.println() 142} 143 144private const val BUFFER_SIZE: Int = 32 145private const val LINE_SEPARATOR_MAX_LENGTH: Int = 2 146 147private val decoder: CharsetDecoder by lazy { Charset.defaultCharset().newDecoder() } 148 149/** 150 * Reads a line of input from the standard input stream. 151 * 152 * @return the line read or `null` if the input stream is redirected to a file and the end of file has been reached. 153 */ 154fun readLine(): String? = readLine(System.`in`, decoder) 155 156internal fun readLine(inputStream: InputStream, decoder: CharsetDecoder): String? { 157 require(decoder.maxCharsPerByte() <= 1) { "Encodings with multiple chars per byte are not supported" } 158 159 val byteBuffer = ByteBuffer.allocate(BUFFER_SIZE) 160 val charBuffer = CharBuffer.allocate(LINE_SEPARATOR_MAX_LENGTH) 161 val stringBuilder = StringBuilder() 162 163 var read = inputStream.read() 164 if (read == -1) return null 165 do { 166 byteBuffer.put(read.toByte()) 167 if (decoder.tryDecode(byteBuffer, charBuffer, false)) { 168 if (charBuffer.containsLineSeparator()) { 169 break 170 } 171 if (!charBuffer.hasRemaining()) { 172 stringBuilder.append(charBuffer.dequeue()) 173 } 174 } 175 read = inputStream.read() 176 } while (read != -1) 177 178 with(decoder) { 179 tryDecode(byteBuffer, charBuffer, true) // throws exception if undecoded bytes are left 180 reset() 181 } 182 183 with(charBuffer) { 184 val length = position() 185 val first = get(0) 186 val second = get(1) 187 when (length) { 188 2 -> { 189 if (!(first == '\r' && second == '\n')) stringBuilder.append(first) 190 if (second != '\n') stringBuilder.append(second) 191 } 192 1 -> if (first != '\n') stringBuilder.append(first) 193 } 194 } 195 196 return stringBuilder.toString() 197} 198 199private fun CharsetDecoder.tryDecode(byteBuffer: ByteBuffer, charBuffer: CharBuffer, isEndOfStream: Boolean): Boolean { 200 val positionBefore = charBuffer.position() 201 byteBuffer.flip() 202 with(decode(byteBuffer, charBuffer, isEndOfStream)) { 203 if (isError) throwException() 204 } 205 return (charBuffer.position() > positionBefore).also { isDecoded -> 206 if (isDecoded) byteBuffer.clear() else byteBuffer.flipBack() 207 } 208} 209 210private fun CharBuffer.containsLineSeparator(): Boolean { 211 return get(1) == '\n' || get(0) == '\n' 212} 213 214private fun Buffer.flipBack() { 215 position(limit()) 216 limit(capacity()) 217} 218 219private fun CharBuffer.dequeue(): Char { 220 flip() 221 return get().also { compact() } 222}
これはどこを参照したサンプルなのか、出典を示していただくことは可能でしょうか?ぱっと見、コマンドプロンプトのような端末上で動作させるプログラムのように思えますが。
どこで動かしているかってことでしたら、Android Studioの中のKotlin REPLです。
どこで動かしているかではなくて、サンプルコードはサイトや書籍を参考にしたものですか?もしそうであれば、参考元を教えてくださいってことではないでしょうか?
ああ、すみませn。書籍を参考にしています。SBクリエイティブ株式会社から発行されている「基本からしっかり身につくAndroidアプリ開発入門 Android Studio 3.x対応」です。森洋之さんが書かれています。
ああ、すみません。書籍を参考にしています。SBクリエイティブ株式会社から発行されている「基本からしっかり身につくAndroidアプリ開発入門 Android Studio 3.x対応」です。森洋之さんが書かれています。