File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange Behaviour -- Pls Advice

 
Sujatha Kumar
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]);

}

}
 
Sujatha Kumar
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rahul Gujarathi
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java Operations 2 5 "*"
 
Sujatha Kumar
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11833
181
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sujatha Kumar
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic