Samasaur1 / CityManagement

A text-based city management game
3 stars 7 forks source link

Too large amounts crash #24

Closed Samasaur1 closed 5 years ago

Samasaur1 commented 6 years ago

Describe the bug The game crashes when an amount is too large

To Reproduce Steps to reproduce the behavior:

  1. Type buy food 1000000000000000
  2. See error

Expected behavior The game should tell me I can't afford it, that I can afford it, or that I couldn't but went into debt

Screenshots

screen shot 2018-07-02 at 5 40 12 pm

Desktop (please complete the following information):

Stack trace

Exception in thread "main" java.lang.NumberFormatException: For input string: "100000000000000000"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:583)
    at java.lang.Integer.parseInt(Integer.java:615)
    at com.samgauck.CityManagement.Command.buy(Command.java:114)
    at com.samgauck.CityManagement.Command.execute(Command.java:62)
    at com.samgauck.CityManagement.Main.main(Main.java:149)

Additional context I don't know the size necessary, only that it is large

Samasaur1 commented 6 years ago

I believe it stops working at a one with 10 zeros after it (i.e. 10000000000), but that a one with 9 zeros after it (i.e. 1000000000) works. That's what my testing showed

Samasaur1 commented 6 years ago

In addition, the stack trace shows that it's a NumberFormatException, while I am certain that my formatting was correct. Perhaps Integer.ParseInt has a limit?

Samasaur1 commented 5 years ago

It turns out that my failed values were out of Integer range. According to this SO post, I should use Long.parseLong() instead. However, I can just put a cap on the maximum amount at a time

Samasaur1 commented 5 years ago

I think I'm just going to put a cap on it

Samasaur1 commented 5 years ago

Although this brings up a good question: Should there be discounts for buying in bulk?

Samasaur1 commented 5 years ago

Amounts in buying and selling still need to be redone

Samasaur1 commented 5 years ago

Should be done now