The moose likes Beginning Java and the fly likes can we typecast String to String array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can we typecast String to String array" Watch "can we typecast String to String array" New topic

can we typecast String to String array

kalai sundar
Ranch Hand

Joined: May 25, 2005
Posts: 69
In my program i use String returval="";

whether i can typecast as String array?

for eg im mrogarm i am doing as follows

int len = rtvalue.length();
String[] newArr = new String[len];
newArr = (String) returval;

i am geting error vat should i do ? how to typecast string to stringarray

help me

Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
A String is an object. A String array is an Array object that holds Strings. A String array is not a String and a String is not a String array. They are two different objects.

Think of an array as a container. It is holding objects of the type you've declared. For example a String array can hold Strings.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
KasiMurugan Ramasamy
Ranch Hand

Joined: Jan 30, 2005
Posts: 125
Hi kalai,

You can't type cast from String to String array.

If you want create a String array from String means, you can use the following

String[] strArray = new String{ str1 };

Thanks & Regards
Kasimurugan (SCJP1.4, SCBCD1.3), Preparing SCWCD1.4
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
Why do you need to do this? Explain how your going to apply it and then maybe I can lead you to an alternative.
kalai sundar
Ranch Hand

Joined: May 25, 2005
Posts: 69
my problrm is first i will read text file, in that text file i want to count the occurance of given word,(given word in the sense, i will get the word from user).if that word occur in the text file, i want to replace that word with someother word.
till now vat i have done in my program is, i read a text file, so the content of the text file is string, i was trying to string to string[], so that i can put the string array in loop, and fine for the occurence of the string, can u suggest someother idea

with rgds
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
So you actually don't want an array with *one* string, but an array that in every string contains one word of the original string?

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Amol Fuke
Ranch Hand

Joined: Apr 08, 2005
Posts: 129
If you just want to find and replace the word with another then you can use replace(word1,word2) method of String/StringBuffer.That wud be easy.

"There are no mistakes, only lessons"
I agree. Here's the link:
subject: can we typecast String to String array
It's not a secret anymore!