NoSuchElementException java.lang.Scanner - kotlin

I have no idea what the error is, I am having a hard time adapting to this language, any help thank you very much.
Error:
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
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 Packing.<init>(Packing.kt:100)
at PackingKt.main(Packing.kt:7)
at PackingKt.main(Packing.kt)
My code:
import java.io.InputStream
import java.util.Scanner
fun main() {
val input = Scanner(InputStream.nullInputStream())
val packing1 = Packing(input)
val packing2 = Packing(input)
val packing3 = Packing(input)
var total = 0
var min = 0
val combinations = ArrayList<String>()
for(a in 1..3){
for(b in 1..3){
for(c in 1..3){
//here is a piece of code
}
}
combinations.sort()
println("${combinations.get(0)} $min")
}
}
class Packing {
var brownBottles = 0
var greenBottles = 0
var clearBottles = 0
constructor (input : Scanner){
brownBottles = input.nextInt() //this is the line 100
greenBottles = input.nextInt()
clearBottles = input.nextInt()
}
}
The idea is to enter values by console that initialize the variables of my objects.

I would just use
val input = Scanner(System.`in`)
If you enter 9 integers in the console the initialization of the Packing objects should work.
The nullInputStream() makes no sense to me. It's not possible to read from the console with that.
The combinations list is empty so it throws an exception accessing it here
println("${combinations.get(0)} $min")

Related

How to set Xaxis and Yaxis using data class using kotlin in MpAndroid bar chart

Halo i am trying to create barchart using MPAndroid Library but icant how to use it when i send json from php
echo json_encode($output);
and the output contain 2 data that is hari and total_jual
$rowdata[]=array('hari'=>$row['hari'],
'total_jual'=>$row['total_jual']);
in android studio i am using volley to catch jason
for(i in 0 until arr.length()){
val obj = arr.getJSONObject(i)
dataListPenjualan.add(
ClassPenjualan(
obj.getString("hari").toString(),
obj.getString("total_jual").toString()
)
)
val entries = ArrayList<BarEntry>()
val barDataSet = BarDataSet(entries, "Cells")
val labels = ArrayList<String>()
labels.add(dataListPenjualan[i].hari)
//barDataSet.setColors(ColorTemplate.COLORFUL_COLORS)
barDataSet.color = resources.getColor(R.color.black)
chartPemasukan.animateY(5000)
}
the data i catch using volley i send it into class
this is my class
data class ClassPenjualan (val hari:String,
val totalPenjualan:String)
how can i create barchart using data i catch from php. I already try to search but many explanation is in java.
this is what i try
val entries = ArrayList<BarEntry>()
entries.add(BarEntry(dataListPenjualan[i].hari.toFloat(), i))
val barDataSet = BarDataSet(entries, "Cells")
val labels = ArrayList<String>()
labels.add(dataListPenjualan[i].hari)
val data = BarData(labels, barDataSet)
chartPemasukan.data = data // set the data and list of lables into chart
chartPemasukan.setDescription("Set Bar Chart Description") // set the description
//barDataSet.setColors(ColorTemplate.COLORFUL_COLORS)
barDataSet.color = resources.getColor(R.color.black)
chartPemasukan.animateY(5000)
Your code entries.add(BarEntry(dataListPenjualan[i].hari.toFloat(), i)) is wrong.
just try entries.add(BarEntry(i,dataListPenjualan[i].hari.toFloat()))
below code is my demo
val values = ArrayList<BarEntry>()
var i = 0
while (i < xValueCount) {
val yValue = (Math.random() * (100)).toFloat()
values.add(BarEntry(i.toFloat(), yValue))
i++
}
val set1: BarDataSet
if (chart.data != null &&
chart.data.dataSetCount > 0) {
set1 = chart.data.getDataSetByIndex(0) as BarDataSet
set1.values = values
chart.data.notifyDataChanged()
chart.notifyDataSetChanged()
} else {
set1 = BarDataSet(values, "speed")
//绘制图标
set1.setDrawIcons(false)
//绘制数值
set1.setDrawValues(false)
set1.color = ContextCompat.getColor(mContext, getBarHighColorByDataType(false))
set1.highLightColor = ContextCompat.getColor(mContext, getBarHighColorByDataType(true))
set1.highLightAlpha = 255
val dataSets = ArrayList<IBarDataSet>()
dataSets.add(set1)
val data = BarData(dataSets)
data.setValueTextSize(10f)
//barWith = 柱宽度/(柱宽度+旁边一处空白宽度)
data.barWidth = when (dataType) {
0 -> 0.37f
1 -> 0.52f
2 -> 0.3f
else -> 0.43f
}
chart.data = data
}

