Avatar of Tolgar
Tolgar

asked on 

How to loop through an input array and assign each element to another array

Hi,
I have a perl code and I pass some variables to a java code. Some of these variables are arrays.

In the java code I want to pass these inputs (the ones that are arrays) to a variable in Java.

In order to do that, I pass the length of the array along with the array as a second variable to my java code.

Now, in Java I need to write a loop to go through this input array and assign each of its element to the variable in java by appending new line character between each of them.

Here is the sudo code:
 java.lang.Integer fileLength = args[3];
 //loop through args[4] for args[3] times and assign each value to fileList and append a new line //character at the end of each one
java.lang.String[] fileList = args[4]{i}+"\n";

Open in new window



In one case the variables are integers and the others are strings.

Can you please let me know how I can write it in Java for both cases?


Java

Avatar of undefined
Last Comment
for_yan
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Both target variables are of type String?
Avatar of Tolgar
Tolgar

ASKER

yes...
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

I would be inclined to something like the following:
public static String formatVariable(Object[] args, int length) {
	StringBuilder sb = new StringBuilder();
	String sep = "";
	for(int i = 0;i < args.length;i++) {	
	    sb.append(sep).append(args[i].toString());
	    sep = "\n";
	}
	return sb.toString();
    }

Open in new window

Avatar of for_yan
for_yan
Flag of United States of America image


It is better to use
System.getProperty("line.separator");
that should work on all OS

You probably may not need to use fileLength,
if your array was created correctly according to java
rules, but if it happens to have more elements that you actually need,
then better use fileLength, once they pass it to you.
This code should work for both Strings and int's


public  String getString() {
String linesep = System.getProperty("line.separator");
String s = "";
for(int j=0; j<fileLength; j++){
s += "" + args[j] + linesep;

}
return s;
}

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

It IS better to use ${line.separator} if you want platform portability. I was simply taking you literally with
>>
Now, in Java I need to write a loop to go through this input array and assign each of its element to the variable in java by appending new line character between each of them
>>

Don't use string concatenation though - oftentimes it will be inefficient
Avatar of Tolgar
Tolgar

ASKER

I think in both of these answers, you loop through the args.


>>//loop through args[4] for args[3] times
But I want to loop within the args[4] in my case.

args[4] is an array that I pass from Perl. I don't know the syntax but it may be something like this:

	    	   java.lang.Integer fileLength = args[3];
	     	   java.lang.String linesep = "";
	    	   java.lang.StringBuilder sb = new StringBuilder();
	    		for(int i = 0; i<fileLength; i++) {	
	    		    sb.append(sep).append(args[4][i].toString());
	    		    linesep = "\n";
	    		}

Open in new window


Because, if I loop through args then other args have other variables.

Thanks,
Avatar of for_yan
for_yan
Flag of United States of America image

So you mean that args[3] is just int
and args[4] is array ?

That seesm strange
Avatar of for_yan
for_yan
Flag of United States of America image

And waht is args[0], args[1] ?

What if you first try to prtint:

for (Object a : args){
System.out.println(a);

}

and let's see what will be the output
Avatar of Tolgar
Tolgar

ASKER

I have other stuff in args[0], args[1] and args [2].

They are all simple strings and I can easily assign them to a varaible in Java.

But the arrays part is tricky.

Do you have any idea?

Thanks,
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You just need to nest the loops - or apply the one loop to args[4]
Avatar of Tolgar
Tolgar

ASKER

Sorry but can you please show me how to do with the exact syntax?

 I am kind of new to java.

