4

I just started learning Android Studio and this is my first project(Hello World Application). The project is to create an app where I have to return a greeting according to the time of day. The problem I am facing is that when I enter a name it shows "null (name) " instead of the appropriate greeting. I cannot understand why this is happening. I just need a hint or a nudge in the right direction.

Here is the result on the emulator: (https://prnt.sc/g5lc7e)

Here is my code for button pressed event:

@Override
    public void onClick(View v) {

        // get a reference to the TextView on the UI
        TextView textMessage = (TextView) findViewById(R.id.textMessage);

        //get a reference to the EditText so that we can read in the value typed
        // by the user
        EditText editFriendName = (EditText) findViewById(R.id.editFriendName);

        // get the name of the friend typed in by the user in the EditText field
        String friendName = editFriendName.getText().toString();

        //Get the time of day
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int hour = cal.get(Calendar.HOUR_OF_DAY);

        //Set greeting
        String greeting = null;
        if(hour>=6 && hour<12){
            greeting = "Good Morning";
        } else if(hour>= 12 && hour < 17){
            greeting = "Good Afternoon";
        } else if(hour >= 17 && hour < 21){
            greeting = "Good Evening";
        } else if(hour >= 21 && hour < 24){
            greeting = "Good Night";
        }

        //Change string displayed by TextView
        switch (v.getId()) {

        case R.id.greetButton:

         //set the string being displayed by the TextView to the greeting
         //message for the friend
        textMessage.setText( greeting + " " + friendName + "!");

        break;

        default:
            break;
        }

    }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 2
    What do you set greeting to when hour < 6? – user3486184 Aug 07 '17 at 21:07
  • as @user3486184 said add an `else` statement for all other hours or add an `else if(hour<6)`. Also, you do not need `cal.setTime(date)` because once you call [Calendar.getInstance()](https://developer.android.com/reference/java/util/Calendar.html#getInstance()), it initializes your calendar to the current time. – ArtiomLK Aug 08 '17 at 04:13
  • 1
    Does this answer your question? [Showing Morning, afternoon, evening, night message based on Time in java](https://stackoverflow.com/questions/27589701/showing-morning-afternoon-evening-night-message-based-on-time-in-java) – Gabor Mar 10 '22 at 00:19

1 Answers1

5

You can try like this, you missed less than 6 AM.

if(hour>= 12 && hour < 17){
    greeting = "Good Afternoon";
} else if(hour >= 17 && hour < 21){
    greeting = "Good Evening";
} else if(hour >= 21 && hour < 24){
    greeting = "Good Night";
} else {
    greeting = "Good Morning";
}
Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41