• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Switch Case Problem

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i had written a small code for calculator using switch case & getting the values at runtime. But multiplication is not happening.


what can be the solution for it?
 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Mohsin

Please UseAMeaningfulSubjectLine when you post anything on JavaRanch. Also please UseCodeTags when you post a source code - It's unnecessarily hard to read the code otherwise. I've edited your post accordingly.

Getting into your question, what did you mean by "multiplication is not happening.", are you getting any exception, incorrect result, or something else? Please TellTheDetails.
 
Ranch Hand
Posts: 34
1
MyEclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi mohsin

it seems that reading from consol as string * loss his identity. so convert in chararray and then use the first position value as operation.
 
Mohsin Shaikh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Devaka,

"Multiplication not happening" - by that i mean, its not giving any error but it is taking the program flow to default statement. Therefore unable to get multiplication result.



calc.jpg
[Thumbnail for calc.jpg]
Output
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest you find out what the JVM thinks args[2].charAt(0) is . . .

Insert this line before the switch block and see what it prints.
System.out.printf("args[2].charAt(0) = %c%n", args[2].charAt(0)); //test code
 
Greenhorn
Posts: 10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
put a print statement like this,


and see the result. if you don't quote your asterix "*" when you pass in your arguments, it will do a shell expansion.
so quote your "*" when you pass in as argument
 
Mohsin Shaikh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Jave - your name is almost Java & your post worked for me, Thanks
@Campbell Ritchie - Thanks
@Ravi - Thanks for your reply...

enJoy Guys!!
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic