aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strange Behaviour -- Pls Advice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strange Behaviour -- Pls Advice" Watch "Strange Behaviour -- Pls Advice" New topic
Author

Strange Behaviour -- Pls Advice

Sujatha Kumar
Ranch Hand

Joined: Jan 05, 2004
Posts: 134
Following is a simple java program, which is suppose to multiply two numbers based on the arguments passed.
For this program i passed the following values 2 3 *, where * is meant to multiply the numbers that i have passed.
I execute like this java Operations 2 5 *
But when print the values, The following are displayed

Arguments....... args[0]: 2
Arguments....... args[1]: 5
Arguments....... args[2]: Edit1

It is really strange, can anyone of you investigate, how "Edit1" is
passed...


public class Operations
{

public void displayMul(int valueI, int valueII, String enterSym)
{

int c = 0;

if (enterSym.equals("*"))
{
c = valueI * valueII;
System.out.println("The Multiplied values are : " + c);
}
}



public static void main(String args[])
{
Operations objOperator = new Operations();

System.out.println("Arguments....... args[0]: " + args[0]);
System.out.println("Arguments....... args[1]: " + args[1]);
System.out.println("Arguments....... args[2]: " +args[2]);

objOperator.displayMul(Integer.parseInt(args[0]),Integer.parseInt(args[1]),args[2]);

}

}


SCJP,SCWCD,SCBCD,SCEA Part I
Sujatha Kumar
Ranch Hand

Joined: Jan 05, 2004
Posts: 134
I got to know why "Edit1" is displayed,
It is because whenever * (asterik) is used in the dos prompt it refers to the list of file available in that directory. In my directory "Edit1" was the first file name.
Could anyone suggest how to pass a * (asterik) parameter to my program.
I know that we solve this problem by passing some other type of parameter like "MUL" or whatever.
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
Enclose the * in quotes

Note: This will work with Windows. I think it should be fine in Unix also. Else try with escape characters and see whether it works.

[ March 24, 2004: Message edited by: Mani Ram ]

Mani
Quaerendo Invenietis
Rahul Gujarathi
Greenhorn

Joined: Oct 23, 2002
Posts: 10
java Operations 2 5 "*"


Rahul G.<br />SCJP 1.4<br />-----------------<br />Be Good, Do Good!
Sujatha Kumar
Ranch Hand

Joined: Jan 05, 2004
Posts: 134
Hi Ram,
Thanks for your posting. "*" works but /* does not work i have tried it earlier.
Even '*' does not work.
Thanks once again.
Sujatha Kumar
Ranch Hand

Joined: Jan 05, 2004
Posts: 134
This behaviour prompts me to ask one more question.
In java all the arguments that you pass to the main method is considered as as string, In this case why we need to enclose the asterik within quotes.
Is " is used as escape character for *.
Pls Advice.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11509
    
  95

Hi Sujatha,
We don't have many rules here, but one we do have is the JavaRanch Official Policy On Displayed Names, which requires your displayed name to be a real name. Could you please change your displayed name to meet this policy? You can change it here.
Java doesn't need the quotes - your operating system does. What is happening is that the operating system is seeing the unquoted *, and converting it to a list of files in your current directory, then passing that to Java. By putting the * in quotes you are telling the operating system that you do intend to use the * as a parameter, and it should not convert it.
In most shells in Unix the star has to be inside single quotes: '*', as most shells will still convert parameters within double quotes. This can be powerful when you get used to it, but can cause all sorts of problems if you are not aware of it .
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Sujatha Kumar
Ranch Hand

Joined: Jan 05, 2004
Posts: 134
Hi Andrew,
I have changed the displayed name, as suggested.
Thanks for your detailed explaination. It was indeed very helpful for me to clear the fundamental doubts.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Sujatha
I think that that as *& is used to define all the files(eg *.bat for all bat files) the compiler wanted something that would differentiate a * from a "*". "*" may have been used over \* and '*' because as the command line agruments are String it was expecting a String. Because a "2" and 2 are the same when passed as command line arguments.
 
wood burning stoves
 
subject: Strange Behaviour -- Pls Advice