Kotlin ListView IndexOutOfBoundsException

(new to kotlin) I'm making my own music app but i'm having an error that i don't understand :
in the bit of code where i try to access a random view (shuffle), i get the java.lang.IndexOutOfBoundsException: Index:96 Size:11.
96 in this example is the view in the listview that i'm trying to access.
It happens in this line : var iView = listViewMusic.get(idShuffle)
Same if i use listViewMusic[idShuffle]
EDIT : Turns out that 11 is only the 11 visible items on screen at any given moment, even if the list contains hundreds of items. When i use listViewMusic.smoothscrolltoposition(idShuffle) it works, but the size of 11 now relates to the 11 on screen after the scrolling
The function playNext() inside the activity, called when clicking on the shuffle button:
fun playNext(){
try {
// Find the order of the next song to play
var idShuffle = musicAdapter!!.getIdofItem(listMusicShuffled.get(musicNextToPlay).title)
// Find the right record in the list
//var iView = listViewMusic[idShuffle]
//toastIt(applicationContext, "adapter count ${listViewMusic.adapter.count}")
//toastIt(applicationContext, "listview count ${listViewMusic.count}")
var iView = listViewMusic.get(idShuffle) //throws the error
// Play it
playOrPause(iView)
// Prepare the next track
musicNextToPlay += 1
if (musicNextToPlay >= listMusicShuffled.size) {
musicNextToPlay = -1
}
} catch (e:Exception) {toastException(applicationContext, "playnext", e) }
}
The part of the function onCreate that fills in the listViewMusic:
// Retrieve the data
val mediaStoreUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
val cursor = contentResolver.query(mediaStoreUri, null, "", null, null)
//Browse the data
listMusic = mutableListOf()
val listMusicJson = getMusicFromJson()
while (cursor!!.moveToNext()) {
val musicName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME))
val musicArtist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST))
val musicUrl = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA))
val musicType:String =
if (musicArtist.contains("lmdmf", true)) { "LMDMF" }
else if (musicName.contains("slack", true)) { "SLACK" }
else { "MUSIC" }
listMusic.add(
MusicTrack(
musicName,
musicPlayCount,
musicUrl,
musicType
)
)
}
cursor.close()
musicAdapter = MusicAdapter(listMusic.sortedWith(compareBy<MusicTrack>{ it.title }).filter { it.type == "MUSIC"}.toMutableList())
listViewMusic.adapter = musicAdapter

Kotlin - The caracter literal does not conform expect type Int

I'm struggling with types with my program, I've been asked to do it in JS first and it worked fine but now I can't achieve the result.
Do you think I should make another 'algorithm' ? In advance, thank you for your time.
fun main(){
// the idea is to put numbers in a box
// that cant be larger than 10
val data = "12493419133"
var result = data[0]
var currentBox = Character.getNumericValue(data[0])
var i = 1
while(i < data.length){
val currentArticle = Character.getNumericValue(data[i])
currentBox += currentArticle
println(currentBox)
if(currentBox <= 10){
result += Character.getNumericValue(currentArticle)
}else{
result += '/'
//var resultChar = result.toChar()
// result += '/'
currentBox = Character.getNumericValue(currentArticle)
result += currentArticle
}
i++
}
print(result) //should print 124/9/341/91/33
}
The result is actually of a Char type, and the overload operator function + only accepts Int to increment ASCII value to get new Char.
public operator fun plus(other: Int): Char
In idomatic Kotlin way, you can solve your problem:
fun main() {
val data = "12493419133"
var counter = 0
val result = data.asSequence()
.map(Character::getNumericValue)
.map { c ->
counter += c
if (counter <= 10) c.toString() else "/$c".also{ counter = c }
}
.joinToString("") // terminal operation, will trigger the map functions
println(result)
}
Edit: If the data is too large, you may want to use StringBuilder because it doesn't create string every single time the character is iterated, and instead of using a counter of yourself you can use list.fold()
fun main() {
val data = "12493419133"
val sb = StringBuilder()
data.fold(0) { acc, c ->
val num = Character.getNumericValue(c)
val count = num + acc
val ret = if (count > 10) num.also { sb.append('/') } else count
ret.also { sb.append(c) } // `ret` returned to ^fold, next time will be passed as acc
}
println(sb.toString())
}
If you want a result in List<Char> type:
val data = "12493419133"
val result = mutableListOf<Char>()
var sum = 0
data.asSequence().forEach {
val v = Character.getNumericValue(it)
sum += v
if (sum > 10) {
result.add('/')
sum = v
}
result.add(it)
}
println(result.joinToString(""))

