File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Problem with array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with array" Watch "Problem with array" New topic
Author

Problem with array

Sanny kumar
Ranch Hand

Joined: May 18, 2005
Posts: 53
Hi Guys,

i have a String i want to convert it into integer and want to get the sum of the String.

ex:

String s="2,2,2,3";--The ouput should be :9

String str=s.split(",")
Vector v=new Vector();

v.addElement(str);


can anyone give suggestions?
Thanks in advance
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You've spotted that you can split a String into an array of parts with:

Now you need to go through the array adding each value to some result. You can do this with a simple loop and:

There are potential errors you will need to worry about, the biggest one being what you do if the String contains unparsable values.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Sanny kumar
Ranch Hand

Joined: May 18, 2005
Posts: 53
Hi Paul,

if i use str.split i m getting error array indexoutof bounds exception.

The code i wrote is:

for(int i=0;i<=score.length();i++)
{
String sc[]=score.split(",");
out.println(sc[i]);
}

can u please help me??

Thanks and Regards
Sanny
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

An ArrayIndexOutOfBoundsException happens when you try to access an element in an array using an index that is outside the range the array contains. For example an array defined like this:

has a range 0 to 2 inclusive. If I write:

I will get an ArrayIndexOutOfboundsException

Why is htis happening to your code? Well, you logic is wrong. You need to split the String to get your array of parts, then step through that array adding the parts. What you have written steps through the String one character at a time, then split the String repeatedly.

The steps should be in this order:
  • Split the String "source" into an array of parts
  • loop through the array of parts
  • for each part parse its value and add to a result.


  • If you look at your code you should be able to see what needs changed to get this to work.
    [ May 22, 2006: Message edited by: Paul Sturrock ]
    Tony Morris
    Ranch Hand

    Joined: Sep 24, 2003
    Posts: 1608

    This is called an "off-by-one" error.
    Try this instead:


    Tony Morris
    Java Q&A (FAQ, Trivia)
    Paul Sturrock
    Bartender

    Joined: Apr 14, 2004
    Posts: 10336

    That won't work in this instance. The length of his CSV String will always be much longer than the length of the array produced by splitting the String, not just one longer.
    Kevin Huang
    Greenhorn

    Joined: Mar 27, 2004
    Posts: 13
    String sc[]=score.split(",");
    for(int i=0;i<sc.length();i++)
    {
    out.println(sc[i]);
    }

    Kevin Huang
    SCJP,SCWCD,SCBCD
    MCP,MCAD,MCSD
     
    GeeCON Prague 2014
     
    subject: Problem with array