Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: String Vector

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default String Vector

    I have a vector of characters for example [e,m,a] but I need to have a String in which all characters are in a same word such as "ema", how should I change [e,m,a] to "ema"?


  2. #2
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default String vector

    int ch;
    Vector<Character> result = new Vector<Character>(50, 10); 
    String[] Results ;
    		    while ((ch=inputStream.read()) > 0) {
    		    	result.add((char)ch);
    		 	}
    Results = new String[result.size()];
    Results= (String[])(result.toArray());
    it generates an error"[Ljava.lang.Object; cannot be cast to [Ljava.lang.String;"

    what should I do if I need to convert a vector of chars to an string?
    Last edited by helloworld922; April 3rd, 2010 at 09:50 PM.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: String Vector

    Please don't post the same question in two different forums and please use [code] tags.

    You can use a StringBuilder object and add characters as you go. There's no need to read in the data if you already have it.

    // to append stuff to a StringBuilder
    // this code appends the character 'h' to the end of the StringBuilder object
    StringBuilder myStringBuilder = new StringBuilder();
    myStringBuilder.append('h');

    To get the character at each position on the Vector, use a for-loop across the size of the vector.

    for (int i = 0; i < myVector.size(); i++)
    {
         char theCharacter = myVector.get(i);
    }

  4. The Following User Says Thank You to helloworld922 For This Useful Post:

    nasi (April 3rd, 2010)

  5. #4
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: String Vector

    Thank you very much helloworld992. sorry for that. I don't undrestand what you mean by use [code] tags.

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: String Vector

    Please see Rule #1 for how to use [code] tags.

Similar Threads

  1. Problem about Vector
    By tohid in forum Collections and Generics
    Replies: 1
    Last Post: March 26th, 2010, 01:44 PM
  2. Difference between Vector and Arraylist
    By Lil_Aziz1 in forum Collections and Generics
    Replies: 4
    Last Post: January 3rd, 2010, 11:03 AM
  3. Difference between Arraylist and Vector in abstractTableModel ?
    By riddhik84 in forum Collections and Generics
    Replies: 2
    Last Post: November 7th, 2009, 04:22 PM
  4. byte[] from vector
    By perlWhite in forum Collections and Generics
    Replies: 1
    Last Post: August 26th, 2009, 05:10 AM
  5. vector
    By sriraj.kundan in forum Java Theory & Questions
    Replies: 8
    Last Post: August 12th, 2009, 10:17 AM

Tags for this Thread