• Post Reply Bookmark Topic Watch Topic
  • New Topic

Command line arguments  RSS feed

 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I've developed this simple calculator program that pass three arguments to main method
as the following : java Calculator 2 + 5
and the output of the program is as following: 2+3=5
this will work with *,-,+,/

My problem in this line

as it want to check the kind of operation to take the corresponding calculations
but it give me an error.
how can i get rid of this error?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't use Strings in switch statements (using Java 6, it can be done with Java 7). You'll need to do something like
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or pick off the first character of the string, and switch on it as a char.

a friendly tip - instead of saying "i get an error", it can be of great help if you post the actual error you get:

Calculator.java:11: incompatible types
found : java.lang.String
required: int
switch(sign)
^
1 error

That let's whoever is reading your post focus in on where exactly the error is without moving it to their machine and compiling.
 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:or pick off the first character of the string, and switch on it as a char.

a friendly tip - instead of saying "i get an error", it can be of great help if you post the actual error you get:

Calculator.java:11: incompatible types
found : java.lang.String
required: int
switch(sign)
^
1 error

That let's whoever is reading your post focus in on where exactly the error is without moving it to their machine and compiling.

Thanks very much
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!