Results 1 to 19 of 19
- 07-12-2009, 05:06 AM #1
Member
- Join Date
- Jul 2009
- Location
- batticaloa, Sri Lanka
- Posts
- 30
- Rep Power
- 0
String is not accepting "*" letter, please help me to solve
all the calculations are working exept multiply, i dont know why, please help me to solve thisJava Code:public class SimpleCalc { public static void main(String[] args) { int a = 0, b = 0, c = 0; String sign = null; boolean err = false; try { a = Integer.parseInt(args[0]); sign = args[1]; b = Integer.parseInt(args[2]); } catch (Exception e) { System.out.println("Error in arguments input, program terminated"); err = true; } if (sign.equals("+")) { c = a + b; } else if (sign.equals("-")) { c = a - b; } else if (sign.equals("*")) { c = a * b; } else if (sign.equals("/")) { c = a / b; } else { err = true; } if (err) { System.out.println("Error in operation, program terminated..."); System.exit(0); } else { System.out.println("Rersult of the calculation is " + c); } } }
-
run this app with 2 + 4 as the args and then 2 * 4, and note the difference:
* has meaning on the command line you know.Java Code:public class ReadArgs { public static void main(String[] args) { for (String arg : args) { System.out.println("-" + arg + "-"); } } }
- 07-12-2009, 05:44 AM #3
- 07-12-2009, 06:15 AM #4
Member
- Join Date
- Apr 2009
- Location
- Brisbane
- Posts
- 86
- Rep Power
- 0
As others have alluded to, but never quite said explicitly... on the command line, you need to quote the star (i.e: "*") to supress it's "special" meaning to the command shell (star means "all files" in both Winblows and *nix command shells).
*SimpleCalculator.java*
*SimpleCalculatorTest.bat* (note that the star is doublequoted).Java Code:package forums; public class SimpleCalculator { public static void main(String[] args) { try { int a = Integer.parseInt(args[0]); char sign = args[1].charAt(0); int b = Integer.parseInt(args[2]); int result = 0; switch (sign) { case '+': result = a + b; break; case '-': result = a - b; break; case '*': result = a * b; break; case '/': result = a / b; break; default: throw new IllegalArgumentException("unknown sign: "+sign); } System.out.println(a+" "+sign+" "+b+" = "+result); } catch (Exception e) { System.err.println(e.toString()+": "+e.getMessage()); } } }
Cheers. Keith.Java Code:---------- run bat ---------- C:\Java\home\src\forums>cd /d c:\java\home\src\forums C:\Java\home\src\forums>javac -d c:\java\home\classes SimpleCalculator.java C:\Java\home\src\forums>java -cp c:\java\home\classes forums.SimpleCalculator 1 + 1 1 + 1 = 2 C:\Java\home\src\forums>java -cp c:\java\home\classes forums.SimpleCalculator 5 - 2 5 - 2 = 3 C:\Java\home\src\forums>java -cp c:\java\home\classes forums.SimpleCalculator 2 "*" 1 2 * 1 = 2 C:\Java\home\src\forums>java -cp c:\java\home\classes forums.SimpleCalculator 4 / 2 4 / 2 = 2 Output completed (1 sec consumed)
- 07-12-2009, 02:16 PM #5
Member
- Join Date
- Jul 2009
- Location
- batticaloa, Sri Lanka
- Posts
- 30
- Rep Power
- 0
thanks guys..
im sorry for late reply, because i faced a power failure, so i couldnt reply..
thank goodness there are somany good people out here to help a new comer...
thanks a lot again....
- 07-12-2009, 03:17 PM #6
Member
- Join Date
- Jul 2009
- Location
- batticaloa, Sri Lanka
- Posts
- 30
- Rep Power
- 0
i think i have a proble, i tried the same way, i dont know why, by the way im using netbeans, and im passing arguments through Project config - arguments, and it all worked for all other expreesions, but not for this * mark...i dont know
corelttk, i tried ur method and it didnt seems to work.. i gave me the same result as before
java.lang.NumberFormatException: For input string: "build.xml": For input string: "build.xml"
i think maybe there is a problem in netbeans???
-
My advice: the command line is not a good place for a program like this as it forces too many kludges on you. How about doing it in a console window or (even better) a GUI.
- 07-12-2009, 03:34 PM #8
Member
- Join Date
- Jul 2009
- Location
- batticaloa, Sri Lanka
- Posts
- 30
- Rep Power
- 0
yes you are right, it is not a big deal, using a bufferedreader to read from console, but it is my curiosity why it didnt work?
-
Did you run the program that I posted? If so, the results should suggest the why here.
- 07-12-2009, 03:56 PM #10
Member
- Join Date
- Jul 2009
- Location
- batticaloa, Sri Lanka
- Posts
- 30
- Rep Power
- 0
-2-
-build-
-build.xml-
-manifest.mf-
-nbproject-
-src-
-test-
-2-
this is the result it gave me
- 07-12-2009, 03:58 PM #11
Member
- Join Date
- Jul 2009
- Location
- batticaloa, Sri Lanka
- Posts
- 30
- Rep Power
- 0
cn u explain what is this
String arg : args
as a beginner, i could not figure this out
-
If you also include this line in my code:
You'll see that it outputs a directory on your computer. Go to that directory and tell me the names of the files and folders that are present. Then you'll understand that * can be used as a wildcard for directorys and files on the command line and how this works.Java Code:System.out.println(System.getProperty("user.dir"));
best of luck.
-
This is a fancy for loop, called a for-each loopcn u explain what is this
String arg : args
It is equivalent to thisJava Code:for (String arg : args) { System.out.println("-" + arg + "-"); }
One word of unasked for advice: Please avoid unnecessary abbreviations in your posts here. I recommend this for several reasons:Java Code:for (for int i = 0; i < args.length; i++) { System.out.println("-" + args[i] + "-"); }
1) Programming is an exercise in precision. When you communicate here (or anywhere) about programming issues and questions, you want this communication to be as clear as possible to avoid any chance for ambiguity. I'd say at least half the answers here are requests for clarification. Let's avoid that.
2) For many here, English is not their first (or second or third) language. It's hard enough for them to understand what people are posting here much less if it's couched in obscure and non-standard abbreviations.
3) Extra letters don't cost anything here, so you might as well use them.
4) It makes the poster seem a bit immature and thus for some not worthy of help.
- 07-12-2009, 04:19 PM #14
Member
- Join Date
- Jul 2009
- Location
- batticaloa, Sri Lanka
- Posts
- 30
- Rep Power
- 0
I got your advice. sorry i will not use any abbreviations anymore.., by the way thanks for the explanations on for each loop:
yes i saw the directories and got the point, but sorry for disturbing you again and again, how can i use this "*" character as input argumentLast edited by srisar; 07-12-2009 at 04:23 PM.
-
so if you go to the C:\Users\Sri Saravana\Desktop\Java Works\Sample_Studies directory using Windows Explorer (if you are using a Windows OS), I'll bet you'll find these directories and files: build, build.xml, manifest.mf, nbproject, src, test
- 07-12-2009, 05:06 PM #16
Member
- Join Date
- Jul 2009
- Location
- batticaloa, Sri Lanka
- Posts
- 30
- Rep Power
- 0
yes i did. so is there is no other way to use this char '*', i did tried this : " * ", and it did worked, so it means i have to use space between this char.
-
Is it your teacher's requirement that you do this on the command line? Again, the best recommendation is not to do it this way unless you are being forced.
- 07-12-2009, 05:28 PM #18
Member
- Join Date
- Jul 2009
- Location
- batticaloa, Sri Lanka
- Posts
- 30
- Rep Power
- 0
yes, my teacher want me to take input like arguments, because he said input machanism is much complex than System.out.println(); so he said we will use arguments for now, but this calculator idea was mine. but im little bit worried that i could not finish it fully.
as you said it is not a big deal i think..., i will try to use bufferedReader instead. i found it some where on this forum some samples.
- 07-12-2009, 05:47 PM #19
you could also use 'X' and 'x' instead of '*'.
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
Similar Threads
-
How can I prevent "found void but expected java.lang.String" ?
By trueblue in forum New To JavaReplies: 3Last Post: 05-21-2009, 03:48 PM -
final String currentWorld = "Java Forums"; String.format("Hello %s", currentWorld);
By mcfrog in forum IntroductionsReplies: 0Last Post: 04-02-2009, 07:02 PM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
How to solve "No compiler error"?
By iceman in forum New To JavaReplies: 5Last Post: 04-22-2008, 03:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks