0

I'm trying to make a method where the user inputs the hour (eg. 13:00 for 1pm) into the console (this is a console application) and depending on the hour of the day, my object would be a certain price. I've got Java to recognise the hours, but I'm having so much trouble with the early morning hours (00:00-04:59).

I've converted the time into an integer using substring and parseInt and the if statement below works well up until 23:59, if I type 01:00 or 03:30, nothing happens. Here's what I've done so far, I hope that you can help me. :)

int hrs = Integer.parseInt(hrsString);
int mHrs = 0;
if (hrs == 00)
  mHrs = 24;
if (hrs == 01)
  mHrs = 25

Etc. up to 04

if ((hrs >= 22) && (mHrs <= 28))
   price = 100.00
else if ((hrs >= 05) && (hrs <= 16))
   price = 20.00

Etc. up to 21.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
TMI
  • 65
  • 1
  • 9
  • 6
    Numbers starting with 0 are octals. Change `01` to `1` See http://stackoverflow.com/questions/565634/integer-with-leading-zeroes – Ruan Mendes Jun 20 '16 at 15:45
  • You should be able to remove leading zeros using `substring`. – noumenal Jun 20 '16 at 15:47
  • 1
    That's what I'm doing, I'm not using the minutes, I have a separate minString – TMI Jun 20 '16 at 15:47
  • 1
    Use the java 8 time API to deal with times as `LocalTime` instances (or jodatime in pre Java 8). – Andy Turner Jun 20 '16 at 15:49
  • 2
    Aren't `1` and `01` the same value? – Scott Hunter Jun 20 '16 at 15:50
  • 1
    @noumenal if I've only got two characters can I use CharAt? – TMI Jun 20 '16 at 15:50
  • Sure, but you would have to treat the return value as a `char`: `hrs.charAt(0) == '0'` – noumenal Jun 20 '16 at 16:00
  • And after that do Integer.parseInt? That's not my problem though. – TMI Jun 20 '16 at 16:02
  • `parseInt` expects a string, not a char. Read the Java Documentation. If you have to deal with a char, you need something like `String.valueOf(char)`. To avoid this back and forth casting, just use the substring: `hrsString.substring(0)` – noumenal Jun 20 '16 at 16:07
  • if (hrsString.substring(0, 0) == 0) { hrsString = hrsString.substring(1); //Getting rid of a leading zero } – noumenal Jun 20 '16 at 16:22
  • static int DAY = 6; static int EVENING = 16; static int NIGHT = 22; static double day_fare = 100.0; static double evening_fare = -99.0; //Not sure what you want here static double night_fare = 20.0;` – noumenal Jun 20 '16 at 16:23
  • And then: if (hrs >= DAY && < EVENING) { price = day_fare; } else if (hrs >= EVENING && < NIGHT) { price = evening_fare; } else if (hrs >= NIGHT && < DAY) { price = night_fare; } else { System.out.println("Error"); } – noumenal Jun 20 '16 at 16:23
  • Using the built-in [`LocalTime`](http://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html) class would be much easier. It knows how to parse strings. It knows how to compare. – Basil Bourque Jun 20 '16 at 17:32

1 Answers1

0

Your problem lies here:

if ((hrs >= 22) && (mHrs <= 28))
   price = 100.00
else if ((hrs >= 05) && (hrs <= 16))
   price = 20.00

Based on the code fragments you've provided, when it's 03:00 then hrs will be 3 and mHrs will be 27. So it will not be the case that (hrs >= 22) && (mHrs <= 28). This will never happen in the early hours of the morning.

I'm not sure what the point of mHrs is, but if it serves no other purpose than this line, I would suggest ditching the variable altogether and using this:

if ((hrs >= 22) || (hrs <= 4))
   price = 100.00
else if ((hrs >= 05) && (hrs <= 16))
   price = 20.00

Otherwise, if you need mHrs for other purposes, you could try this:

if ((hrs >= 22) || (mHrs >= 24 && mHrs <= 28))
   price = 100.00
else if ((hrs >= 05) && (hrs <= 16))
   price = 20.00
dcsohl
  • 7,186
  • 1
  • 26
  • 44