File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Scanners?!  Where?! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Scanners?!  Where?!" Watch "Scanners?!  Where?!" New topic
Author

Scanners?! Where?!

Nick Garcia
Greenhorn

Joined: Dec 30, 2008
Posts: 15
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


Carpe diem, because every day is worth living to its fullest.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

What is "helper" object ? And about Scanner class, look here.

And I think this discussion helps you get some things about Scannaer class.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Nick Garcia
Greenhorn

Joined: Dec 30, 2008
Posts: 15
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

Joined: Jan 01, 2009
Posts: 874

Have a look at BufferedReader to understand the Java IO
Marky Vasconcellos
Ranch Hand

Joined: Jan 28, 2009
Posts: 36
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



Each of their nuggets of wisdom contracted to a sound bite: Joshua Bloch: Write Lots of Code; Chet Haase: Don't Put Your Entire Application in One Method; Masood Mortazavi: Start Simple and Keep Learning; Cay Horstmann: First, Don't Panic
Nick Garcia
Greenhorn

Joined: Dec 30, 2008
Posts: 15
Hmm...alright. I'll give it a shot and see what comes up. As always, thanks for the fast help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scanners?! Where?!