• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using arithmethic operation symbols in switch statements  RSS feed

 
Akimbas Akimbasas
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I need to write a program that asks for user user to choose two integers a and b and a symbol of arithmethic operation that will be done between these two integers. For example if the symbol is -> /.
Then the result needs to be like this: "a / b = answer". User can choose from four operations: (+, -, *, /).

This code is not complete, I only made it to do the first operation to test whether my way of working this problem out is correct, but it seems that here are problems. The first one is that (illegal start of expression) doesn't seem to work and after that that ( cannot find symbol - method nextChar() ) doesn't work either. So I would like to ask: How should I let user choose the symbol? Is char type not the right one ? How should I connect case command to a symbol?
Thanks.
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scanner does not have nextChar() method. You may need something else.
switch support certain primitives and enum, but not operator.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akimbas Akimbasas wrote:
This code is not complete, I only made it to do the first operation to test whether my way of working this problem out is correct, but it seems that here are problems. The first one is that (illegal start of expression) doesn't seem to work and after that that ( cannot find symbol - method nextChar() ) doesn't work either. So I would like to ask: How should I let user choose the symbol? Is char type not the right one ? How should I connect case command to a symbol?
Thanks.


Generally, character literals are surrounded with single quotes. Perhaps you meant to use '/'?

Henry
 
Akimbas Akimbasas
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Generally, character literals are surrounded with single quotes. Perhaps you meant to use '/'?

Henry


I see, now that resolves the problem of case error, thanks.

Raymond Tong wrote:Scanner does not have nextChar() method. You may need something else.
switch support certain primitives and enum, but not operator.


I don't need the operator, I only need the symbol, my mistake was that I wrote / instead of '/'.

So if Scanner doesn't support nextChar(); , how should I let the user choose a symbol with the code that I already have ?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
myScanner.next().charAt(0)???
 
Akimbas Akimbasas
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:myScanner.next().charAt(0)???

Yes, it worked, thanks. I would like to ask what charAt(0) means.
 
manish awara
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
charAt(0) means character at string's first position will accessed.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch manish awara
 
Akimbas Akimbasas
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manish awara wrote:charAt(0) means character at string's first position will accessed.

I see, thanks for explaining
 
manish awara
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you sir@Campbell Ritchi
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome, both of you
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!