Scanner question

Hello, tried to make program that ask number of elements and sorts them out.

Works partially, something to do with Scanner? Why prints out as mess?

import java.util.Scanner

fun main(){
val scan = Scanner(System.in)
println("Enter the total Number of Elements: ")
var arrayInt = IntArray(scan.nextInt())
println("Please nter the $arrayInt Element: ")
for(item in arrayInt)
arrayInt[item] = scan.nextInt()

for(i in arrayInt)
    print("${arrayInt[i]} ")

println(arrayInt.sortedArrayDescending())

}

for(item in arrayInt) does not iterate over indices, but over values stored in the array. You can iterate over indices like this: for(item in arrayInt.indices). Even better, you can do it like this:

IntArray(scan.nextInt()) { scan.nextInt() }

P.S.
You still need to work on your post descriptions. You post quite a lot of questions here and they are always/usually at least confusing.

You said above that your code “prints out as mess”, but you didn’t explain what do you mean by “mess”. What is the output? What did you expect instead? You don’t even care that your code is formatted improperly.

It doesn’t really encourage to help you.

2 Likes

Ok. Thanks for reply. I need to practise to be more precise. Sorry for that, haven’t been board before neither any coding type board. Actually have made coding 25 years ago, been working on telecommunications area for 26 years :slight_smile:

That code above prints:
"Please enter the [I@34a245ab Element: "
Instead “Please enter the 1 element:” (or 1 is 1-> depends from answer inputted)
after elements inputted:
“0 2 2 2 2 [I@4769b07b” sorted values also messed up.

But i practise more and start from your examples.

So, there are several things going on here.

First is this cryptic [I@34a245ab string. This is what you get when you convert an array to a string or when you print it. Yes, this is not really useful and yes, this is counter-intuitive as we get a nice string representation for lists, but we get this meaningless gibberish for arrays. This is just how it is in Java, you can’t do too much about it. To get array contents as string use contentToString() or just convert it to a list with: asList(). If you are interested, [I@34a245ab means that this is an array ([) of integers (I) identified by some kind of ID: 34a245ab.

Second, if you meant to output something like this:

Please enter the 1 element:
Please enter the 2 element:
Please enter the 3 element:

Then you really messed up this part. Printing isn’t really inside the loop, but before it. Moreover, it does not print the current item (item), but the array itself (arrayInt).

After fixing all bugs in your code, we get something like:

val scan = Scanner(System.`in`)
println("Enter the total Number of Elements: ")
var arrayInt = IntArray(scan.nextInt())

for(item in arrayInt.indices) {
    println("Please enter the ${item + 1} Element: ")
    arrayInt[item] = scan.nextInt()
}

for(i in arrayInt.indices)
    print("${arrayInt[i]} ")

println(arrayInt.sortedArrayDescending().contentToString())

But it would be really much more Kotlin-ish to do it like this:

val scan = Scanner(System.`in`)

println("Enter the total Number of Elements: ")
val arrayInt = IntArray(scan.nextInt()) {
    println("Please enter the ${it + 1} Element: ")
    scan.nextInt()
}

arrayInt.forEach(::print)

println(arrayInt.sortedDescending())
1 Like

Great! I really appreciate that! Good info! Thanks :+1: