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...
...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).
All things are lawful, but not all things are profitable.
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.
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.
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!!