Thanks,
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Assuming your args[4] is the csv variable you mention, then the below (See http:#36913861 )
String[] args4 = args[4].split(",");
String s = formatVariable(args4, args4.length);

Open in new window

Avatar of Tolgar
Tolgar

ASKER

@CEHJ
1- How do you know that args[4] is separated by commas?

2- Does this append a new line character between every element of the array?


Thanks,
Avatar of for_yan
for_yan
Flag of United States of America image


Print your args in this way:
for (Object a : args){
System.out.println(a);

}

we'll at klleast have minimu idea what we
are talking about

and then we'll go from there

Look, how CEHJ can know anything about arg[4] ?
He just guesses.

If you have the above printout, we'll at least have minimu information and maybe will know where to start


Avatar of Tolgar
Tolgar

ASKER

by the way, everthing that I pass to java is string.
Avatar of Tolgar
Tolgar

ASKER

ID: 27343614

and this is how I came to this point. Maybe it help a little bit. Because it is hard for me to print out the inputs after I pass them from Perl. The code must start with Perl

Thanks
Avatar of for_yan
for_yan
Flag of United States of America image


If everything which you get into java is String, then how you make array of arg[4] - is it indeed a comma separated list, which
can be parsed as CEHJ sugggested, or maybe there is some other separator? Do you have any idea?
Avatar of Tolgar
Tolgar

ASKER

I mean all the variables that I pass into java are strings. Maybe it is unnecessary to say but I just wanted to mention it.

So all the args are strings. I think I am not mistaken. And It comes from here.

public static void main(java.lang.String args[])
       {  


Just an additional info.

Thanks,
Avatar of for_yan
for_yan
Flag of United States of America image

Looking at that question I'm acually thinking that
args[0] is  Login;
args[1] is number of records (let's say n)
args[2], args[3],....up to args[1+n]  - these are your elements

and I actually even had posted a copde how to read rthem in java

public ststic void main(String [] args){
String login = args[0];
String numRec = args[1];

int i = -1;

try{
i = Integer.parseInt(numRec);

}catch(Exception ex){
System.out.println("second argument should be int");
System.exit(0);
}

String [] recs = new String[i];

for(int j=0; j<recs.length; j++){

recs[j] = args[2+j];
}


}

Open in new window

:


Avatar of Tolgar
Tolgar

ASKER

It is a comma separated list as you said:

 This is how I call my java code:
my @javaCmd = ('java', '-jar', "/Tool/myTool.jar", 
		$something1, $something2, @something3, $lengthsomething3, @something4, 
		$lengthsomething4, @something5,  $lengthsomething5, $something6, $something7, 
		$something8, $something9, $something10);

Open in new window



Thanks,
Avatar of for_yan
for_yan
Flag of United States of America image

you cakll it this way but it is not a comma separated list it comes int java as array of args,
where args[0] corresponds to $something1
args[1] corresponds to $something2, etc.


But when you look at this:

my @javaCmd = ('java', '-jar', "$mydir/myTool/Tool-1.0.jar",
    "$Login",
    ($#Rec + 1),
    "@Rec",
    "$ID",
    "$clstr', 
    ... (the rest)

Open in new window


then in that code as I uderstand PHP will feed one string for "$Login",
one string for   ($#Rec + 1), but it would replace
  "@Rec" with many strings, actually with  ($#Rec + 1) - this number of strings

So when they arrive in java program it happens as if java was called with this command line

java -jar JarName.jar login_name 100(or number of elements in the following array)   element1 element2 element3 ....

so each eleemnet of that array will get numbers
args[2], args[3], args[4], etc

So you eed just to crate in java another array , say myArray
and shoudl assign

myArray[0] = args[2];
myArray[1] = args[3];
...

that's waht my code above will be doing

I still don't understand why you don't want to print it from
the beginnig of main

liek that:

for(String s: args){
System.out.println(s);
}

and it will facilitiate our understanding quite a llot







ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>1- How do you know that args[4] is separated by commas?

Because you said didn't you - possibly in your other q?
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of for_yan
for_yan
Flag of United States of America image

yes, so those arguments come separated with white spaces, not commas,
the only thing is to move them to start from from  zero'th  elemnt of the array,
so the code in ID:36921488 should work and plce them in the array recs
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo