Rouxster
Rouxster

Reputation: 13

Calculate the hour plus one

Learning Java my current problem is with the "else" statement below. The result I am looking for is, if I type in 3 hours and 55 minutes: "Five to Four"

With the code below I get: Please enter the hours (between 1 and 12): 3 Please enter the minutes (between 1 and 60): 55 Five to Three

Can someone please tell me what am I doing wrong? (Thanks)

// actual calcuations

    if(minuteValue==0){
        result = hourPart + minutePart + " o'clock"; // eg two o'clock (the String o'clock defined later in the minute values)
    }
    else if (minuteValue <= 30 ){
        result = minutePart + " past " + hourPart; // eg ten past five
    }
    else
    {
       int remainingtime = 60 - minuteValue;
       int nexthr = hourValue + 1;
       if(nexthr==13) nexthr = 1;  

       result = getHourString(remainingtime) + " to " + hourPart; // eg twenty to five

    }

    return result;

Upvotes: 1

Views: 89

Answers (2)

Rouxster
Rouxster

Reputation: 13

String result;
int minuteValue = 55;
int hourValue = 9;

if(minuteValue==0){
    result = hourValue + minuteValue + " o'clock"; // eg two o'clock (the String o'clock defined later in the minute values)
}
else if (minuteValue <= 30 ){
    result = minuteValue + " past " + hourValue; // eg ten past five
}
else
{
   int remainingtime = 60 - minuteValue;
   int nexthr = hourValue + 1;
   if(nexthr==13) nexthr = 1;  
   result = remainingtime + " to " + nexthr; // eg twenty to five

}

System.out.println(result);

Upvotes: 0

Daniel Figueroa
Daniel Figueroa

Reputation: 10666

Well first of all you're not using nexthr:

result = getHourString(remainingtime) + " to " + hourPart; //where's nexthr?

Secondly you got way to much stuff going on here. Just have hourValue and minuteValue to keep track of the entered time. Change minutePart to minuteValue and hourPart to hourValue and you're done:

String result;
int minuteValue = 55;
int hourValue = 9;

if(minuteValue==0){
    result = hourValue + minuteValue + " o'clock"; // eg two o'clock (the String o'clock defined later in the minute values)
}
else if (minuteValue <= 30 ){
    result = minuteValue + " past " + hourValue; // eg ten past five
}
else
{
   int remainingtime = 60 - minuteValue;
   int nexthr = hourValue + 1;
   if(nexthr==13) nexthr = 1;  
   result = remainingtime + " to " + nexthr; // eg twenty to five

}

System.out.println(result);

Result: 5 to 10

Upvotes: 1

Related Questions