Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with array

 
Sanny kumar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sanny kumar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 1608
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    This is called an "off-by-one" error.
    Try this instead:
     
    Paul Sturrock
    Bartender
    Posts: 10336
    Eclipse IDE Hibernate Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    String sc[]=score.split(",");
    for(int i=0;i<sc.length();i++)
    {
    out.println(sc[i]);
    }

    Kevin Huang
    SCJP,SCWCD,SCBCD
    MCP,MCAD,MCSD
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic