aspose file tools*
The moose likes Beginning Java and the fly likes getting error when converting vector to string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getting error when converting vector to string" Watch "getting error when converting vector to string" New topic
Author

getting error when converting vector to string

Prabhudatta Choudhury
Greenhorn

Joined: Jul 03, 2009
Posts: 20

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.


Thanks
Prabhudatta
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
. . . 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.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1075
    
  10

And please use Code Tags when posting java code.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Prabhudatta Choudhury
Greenhorn

Joined: Jul 03, 2009
Posts: 20

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

Joined: Jul 03, 2009
Posts: 20

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]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

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

[code]
... put your code here
[/code]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
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
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
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

Joined: Jul 03, 2009
Posts: 20

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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

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?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
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

Joined: Jul 03, 2009
Posts: 20

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

Joined: Jul 03, 2009
Posts: 20

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
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting error when converting vector to string
 
Similar Threads
problem with interfaces
implementing persistance with BigVector
arrayOutOfIndex
for loop logic problem