• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to convert Object[] to int[]?

 
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an Obejct[] which is a [Ljava.lang.Object;,

When I use toString():

it returns something that looks like:

I'm wondering how I can convert this to an int[], like each letter would become the corresponding ASCII value.

I did find online a function to convert to numbers but I need a String[] but myString returns as just a String.

So I'm wondering how I could go about the conversion of the Object[] to an int[]?

Thanks so much!
 
Marshal
Posts: 80226
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10978
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Glenda Karen
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

For ex.


 
Campbell Ritchie
Marshal
Posts: 80226
424
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Master Rancher
Posts: 5116
82
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Campbell Ritchie
Marshal
Posts: 80226
424
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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½.
 
Bartender
Posts: 5584
213
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also do (if it is a joinery dataframe)
 
Glenda Karen
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much everybody for all your help!

Campbell Ritchie wrote:maybe the quickest way to turn it into an int is with the promotion operator.

Thanks, I can look into this.

Campbell Ritchie wrote:Are you aware of the Character#getNumericValue() method?  

No I was not aware, thanks so much, that's a good point, I wasn't thinking about before.

Campbell Ritchie wrote:If you are reading from a text file, I am not sure I would use an array in the first place.


I'm reading from a csv but I presume it's the same as a text file just with comma separation.
 
Glenda Karen
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:I suspect it would be helpful to find out what class is in that array:


Thanks, so I tried and it's a String

Mike Simmons wrote:


Thank you, maybe for the someMethodToConvert... I can convert to char and use promotion operator as Campbell Ritchie suggested.
 
Glenda Karen
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote: You can also do (if it is a joinery dataframe)


Thank you, I am using the joinery, I tried this out and I'm getting:
 
Mike Simmons
Master Rancher
Posts: 5116
82
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you need to count the parentheses carefully in the mapToInt() line - you have one more '(' than ')'.
 
Glenda Karen
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:I think you need to count the parentheses carefully in the mapToInt() line - you have one more '(' than ')'.


Thanks, I added another ")" but still getting same error.
 
Sheriff
Posts: 28365
99
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should be



Maybe you added it elsewhere?
 
Mike Simmons
Master Rancher
Posts: 5116
82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, please show the latest version of the code and the exact error.
 
Glenda Karen
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Should be


That worked, thanks!

Updated code:


I'm getting a new error:
 
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
You got style baby! More than this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic