質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

1回答

2362閲覧

Android StudioでのKotlinについて

shalllaugh

総合スコア17

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2018/09/19 06:00

編集2018/09/20 08:10

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}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

keicha_hrs

2018/09/19 14:47

これはどこを参照したサンプルなのか、出典を示していただくことは可能でしょうか?ぱっと見、コマンドプロンプトのような端末上で動作させるプログラムのように思えますが。
shalllaugh

2018/09/19 15:06

どこで動かしているかってことでしたら、Android Studioの中のKotlin REPLです。
shikasama

2018/09/20 01:27

どこで動かしているかではなくて、サンプルコードはサイトや書籍を参考にしたものですか?もしそうであれば、参考元を教えてくださいってことではないでしょうか?
shalllaugh

2018/09/20 04:54

ああ、すみませn。書籍を参考にしています。SBクリエイティブ株式会社から発行されている「基本からしっかり身につくAndroidアプリ開発入門 Android Studio 3.x対応」です。森洋之さんが書かれています。
shalllaugh

2018/09/20 04:55

ああ、すみません。書籍を参考にしています。SBクリエイティブ株式会社から発行されている「基本からしっかり身につくAndroidアプリ開発入門 Android Studio 3.x対応」です。森洋之さんが書かれています。
guest

回答1

0

読み込むファイル自体がUTF-8でないとかはありませんか?
読み込みファイルの文字コードセットを確認し、UTF-8と違う場合にはUTF-8にしてみてください。

以下が類似の質問に思えますので、リンク先を読んでみてください。
入力ファイルのエンコーディング

投稿2018/09/20 01:37

shikasama

総合スコア163

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

shalllaugh

2018/09/20 04:57

すみません。いただいたリンク先の質問は確認したのですが、「読み込むファイル」というのがどういうものなのかよくわからず…何か他にファイルを用意した覚えはないのですが、もし自動的に読み込むためのファイルが作成されるならば、どこで確認できるのでしょうか?
shikasama

2018/09/20 05:13

すみません。質問本文にも書かれていた内容でしたね。>読み込むファイル ログに出ているConsole.kt:154あたりは確認されましたか? readLineは何を読んでいますか?
shalllaugh

2018/09/20 06:04

154行目では「fun readLine(): String? = readLine(System.`in`, decoder)」となっています。他に似たようなエラーがあるのでたどってみましたが、167行目では「if (decoder.tryDecode(byteBuffer, charBuffer, false)) {」、203行目では「if (isError) throwException()」となっています。
shikasama

2018/09/20 06:54

標準入力したものをバイトに変換し、さらにデコードするといったプログラムでしょうか? readLineは別に定義されていませんか? 1byteずつデコードする際に、2byte文字の片割れが入ってきたときにおかしくなっているのかもしれません。
shalllaugh

2018/09/20 07:00

解説によると、関数を定義したうえでその関数を呼び出して利用するコードのようです。今打ち込んでみたのは関数を定義する部分のようなのですが、指定された作業のみを行っているのでreadLineを別で打ち込んで定義している可能性は低いのかなと思います。
shalllaugh

2018/09/20 07:41

そうですね、なっていないように見えます。全体公開とはどのようにしてやればよいのでしょうか?該当するConsole.ktのソースを貼り付ければ良いでしょうか?
shikasama

2018/09/20 07:57

Console.ktを見せていただきたいです。
shalllaugh

2018/09/20 08:10

質問欄に追記しました
shikasama

2018/09/20 09:05

internal fun readLine(inputStream: InputStream, decoder: CharsetDecoder)の部分で定義されていますね。 デコードに失敗しているようですね。 inputStream.read()で1byte読んでデコードしているので、マルチバイト文字などをデコードしようとしているのではないでしょうか? Charset.defaultCharset()が何になっているか確認してみるのもいいかもしれません。
shalllaugh

2018/09/21 10:23

ありがとうございます。こんなに長いコードなのに目を通していただき感謝です。ひとまず確認の作業を行ってみますね。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問