Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String[] and String

Status
Not open for further replies.

skarosi

Programmer
Jan 25, 2004
140
GR
Hi all,
I need to pass a String[] to a method(main(String args[]) but the variable i have is a String. how can i format it to b accepted?
i have tryed some ways but they didnt work
and i cant find anything on the API.
thanks
 
You usually don't invoke method main, it's invoekn when you execute java ClassName -parameter1 -parameter2.

Anyway, you can try

Code:
SomeClass.main(new String[]{yourString});

Cheers,

Dian
 
this doesnt seem to work
when i call the method by:
MethodName(new String[] stringVariable);
i get an error:
array dimension missing

i even tried:

String[] variable = stringVariable;
i get:
incompatible types.
cheers
 
Hi,

It should be MethodName(new String[]{stringVariable}); if stringVariable is already defined as String. if not it shouled be MethodName(new String[]{"stringVariable"});


Cheers
Venu
 
String [] args <==> String args[]

These are the same, whereas the syntax is different.
This as i see is in the main(), so after:
1. javac appName.java
2. java appName arg1 arg2

args[0] will have the String "arg1", and
args[1] will have the String "arg2"

This array is dynamically created.

String arrName[] = new String[30] //will initialize an array (String array) with indexes 0, 1, 2 ,...29
To set value: arrName[23] = "blablabla";
 
skarosi: maybe you should start from the beginning, telling us what you want to do and provide a code snippet.

Cheers,

Dian
 
i thought that i did my self clear, but apparently not
ok, here it goes:
i have a method:
Code:
MethodName(String args[])
{...}
when i call this method, i call it with a String parameter:
Code:
Method2(){
  String stringVariable= "something i want to pass";
  MethodName(stringVariable);
}
This apparently doesnt work, cos one is String and the other String[]
i tried to create a new String[] variable and store stringvariable there but didnt work:
Code:
Method2(){
  String stringVariable= "something i want to pass";
  String stringArray[];
  stringArray[0]=stringVariable;  
  MethodName(stringArray);
}
and many other ways.
so, in other words, i want to pass a String variable to a methods that accepts String[].
is it possible?
cheers, Ilias
 
Your second attempt was almost correct. You didn't create the array, you just declared it. (Notice the new String[1] part.)
Code:
public class StringArrayTest {

	public static void main(String[] args) {
		
		String stringVariable = "Something to pass to the method.";
		String[] stringArray = new String[1];
		stringArray[0] = stringVariable;
		MethodTakingArray(stringArray);
	}

	static void MethodTakingArray(String args[]) {
		for (int i = 0; i < args.length; ++i)
			System.out.println(args[i]);
	}
}
 
Code:
Method2(){
  String stringVariable = new String("something i want to pass");
  MethodName(stringVariable);
}
 
Shouldn't it be :

Code:
MethodName(new String[] { "something" });

or :

Code:
String[] sArr = new String[1];
sArr[0] = "something";
MethodName(sArr);

--------------------------------------------------
Free Database Connection Pooling Software
 
that was great ppl! thanks a million
it was just the declaration of the size.
cheers
 
Glad you got it working.

Anyway, you must have been doing something wrong. You don't need to specify the size to build an String array.

Cheers,

Dian
 
well thats what i was thinking as well, thats why i didnt specify the size,but qhen i did, it start working.
so i guess that was the problem.
thanks again
 
If only you had wasted one minute to look at my first post...

you'd have seen the first dynamic array; and the second example where I must and do specify the size.


-bclt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top