Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scanners?! Where?!

 
Nick Garcia
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EDIT: Figured out how to properly put code into a post! Should look better now...

Hello,

I'm back with another question! It could most likely be answered by about 99% of this forum. (Maybe even 99.9%!!)

Here's my problem: I'm trying to make an application that's designed to run once and only once. The scenario is that I'm selling a product, and the program asks the customer how many of said product they want. Based on the amount given, they get a specific discount. (In this case, more than 100 in quantity gives them 50% off, 50-99 is 40%, 20-49 is 30%, 10-19 is 20%, and anything less gets no discount.)

I think I've set everything up correctly except for one main part. The user input. In C++ this equivalent would be "scanf("%i", &desiredQuantity);" or something along those lines. What would it be in Java?

Here's my code. Oh, and if you see room for improvement or other mistakes, please feel free to let me know! It's a simple program, I know. It's just practice. Because I definitely need it.


Thank you for your time!

-Nick
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is "helper" object ? And about Scanner class, look here.

And I think this discussion helps you get some things about Scannaer class.
 
Nick Garcia
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, well, basically what I'm aiming for is this: I want to ask the user to input their desired quantity, have the program store it as a variable ("quantity") then compare it to the if/else statements. I know how to do this in C++ but I can't figure it out in Java. I know it must be pretty simple. And it's probably right under my nose! I just can't seem to locate the command that would do that.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at BufferedReader to understand the Java IO
 
Marky Vasconcellos
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And.. you are using the quantity as a int but it was a String..

to make its convertation use Integer.parseInt(String str) method.




The Scanner object can read an int and make its automatically.

Something like

 
Nick Garcia
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm...alright. I'll give it a shot and see what comes up. As always, thanks for the fast help!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic