Wrong to process command line args in a loop?
I tried to process my command line args like this:
int[] rgb = {0,0,0};
for(int i=1;i<3;i++)
rgb[i-1] = Integer.parseInt(args);
I enter six args where args 1 to 3 are ints and not zero, but rgb[2] always ends up being zero. I had to do away with the loop and write out three assignment statements, but if I had ten or more args that would be a headache! Does anyone know why this is happening? I'm using JDK 1.3.1_15.
for(int i=0;i<3;i++)
rgb[i] = Integer.parseInt(args);
Look closely and make sure you understand the differences between my code and yours:
* i starts at 0, not 1
* rgb uses i for index, not i - 1.
Why would you have rgb using i -1 and args using i?
Now, if you have an arg [i]before the RGB args, then it would make sense for args' index to be different than rgb's. In that case, you might have i-1 and i, or i and i+1, etc. depending on how you structure your loop.
The main problem is that you had i=1; i<3, which will go through the loop body for i=1 and i=2 only. Make sure you understand why.
Similar Messages
-
I have this program and I need to use command line args which
will print ell . this program does this but I am using
substring instead of command line args.
public class h11 {
public static void main (String [] args ) {
String greeting = "Hello";
String h11 = greeting . substring (1, 4);
System.out.println( h11);
output: ell
Anyone..First off, the index of characters in a String is 0 based so in the string "Hello" you have the following;
char 0 = "H"
char 1 = "e"
char 2 = "l"
char 3 = "l"
char 4 = "o"
String.substring(start, end) is first inclusive and last exclusive so the parameters (1, 4) would return characters 1, 2 & 3 but not 0 or 4.
To get the whole word Hello (not withstanding that that's the entire string) you would want greeting.substring(0,5) however that would generate an index out of bounds error because 5 isn't a valid index for that string. So, here's a small sample program to show how to do what I think you're after.
public class ParmsTest{
public static void main(String[] args){
if( args.length > 0 ){
String parm = null ;
int end = 0 ;
int start = 0 ;
for( int i=0; i< args.length; i++ ){
parm = args[i] ;
if( parm.length() > 4 ){
end = 4;
else{
end = parm.length() - 1 ;
if( end > start ){
System.out.println( parm.substring(start, end) ) ;
else{
System.out.println( "Nothing to get" ) ;
System.out.println( "Finished processing parameters" ) ;
}It's a bit more complicated than your sample, but it also checks to see if there were any parameters, and it checks to see if the length we're trying to get is valid. Lastly it checks to see if there was anything to get before trying to print it. If it doesn't find anything to print it prints a message saying so.
Here's the question I have for you. Why? Why did I go to this trouble? -
Create .txt empty file After Message Processing Command Line Linux command
Hi All,
An empty .txt file has to be created in FTP location(FTP loc c:\xyz\Customer20082910.txt) only after the .dat file has been created(FTP loc c:\xyz\Customer20082910.dat)
Note:.dat has the actual data and .txt is an empty file and is created only after .dat is created
I know that .txt empty file can be created using the After Message Processing Command Line...
But confused which command to be given such that it will be created in the FTP location(c:\xyz\Customer20082910.txt)
Can any one please give the Linux command for it...
Regards,
SridharHi Aamir,
Thanks for the suggestion, I will ask my client to create a shell script at FTP server...
And one more question,
This is reverse side , now the .dat file is to be picked only if .txt exists at the FTP server (scenario File > XI>SAP) (earlier was SAP>XI>File)..
So please can you suggest for this also...
How can i check for the .txt and pick corresponding .dat file...
i had posted question long back
Need to pick .TXT file only if .CTL file is available
Regards,
Sridhar -
Include command line args in executable jar?
Hello all,
I'm trying to package up some software which takes 2 command line args to run (for example, java -jar prog.jar FooBar 3.14159). I'm using a utility called Jar2Exe to wrap it up into an exe (still needs JRE to run, but it's nicer packaging for distribution).
My question is, how can I included command line arguments in the jar? Or can I? Or, are there any exe-wrapping utilities out there that will allow me to accomplish this?
Thanks,
-ThokThok wrote:
Hello all,
I'm trying to package up some software which takes 2 command line args to run (for example, java -jar prog.jar FooBar 3.14159). I'm using a utility called Jar2Exe to wrap it up into an exe (still needs JRE to run, but it's nicer packaging for distribution).
My question is, how can I included command line arguments in the jar? Or can I? Or, are there any exe-wrapping utilities out there that will allow me to accomplish this?
Thanks,
-ThokOkay, did some more research... It appears you can't specify command line options in a jar, but if you package it into an exe, you can--depending on what utility you use to wrap it, that is. I downloaded a free one (Launch4j) and it does the trick.
-Thok -
Bash number of command line args....
Hi,
Is there a way in bash to get number of command line args ...
atm I am using $1 but of course if you pass a command like uname -a you need two?
wondered if there is a $<foo> that ill give number of args passed?
EDIT looks like '$#' emmmm lets see....woah!!! ok thanks ..... $# gives me number of args ... will check it out
$1 $2 > /tmp/cmd
messy way..... -
Hana db filesystem directory structure and process command line
Hi experts,
Could someone provide please actual directory structure of Hana db filesystem?
Generally speaking, I need to model some Hana db(of 1.0, 1.5 versions) entities. Among them:
Schemas
Data files
Log files
Important configuration files
Database parameters(guess found in config files and command line)
Database version
The question is where i can find such information having access to filesystem?
One another question is how common Hana db commandline looks like?
Does it have one process or several?
It would be perfect if someone give some ps -aef command result grepped with relevant Hana Db processes
Thanks,
Eugene
Edited by: Eugene Kondrashev on Dec 28, 2011 12:51 PMEugene Kondrashev wrote:
> * What are the processes relevant to Hana Db?
This is still changing pretty much ...
Anyhow, just check the running services in HANA studio (landscape tab) and you have all the process at hand.
> * What is the signature of starting command?
What is that supposed to mean?
> * How can I identify running Hana Db instance on a host having shell access?
How about the HDB command?
SUSE Linux Enterprise Server 11 (x86_64)
/usr/sap/HAN/HDB00> HDB help
Usage: /usr/sap/HAN/HDB00/HDB { start|stop|reconf|restart|version|info|proc|admin|kill|kill-<sig>|term }
kill or kill-9 should never be used in productive environment!
/usr/sap/HAN/HDB00> HDB info
USER PID PPID %CPU VSZ RSS COMMAND
hanadm 14682 14677 0.0 85580 2180 sshd: hanadm@pts/0
hanadm 14683 14682 3.3 16768 2980 \_ -bash
hanadm 14756 14683 18.1 13652 1680 \_ /bin/sh /usr/sap/HAN/HDB00/HDB info
hanadm 14782 14756 0.0 6620 892 \_ ps fx -U hanadm -o user,pid,ppid,pcpu,vsz,rss,args
hanadm 31638 1 0.0 42624 1820 sapstart pf=/usr/sap/HAN/SYS/profile/HAN_HDB00_vml3012
hanadm 31645 31638 0.0 528216 151400 \_ /usr/sap/HAN/HDB00/vml3012/trace/HDB.sapHAN_HDB00 -d -nw -f /usr/sap/HAN/HDB00/vml3012/daemon.ini pf=/usr/sap/HAN/SYS/profi
hanadm 31665 31645 0.5 5001972 1002844 \_ hdbnameserver
hanadm 31723 31645 0.0 4367024 244336 \_ hdbpreprocessor
hanadm 31744 31645 1.9 5664496 1779448 \_ hdbindexserver
hanadm 31752 31645 3.6 7197924 3359440 \_ hdbstatisticsserver
hanadm 31759 31645 0.6 5289164 1507684 \_ hdbxsengine
hanadm 32017 31645 0.0 429188 124344 \_ sapwebdisp_hdb pf=/usr/sap/HAN/HDB00/vml3012/wdisp/sapwebdisp.pfl -f /usr/sap/HAN/HDB00/vml3012/trace/dev_webdisp
hanadm 31527 1 0.0 292676 110380 /usr/sap/HAN/HDB00/exe/sapstartsrv pf=/usr/sap/HAN/SYS/profile/HAN_HDB00_vml3012 -D
> * Does Hana DB started with SAP start profile? If so, could someone point me to the example of such a profile?
?? seriously... take a guess
regards,
Lars -
Sun Studio 12 - Can't specify command line args while profiling
New to Sun Studio development environment, and having trouble profiling my application. I have specified command-line arguments for both Debug and Release builds in Project/Configuration Properties/Runninjg/Arguments and this works fine when running the project from within the IDE. However when I attempt to profile the application through IDE Advanced/Advanced Profiling/Start, my application is launched with no command line arguments. So far I have been unable to find any place to define command line arguments for the program when run using the profiler. Any insights would be greatly appreciated.
Regards,
Chad.Yes, there is a bug. I reproduced the problem using a sample project Welcome. If I run it without profiling, the program prints correct arguments:
Welcome ...
Arguments:
1: arg 1
2: arg 2
[Press Enter to close window] If I run it with profiling "Advanced->Advanced Profiling->Start", the program prints wrong arguments:
Running "/export/home/SunStudio/SUNWspro/prod/bin/collect -d /export/home/nikm/SunStudioProjects/Welcome/nbproject/private/experiments -o test.1.er -A on -p on -S on /export/home/nikm/SunStudioProjects/Welcome/dist/Debug/welcome arg 1 arg 2" in /export/home/nikm/SunStudioProjects/Welcome
Creating experiment database /export/home/nikm/SunStudioProjects/Welcome/nbproject/private/experiments/test.1.er ...
Welcome ...
Arguments:
1: arg
2: 1
3: arg
4: 2
5: null
Run successful. Exit value 0.Note, it got 5 arguments instead of 2.
So, there are two bugs:
1. argument with spaces is transformed to several arguments.
2. a "null" argument is added to the list.
I'll file this bug and let you know the number.
A workaround is to ignore the last argument in your
program if it is "null", but this is a very ugly workaround :-)
Thanks,
Nik -
there is command line argument fir integer like Integer.parseInt(args[0]); can we write same for "char" and" double" also like "Double.parseDouble" or else......
this has nothing to do with command line arguments
but yes, all the primitive wrapper classes can parse back their primitive
Learn to use the API, it's your friend
http://java.sun.com/javase/6/docs/api/ -
I'm a beginner in java and I need to know now to write the code that takes in a randon number of numbers at the command line and stores them in an array
ie. the program is called number and when I run it, it has to take in the numbers at the command line
ie. java numbers 12 13 14 15 19 1
then these numbers are to be stored in an array
How can I do this??
Thanking you
Tompublic class Numbers {
public static void main(String[] args) {
// Convert to integers
int[] param = new int[args.length];
for ( int cnt = 0; cnt < args.length; cnt++ ) {
try {
param[cnt] = Integer.parseInt(args[cnt]);
} catch (NumberFormatException nfe) {
System.err.println("Argument " + cnt + " is not integer, using '-1'");
param[cnt] = -1;
// use integers ...
} -
Reading directory path as command line args
Hello,
I'm trying to write a simple java web server that serves files from a specified directory over a specified port no. (both specified at the command line).
I am just wondering if any one has any advice on how to handle the directory argument and how check that it is valid etc.
Thanks
GaryTake a look at the File class, and the method isDirectory, and exists.
Kaj -
Environmental variables as command line args
Hello, all,
I would like to pass an environmental variable as a command line argument for a java program.
Example:
java MyClass $MY_VARIABLE
However, it actually passes "$MY_VARIABLE", and not the value of MY_VARIABLE. I know that I can use java -D(MY_VARIABLE=SOMETHING) and get it using System.getProperty ("MY_VARIABLE");
However, I would prefer it the first way, actually passing the variable inside command line. Is there a way to do this?
Thank you,
ElanaNevermind... Error in my code... It works
Thank you -
Command line args from java to C++
Hi
I want to pass command line arguemnts argc and argv from java to C++. I have browsed the net and most of them suggested that I declare the jni statement with jObjectarray.
I saw an example at http://www.csc.calpoly.edu/~fouzi/crossroads/Arguments.c
It said
#include "/home/java/linux/jdk118/include/jni.h"
#include "Arguments.h"
#include <stdio.h>
JNIEXPORT void JNICALL
Java_Arguments_setArgs (JNIEnv *jenv, jobject job, jobjectArray oarr)
/* obtain the size the the array with a call to the JNI function
GetArrayLength() */
jsize argc = (*jenv)->GetArrayLength(jenv, oarr);
/* Declare a char array for argv */
char const* argv[128];
int i;
for (i = 1; i < argc + 1; i++) {
/* obtain the current object from the object array */
jobject myObject = (*jenv)->GetObjectArrayElement(jenv, oarr, i-1);
/* Convert the object just obtained into a String */
const char str = (jenv)->GetStringUTFChars(jenv,myObject,0);
/* Build the argv array */
argv[i] = str;
/* print the argv array to the screen */
printf ("argv[%i] = %s\n",i,argv);
/* Free up memory to prevent memory leaks */
(*jenv)->ReleaseStringUTFChars(jenv, myObject, str);
// Increment argc to ajust the difference between Java and C arguments
argc++;
// Call a pipeline simulator function which uses command line arguments
start(argc,argv);
return;
That is what I have where the start method is declared like this:
void start(int argc, /* Argument count */ char argv[] / Argument strings */) {
But I get a warning when I build the C routine. The warning arises when I am calling the start method.
The warning says:
f:\jni\dsaccess.c(711) : warning C4090: 'function' : different 'const' qualifiers
f:\jni\dsaccess.c(711) : warning C4024: 'start' : different types for formal and actual parameter 2
Can you help?
Thanks
Lakshmi1. Your array building looks bogus: Are you missing an index?
2. You claim to be calling a "method". Are you? Or is it a subroutine? -
How to process command line options with plugin?
Hi there,
i am currently working on a c++ plugin for Adobe Acrobat 7 with the SDK 7.0.5.
I let the user open a specific pdf by clicking a link in the browser,
therefor I registered an additional protocol in the windows registry. Works fine.
But I now want to tell my plugin some specific things when calling acrobat.exe that way.
So is there a way to get self-specified command line parameters given to acrobat.exe?
It would also be helpful if my plugin would know the URL parameters appended with #.
I could also use that.
I could not find anything on that issue.
kind regards,
SebastianNo, your plugin doesn't get any access to the command line. Look for other methods of IAC (COM, DDE, shared memory, shared file, etc.)
-
SQLLDR Command line arg question.
Is there a way to override , from the command line,
the table_name of the ...
INTO TABLE table_name
... clause of SQLLDR's LOAD statement?
ie.
If my control file looked like this ...
LOAD DATA
INFILE *
APPEND
INTO TABLE my_table_1
could I have data loaded into my_table_2 like so ...
sqlldr scott/tiger@test TABLE=my_table_2
... I know that TABLE= will not work. But is there
a way to do something like this?oops! ignore this post
-
I am new to java and am trying to accept a integer from the command line and am completely stumped? How to a Output a Request for the number and then accept the number from the command line. Thanks
warner: its ok. you will have a better day soon. I have some homework. Can you help me? I post it
My code does not work
private void test(int number)
int half = number/2;
int copy = half * 2;
if (copy = number) System.out.println("odd");
else System.out.println("even");
}No matter what number it is always odd
Maybe you are looking for
-
Import error with MPEG-2 files: Brorsoft Video Converter to Apple Prores.
Hi, The Brorsoft Video Converter software gives me an 'import error' message when I attempt to import files from a folder on my desktop. That folder contains many mpg files (MPEG-2 Video, AC3). Those files in the folder on my desktop came from a Sony
-
Some calculation issues in Pivot table
Hi all, I am using OBIEE 10.1.3.4.1 We have one requirment like this: Product:1,2 Month Product Region District Sales Percent 201010 1 China Shanghai 200000 25.00%
-
Last changed date different between two systems after transport
Hi, first of all, I am not from basis background and just want to make sure whether is this something correct or not. I found that the "Last changed date" and "username" are not same between the original system and the target system. Is this somethin
-
I am new to final cut, and have been having a very tough time with a project that I am working on. In short, I am attempting to create a music video, and I began by laying the music tracks down first. As I go to cover the music with video, the video
-
Hi gurus, Can somone advise or suggest us in defining a new one or a standard sap listing procedure where it should be listed to only one specific site. example article X in article master should be listed only in one site b001 upon executing listing