You can treat each of the elements as a one‑letter String and convert that letter to a char, which gives you its Unicode value (not ASCII strictly). But this approach is dependent on the elements of the array having a particular type; even using a String would be horribly error‑prone.
Any reason Object isn't a more specific data type? Converting an Object to an int only makes sense if Object just happens to store an Integer, or a String known to represent an integer. I think we need more background on what it is that you're actually trying to achieve, perhaps with an example. What kinds of objects do you anticipate that your Object will hold?
Campbell Ritchie wrote:You can treat each of the elements as a one‑letter String and convert that letter to a char, which gives you its Unicode value (not ASCII strictly). But this approach is dependent on the elements of the array having a particular type; even using a String would be horribly error‑prone.
Thanks! So I could convert to a char and then to an int?
Carey Brown wrote:I think we need more background on what it is that you're actually trying to achieve, perhaps with an example. What kinds of objects do you anticipate that your Object will hold?
Ya sorry I can give more of a background.
Basically what I'm trying to do is use the Smile-Statistical Machine Intelligence and Machine Learning Library to use the k-nearest neighbour classifier to make data science predictions.
To use the Knn.fit method, I need the training labels(which is the second argument) to be an int, however right now my labels are a 1 column dataframe with rows containing a single letter converted to an Object so I'm trying to convert to int to use in Knn.fit.
posted 6 days ago
You can surely read the data from the file directly as text (=Strings), then from each use the charAt(0) method to turn each into a char. Remember a char is a number, and maybe the quickest way to turn it into an int is with the promotion operator.It is very unusual to find a “real” use for the promotion operator. Actually, even this might not be a “real” use for it; you will get the same promotion without that operator anyway.Every non‑empty String has a char at position 0, but I advise you do lots of validation of the text to verify its contents.
Do you want 'g' to come out as 0x0067 (=103 decimal)? Are you aware of the Character#getNumericValue() method? That will give you a different result from 0x0067 however.
I suspect it would be helpful to find out what class is in that array:
It may be a String or Character... or even something else that is overriding its own toString() method. Knowing will help you convert effectively. Then you can do something like:
posted 6 days ago
If you are reading from a text file, I am not sure I would use an array in the first place. Read the tokens into a List<String> and you don't need an Object. It should be easy enough to run a Stream. Let's imagine you have a buffered reader called reader:-...and you no longer need a List.
That is only a suggestion, which will need lots of tweaking to be any use in real life. You can for example insert a filter() call at line 4½.
What is a type of statusdataframe.col("Status")?
Something bad must be happening with its internals.
An empty list wouldn't cause toArray() to throw NoSuchElementException.
An empty String in the list would result in StringIndexOutOfBoundsException.
A null reference inside the list would result in NullPointerException.
A non-String object in the list would result in ClassCastException.
I can't think of any other condition that could result in an exception except the list does something weird.
By the way, when you sort this issue out you may find that System.out.println(ints); doesn't print what you expect.
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad: