problems with semantics and keywords.

I have the following lines of code with which I think there are a few problems to do with adding elements into a Vector:

     Vector v = new Vector();
     String s = "ABCD";
     
      for(int i = 0; i< s.length() i++)
        v.add(0, s.substring (i, i +1));

Please tell me what the problem with the v.add is and what would the final vector look like once the for loop has finished looping?

Also what is wrong with this lines of code:
    String x = " ";
    x += s.substring(2,5);

Does the += operator exist, and if so what does it do?
Does x need a variable type assinged to it?

Thanks for your help!
luinziAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

billy_98_1Commented:
I can't help with the first question, but the += statment should work with a string.
0
luinziAuthor Commented:
Ok thats great......but what does the += do exactly????????

as far as I know v.add() method is part of a collection of interfaces available for java 2. I think!!!
0
_lychee_Commented:
1. there's supposed to be a ; between s.length() and i++.

2. i don't see any prob with the add... just that this form of add started with JDK1.2 so make sure u're using that...
so if u dun want to use add use
insertElementAt(s.substring(....), 0) instead...

3. v will contain "D", "C", "B", "A" in that order...

4. substring's 2nd argument only takes up to s.length... which in this case is 4 (< 5) so the statement will throw an exception....

5. += means add to... s += a is the same as s = s + a
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

s_franklinCommented:
Working code snippet with changes commented:

Vector v = new Vector();
String s = "ABCD";

// Added braces for clarity
// Added semicolon to separate second/third args
for (int i = 0; i < s.length(); i++) {
  // Changed .add(...) to insertElementAt(...) which is
  // supported
  v.insertElementAt(s.substring(i, i + 1),0);
}

// The following line was written to validate what went in
for (int i=0; i< s.length(); i++) {
  System.out.println(v.elementAt(i));
}

String x = " ";
x += s.substring(2, 4); // Fixed the offset in substring so that it
                        // didn't go out of bounds

// Added to print out result to convince us it works
System.out.println("X: " + x);
0
s_franklinCommented:
Sorry lychee - my browser is set up for proxy and I didn't see your response first.
0
_lychee_Commented:
it's alright :>
0
JodCommented:
Also just a further point luinzi, without meaning to sound picky as regards the previous answers, but usually it is best to avoid hardcoding the estimated lengths of strings...

String x = " ";
x += s.substring(2);

will get you all the characters in S from after second one to the last with no danger of going over the string length. For example,

"unhappy".substring(2) returns "happy"

Or instead use

String x = " ";
x += s.substring(2,s.length);

You could get really picky and make sure that there are actually at least two characters in the string but that is a level of fault tolerance I don't think matters for you here.

There are also a range of command similar to

  +=

inherited from C/C++ that are retained in Java.

Such as -=. For example:

  a -= 1;

I bet you can guess the rest...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
luinziAuthor Commented:
Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.