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

Help with using Buffered Reader and if statements

 
Marc Caccamise
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marc Caccamise
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do some debugging and see what happens (or doesn't happen) to numBagsLeft in the calculation steps.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12198
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic