• Post Reply Bookmark Topic Watch Topic
  • New Topic

Scanner class  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have got a project where i need to use the Scanner class for input from user but, is there any function from the Scanner class that I can use to read a +,-,/,* operators from the users input.My code is complete but i just need that? Any ideas?
 
Ranch Hand
Posts: 186
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry vincent, I have edited my post as it might mess up the topic....
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
swaraj gupta wrote: . . . i think you can read them as integer(nextInt()) and then compare that value with ASCII value of these characters(i.e.operators.).
I am afraid you can't. There are various "find" methods in java.util.Scanner, which might be of use to you.

Beware: +-* are meta-characters in regular expressions, so you must escape them like \+. But \ needs to be escaped, so you end up writing \\+.
 
vincent donald
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
swaraj gupta wrote: . . . i think you can read them as integer(nextInt()) and then compare that value with ASCII value of these characters(i.e.operators.).
I am afraid you can't. There are various "find" methods in java.util.Scanner, which might be of use to you.

Beware: +-* are meta-characters in regular expressions, so you must escape them like \+. But \ needs to be escaped, so you end up writing \\+.


thanks for help.I am still a Biginner but I have done some research on the actual class that they want me to use and that is the TextIO.getChar() but my IDE netbean 6.9 doesnt accept it when I compile my program,But I no it is becuase there are no package for it!If there is any way that I can use that TextIO.getChar() or import it then my problem is solved.
 
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you getting this TextIO.java class? I only ask because it's not a part of the standard Java library.

Also, what is the purpose of *,+,/,-? are you reading operations in from a user? or are they just to separate input? in either case you can define your Scanner to use them to separate input from the user. Look up the java.util.Scanner useDelimiter() method

Hunter
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you may need to go farther with the regular expressions API.
 
vincent donald
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I think you may need to go farther with the regular expressions API.



Becuase this is the project:

Write a program that will evaluate simple expressions such as 17 + 3 and 3.14159 * 4.7. The expressions are to be typed in by the user. The input always consist of a number, followed by an operator, followed by another number. The operators that are allowed are +, -, *, and /. You can read the numbers with TextIO.getDouble() and the operator with TextIO.getChar(). Your program should read an expression, print its value, read another expression, print its value, and so on. The program should end when the user enters 0 as the first number on the line.

I really dont no how they could use the TextIO.getDouble and so on.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You ought to have been provided with the TextIO class. Put it in your folder, and try a class like thisThen you can see how it works.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!