aspose file tools*
The moose likes Beginning Java and the fly likes Help with using Buffered Reader and if statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with using Buffered Reader and if statements" Watch "Help with using Buffered Reader and if statements" New topic
Author

Help with using Buffered Reader and if statements

Marc Caccamise
Greenhorn

Joined: Mar 12, 2012
Posts: 2
I'm fairly new in the world of java programming and I have had the misfortune of having a teacher who tends to act like I should already know what I'm doing despite being in an introduction class, so I have struggled to find ways to make sure I get my labs done on time and correctly and thanks to several different sources I have been able to do so and are now learning more and more. However this current lab I am having problems on. The purpose of my current lab is to enter the amount of 'coffee bags' being ordered and then arrange how many boxes of either large, medium, or small that the bags will be shipped in with a large box holding 20 bags, a medium holding 10, and a small holding 5. It's been made to understand that if statements are supposed to be used in this lab but since I've never been taught how to do so I'm very confused. I did believe that I completed the lab however I've been getting the error:

int numBags = Integer.parseInt(n);
^
symbol: variable n
location: class CoffeeBags

I'm assuming the problem might have to do with using BufferedReader but I'm not sure how to fix it, I am also having problems with operands in terms of where I try and make calculations for the box counts and number of bags left. Here is my code so far:




So if anyone could help me try and figure out what I'm doing wrong so I can finish this lab I'd appreciate it. Thanks!
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
The error is telling you - trying, anyway - that it doesn't know what 'n' is in the identified line. 'n' has not been declared, so the compiler is confused.


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36566
    
  16
Welcome to the Ranch

You ought to move 99% of that code out of the main() method. The main method is intended for starting your application off, not for incorporating “real” code in. You ought also to wrap the reader in try-finally blocks to make sure it is closed after use. There is an example which Rob Spoor showed me here.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

Marc Caccamise wrote:So if anyone could help me try and figure out what I'm doing wrong so I can finish this lab I'd appreciate it. Thanks!

As Greg said, the compiler doesn't know what 'n' is.

Another thing (although it may be a bit early yet): DON'T use floating-point types (double, float) for currency, you will end up with errors; particularly if multiplication is involved. You're much better off using ints or longs and calculating everything in pennies; or alternatively use BigDecimal.

This is one of the most 'linked-to' pages on forums like this, and explains exactly why you shouldn't; there may also be a simpler version on this forum (I'm sure one of the bartenders will tell you if there is).

HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Marc Caccamise
Greenhorn

Joined: Mar 12, 2012
Posts: 2
Ok I was able to get the lab to actually compile and work to a degree but its not 100% yet. The program works perfectly fine up until it is supposed to calculate how many small boxes need to be used to ship the order of 52 bags. The output correctly states that 2 Large boxes and 1 Medium Box are supposed to be used however the program states that the order will use 2 Small Boxes instead of 1 like it should be using. I don't know whether my math is wrong somewhere or whether the coding is flat out wrong, but like I said I'm fairly new to this and my teacher is more of a teach yourself type of person so its been hard to keep up. I'll put in the new updated code and any help is again appreciated. Thanks!

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18150
    
    8

Do some debugging and see what happens (or doesn't happen) to numBagsLeft in the calculation steps.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10924
    
  12

System.out.println is your best friend. put dozens of them in your code to see what it is really doing.

Paul gave you good advice...I don't think numBagsLeft is doing what you think it is.

Also, I would suggest changing your if-conditions. this in particular:

if(numBagsLeft > 0 && numBagsLeft <= 5){

is very confusing. I'd just change it to something like

if (smallBoxCount > 0)

I don't care how many bags the person entered, I really only care if I use any small bags. I'd make a similar change for the large and medium boxes as well.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36566
    
  16
Winston Gutkowski wrote: . . . This is one of the most 'linked-to' pages on forums like this, and explains exactly why you shouldn't; there may also be a simpler version on this forum (I'm sure one of the bartenders will tell you if there is). . . .
Try here, and look for No 20.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help with using Buffered Reader and if statements
 
Similar Threads
program assignment help
Total NOOB Needs Help Fast
When does Java allow console input?
reading from keyBoard
about Readline()