• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getting error when converting vector to string

 
Greenhorn
Posts: 20
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

I faced problem in converting a vector to String. I'm giving the code below. Please let me know how can i do that.



when I tried to print the vector all the value is printing. But when I tried to print the string array its giving "[java.lang.string@16978b" as output. Probably its throwing the address of the particular value its keeping.

please suggest what to do.
 
Marshal
Posts: 79066
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and have you read what it says in the Java Language Specification about what members an array has? Or the documentation for toString()? When you have read them, it should be obvious why you are getting that output.
 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And please use Code Tags when posting java code.
 
Prabhudatta Choudhury
Greenhorn
Posts: 20
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:. . . and have you read what it says in the Java Language Specification about what members an array has? Or the documentation for toString()? When you have read them, it should be obvious why you are getting that output.




Hi Ritchie,

I have a requirement of getting String values from a vector which contains String objects. When I tried to iterate that vector on vector size to get those values, its giving the address of the values instead of giving real values.

Please let me know how can I get the real value inside the String array. Please refer the code snapet I posted in my earlier post.
 
Prabhudatta Choudhury
Greenhorn
Posts: 20
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ranganathan Kaliyur Mannar wrote:And please use Code Tags when posting java code.



Hi Ranganathan,

Please find the code with tags below.

[CODE STARTS]

import java.util.*;
public class Test {

private static Vector v1 = new Vector();
private static String[] sarr = null;
private static String s1 = " ";
private static int count =0;

public static void main(String a[])
{

v1.add("one");
v2.add("Two");
v3.add("Three");

System.out.println("Vector values are::" +v1);

for (count =0; count<=v1.size; count++)

{
if (v1.isEmpty() =! true && v1.size =! 0)
{

sarr = v1.toArray();
System.out.println("String array value is ::" +sarr);

s1 = sarr.toString();
System.out.println("String value are::" +s1);
}

}

}

}


[CODE ENDS]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code tags do not look like [CODE STARTS] and [CODE ENDS], but like this:

[code]
... put your code here
[/code]
 
Campbell Ritchie
Marshal
Posts: 79066
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Code tags do not look like [CODE STARTS] and [CODE ENDS], but like this:

[code]
... put your code here

[/code]

Somebody has added code tags . . . but they don't work because they haven't indented the code.
 
Campbell Ritchie
Marshal
Posts: 79066
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I, earlier wrote:. . . and have you read what it says in the Java Language Specification about what members an array has? Or the documentation for toString()? When you have read them, it should be obvious why you are getting that output.

Have you read both those links?
And have you read about the Vector#toString() method?
I know what the answer to your problem is, but it will do you no good if I simply tell you. If you have read those links and not understood them, then we are only to happy to try to help you understand, but, as it says on the "Beginning Java" home page, we do not simply give out such answers.
 
Prabhudatta Choudhury
Greenhorn
Posts: 20
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

I, earlier wrote:. . . and have you read what it says in the Java Language Specification about what members an array has? Or the documentation for toString()? When you have read them, it should be obvious why you are getting that output.

Have you read both those links?
And have you read about the Vector#toString() method?
I know what the answer to your problem is, but it will do you no good if I simply tell you. If you have read those links and not understood them, then we are only to happy to try to help you understand, but, as it says on the "Beginning Java" home page, we do not simply give out such answers.




I already tried with toString() and toArray() method but none of my use.I even tried to iterate the vector and get those data to put into a string variable, but that also no of use.
 
Sheriff
Posts: 22780
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's wrong with toString()? And more importantly, how do you want the Vector to be turned into a String? As in, what should the String look like?
 
Campbell Ritchie
Marshal
Posts: 79066
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob is right to go on about toString().

Like most beginners, you have put lots of code in which you don't actually need. You can for example get rid of all the fields, and the loop which you don't need. When you get rid of the compiler errors too, you will actually see your Vector turned into a String. I am a bit surprised you hadn't noticed that earlier.
Why have you used Vector rather than ArrayList?
Why have you used a raw type, rather than giving the List an actual type parameter?
 
Prabhudatta Choudhury
Greenhorn
Posts: 20
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Rob is right to go on about toString().

Like most beginners, you have put lots of code in which you don't actually need. You can for example get rid of all the fields, and the loop which you don't need. When you get rid of the compiler errors too, you will actually see your Vector turned into a String. I am a bit surprised you hadn't noticed that earlier.
Why have you used Vector rather than ArrayList?
Why have you used a raw type, rather than giving the List an actual type parameter?



Ritchie,

I used Vector as I need my collection to be synchronized after some part. If I would have taken ArrayList, I had to convert that arraylist to synchronized one. In my Vector only Bigdecimals are there with alphabets as well.I want them to be in one String variable so that I can append them as and when I need to.


 
Prabhudatta Choudhury
Greenhorn
Posts: 20
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:What's wrong with toString()? And more importantly, how do you want the Vector to be turned into a String? As in, what should the String look like?



Rob,

There is nothing wrong with toString(). But when I tried that it gave me "Type mismatch" error. That's why I tried to convert that vector to String araay and then String array to String. In my Vector only Bigdecimals are there with alphabets as well. I want them to be in one String variable so that I can append them as and when I need to.

 
Campbell Ritchie
Marshal
Posts: 79066
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's have another look at your initial code, copied and pasted without alterationNow, let's have the same code with indentation correctedNow we start commenting out codeYou don't need the loop. You can loop through the entire Vector and append the values to a StringBuilder, if you prefer. What you are doing is repeatedly going through your Vector, turning it into an array, printing the array, and then repeating it.
That is, if your Vector isn't empty. But if the Vector were empty, you would never get to the test about isEmpty() because the loop won't start. Then you test whether its size (it should be size() not size) is 0, but you know it isn't 0 because you have just tested it isn't empty.
By the way, it isn't =!. It's !=
So you have got all this code which you don't actually need. If you delete enough code, you will get something which will actually compile. Try it. Note I have changed the Vector to be a local variable, and called it v.

If you declared it as List<String> and initialised it as Vector<String>, you can be sure it only takes Strings. Then you can fiddle the toArray method by passing it a String[]. You will have to look in the List interface because the hyperlink doesn't copy to our tags correctly.The good news is, you have now got a String[] out of that for one line of code, rather than about 8 lines. If you want to use that String[] again, you will have to give it a name (identifier).
The bad news is, your String[] doesn't have an overridden toString() method. But I don't think you need that array at all. Look at the line of output above where it says[Ljava.lang.String@abdc1234.

Why are you trying to put BigDecimals and Strings into the same List? That sounds like suspect design to me.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic