File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Arrays.toString() returns what kind of String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Arrays.toString() returns what kind of String" Watch "Arrays.toString() returns what kind of String" New topic
Author

Arrays.toString() returns what kind of String

Abhishek KumarSoni
Ranch Hand

Joined: Nov 05, 2013
Posts: 61



Here output at line 6 is [4,5,6,7,8,9] .What kind of String is this??
Why exception arises at next line.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40

Abhishek KumarSoni wrote:

Here output at line 6 is [4,5,6,7,8,9] .What kind of String is this??
Why exception arises at next line.



As you can tell, the Arrays.toString() method returns a formatted string that represents the array. It is surrounded by square brackets, and the members of the array are separated by commas.

And of course, it generates an exception because "[4,5,6,7,8,9]" can't be parsed. The Integer.parseInt() method expects a single integer only.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Abhishek KumarSoni
Ranch Hand

Joined: Nov 05, 2013
Posts: 61

According to me this function is not properly designed and output is useless outpur should be 456789 not [4,5,6,7,8,9].I could use this in parseInt function or everywhere a string object is required
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3553
    
  15
Abhishek KumarSoni wrote:According to me this function is not properly designed and output is useless outpur should be 456789 not [4,5,6,7,8,9].I could use this in parseInt function or everywhere a string object is required

The toString method for all array types outputs the contents in the same way - all the elements separated by commas and the whole thing enclosed by square brackets.
This is a good design because it produces consistent results for all array types and it is obvious the output was generated from a collection of some sort.
If you want the output in a different format then you need to write your own method.


Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays.toString() returns what kind of String