Using SoundPool for games

class SoundPlayer(context: Context) {
// For sound FX
private val soundPool: SoundPool = SoundPool(10, // Here
AudioManager.STREAM_MUSIC,
0)
companion object {
var playerExplodeID = -1
var invaderExplodeID = -1
var shootID = -1
var damageShelterID = -1
var uhID = -1
var ohID = -1
}
init {
try {
// Create objects of the 2 required classes
val assetManager = context.assets
var descriptor: AssetFileDescriptor
// Load our fx in memory ready for use
descriptor = assetManager.openFd("shoot.ogg")
shootID = soundPool.load(descriptor, 0)
descriptor = assetManager.openFd("invaderexplode.ogg")
invaderExplodeID = soundPool.load(descriptor, 0)
descriptor = assetManager.openFd("damageshelter.ogg")
damageShelterID = soundPool.load(descriptor, 0)
descriptor = assetManager.openFd("playerexplode.ogg")
playerExplodeID = soundPool.load(descriptor, 0)
descriptor = assetManager.openFd("damageshelter.ogg")
damageShelterID = soundPool.load(descriptor, 0)
descriptor = assetManager.openFd("uh.ogg")
uhID = soundPool.load(descriptor, 0)
descriptor = assetManager.openFd("oh.ogg")
ohID = soundPool.load(descriptor, 0)
} catch (e: IOException) {
// Print an error message to the console
Log.e("error", "failed to load sound files")
}
}
fun playSound(id: Int){
soundPool.play(id, 1f, 1f, 0, 0, 1f)
}
}
i have a problem with SoundPool cant use it is says constructor SoundPool is deprecated
i'm kinda new so don't know how to fix this (watched many videos and searched everywhere but i cant fix it)
so maybe someone can help me out tell me what to do
When something is deprecated, there always should be a hint what to use instead.
So you need to use SoundPool.Builder to create new instance of an object.
But there is one issue if you target API level that was release before SoundPool.Builder then you will get ClassNotFoundException.
So general approach is to check the API level and do things in old way before API X(when new feature was introduced), and a new way after API X:
#Suppress("DEPRECATION")
fun buildSoundPool(maxStreams: Int):SoundPool =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val attrs = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build()
SoundPool.Builder()
.setAudioAttributes(attrs)
.setMaxStreams(maxStreams)
.build()
} else {
SoundPool(maxStreams, AudioManager.STREAM_MUSIC, 0)
}
Then:
private val soundPool: SoundPool = buildSoundPool(10)
Also I do recommend to use my custom implementation of SoundPool because lots of platform dependent issues that was introduced in original SoundPool on different versions on Android.

How to get length of a character array in kotlin?

I am trying to find the length of two different character array but I am only being allowed to use array.size only once.
How to bypass this problem?
fun chararraytostr(inp1: CharArray): String{
var arlen: Int = inp1.size //here lies the problem
var out1: String = ""
for(j in 0..arlen-1){
var str = inp1[j].toString()
out1+=str
}
return out1
}
fun uppercase(input: String): String{
var temp1: CharArray = input.toCharArray()
var len = input.size //here lies the problem
var temp3: Char
for(i in 0..len-1){
var temp2: Char = temp1[i]
var ascii: Int = temp2.toInt()
if(ascii<=122 && ascii>=97){
ascii-=32
temp3 = ascii.toChar()
temp1[i] = temp3
}else{}
}
var output = chararraytostr(temp1)
return output
}
fun main(arg: Array<String>){
var toupper = "Hi my friend!"
println(uppercase(toupper))
}
It is always showing Unresolved reference: size. I don't know why. Please help.
You're calling size on the String variable instead of the CharArray variable. Use temp1.size instead of input.size.