• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String[] to int[] question

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I have a new issue that I need to fix in my code and I could use some pointers. I have the following code but it does not work they way I am expecting it to work, as usual..



Everything else works in this except the Int[] array, it gives me an output of:

Student ID: 1 First Name: John Last Name: Smith Age: 20 Grades: [I@a14482
Student ID: 2 First Name: Suzan Last Name: Erickson Age: 19 Grades: [I@140e19d
Student ID: 3 First Name: Jack Last Name: Napoli Age: 19 Grades: [I@17327b6
Student ID: 4 First Name: Erin Last Name: Black Age: 22 Grades: [I@14ae5a5
Student ID: 5 First Name: Jennifer Last Name: Pasternik Age: 40 Grades: [I@131245a


Can someone help me figure out why this isn't working? I'm sure it's something small, I just can't seem to get it...

Thanks,

Jenni
 
Jenni Pasternik
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried this too and I get the same output as above:

 
Jenni Pasternik
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And here's my method call:

 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have an array like this:

...and you try to print it like this:

...you will get something like what you see. "[I" is code for an integer array, @ is a separator, and the numbers are a memory location. This is what Object's toString() prints. You see this because intArray is a reference.

The usual way to print an array is with a for loop. (There may be fancier ways too).
 
Jenni Pasternik
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks!!!

I went back to my print method, and there it was!!!



I was missing my Arrays.toString!!!

Thanks for making me think about it!!

I appreciate you!
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Three formatting things:
What has a stu? Using such abbreviations will confuse you and everybody else if you come back to that in three months.
Use camelCase for multipleWords not_underscores.
Your print instruction is too long to read; I have broken the line so you can see how to do it.

Why didn't you use the % tags and printf?
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jenni Pasternik wrote:. . . I went back to my print method, and there it was!!! . . .

Wrong place to do it. That should be in a toString method in the Student class, then you simply write
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The title of your thread says String[] to int[]. You probably don't need to make such a conversion but if you did you would probably do it with a Stream (well, you would now Java8 is here, though many people don't yet know who to do it). Something like this:-The first line uses the method of the Arrays class to create a Stream<String>.
The mapToInt method creates an IntStream which specifically contains ints. It uses a mapping from the Strings in the Stream (that is what the s means) and the -> (often pronounced, “goes to”) takes the String called s and applies it to the Integer.parseInt method. There is a shorter way to do that.
Once you have got your hands on the IntStream, you can get an int[] out of it simply with its toArray method.

The parseInt method will throw an Exception if your Strings are not in the correct format; there is no exception handling in that code.
 
Bartender
Posts: 2236
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
I would writeinstead of Stream of numbers reads more natural to me.

This is just the matter of preference. Both methods do the same thing.

Actually one of them calls the other:
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:. . . Stream of numbers reads more natural to me. . . .

Also shorter and neater I had forgotten about that factory method.
 
Jenni Pasternik
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow so much great info, and so many ways to do it! This is amazing, and now that I am starting to understand it more it's actually fun! Thanks everyone for all of the help, you really have helped me understand this better!!

Thanks,

Jenni
 
reply
    Bookmark Topic Watch Topic
  • New Topic