0

Hello I'm new in using java i just need some advice..

this is what i need to do: the user inputted the date 8/15/2018, the console should have the output: "Hello! This is day 15 of the month of August in the year of our Lord 2018."

this is my code

    package newdate;
    import java.util.Scanner;
    public class NewDate {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

    System.out.print("Enter the date in mm/dd/yyyy: ");
    String str=sc.next();

    switch (str) {
        case 1:  str.substring(0,2) = "January";
                 break;
        case 2:  str.substring(0,2) = "February";
                 break;
        case 3:  str.substring(0,2) = "March";
                 break;
        case 4:  str.substring(0,2) = "April";
                 break;
        case 5:  str.substring(0,2) = "May";
                 break;
        case 6:  str.substring(0,2) = "June";
                 break;
        case 7:  str.substring(0,2) = "July";
                 break;
        case 8:  str.substring(0,2) = "August";
                 break;
        case 9:  str.substring(0,2) = "September";
                 break;
        case 10: str.substring(0,2) = "October";
                 break;
        case 11: str.substring(0,2) = "November";
                 break;
        case 12: str.substring(0,2) = "December";
                 break;
        default: str.substring(0,2) = "Invalid month";
                 break;

    }
    System.out.println("Hello! This is day " + str.substring(3,5) +" of the month of " + str.substring(0,2) +" in the year of our Lord" + str.substring(6,10));

}

    }

the problem is the data type, i want the month which is a number will be replace by a string(month) example: 11 = November

how can i do it?

Emhz
  • 3
  • 4
  • What is this code doing? Assigning a string to the result of a method call? – ernest_k Jul 23 '18 at 06:55
  • 1
    For production code one would use `LocalDate` and `DateTimeFormatter` from the standard library. Would this be OK for you, or are you exactly making the exercise without the standard classes in order to get programming experience? – Ole V.V. Jul 23 '18 at 07:00
  • `str.substring(0,2) = "January"` - that won't work. Why don't you build up a completely new result string instead of fiddling around with the input string? – Nico Haase Jul 23 '18 at 07:04
  • What do you expect `str.substring(0,2) = ...` to do? Apart from the fact that Strings are immutable, and Java returns by value (so trying to put a returned value on the left-hand side of an `=` probably won't do what you want), you seem to be trying to squeeze up to 13 characters into a place where there's only room for 3. You need a new variable. – Bernhard Barker Jul 23 '18 at 07:04
  • i just want to change the month(in numbers) into a string like 8 is August (Sorry for my bad english) – Emhz Jul 23 '18 at 07:05

3 Answers3

2

First, for real world programming one would use LocalDate from the standard library for holding a date and DateTimeFormatter for parsing the user entered date in a LocalDate. See the link at the bottom.

Most fundamentally, with your code you need a separate String variable for the name of the month. You cannot assign this back into the string you already have. Just declare String monthName; and assign it the proper value in your switch startement.

Also when str is a string and 1 is an int, you cannot switch on str and use 1 as a case label. The straightforward fix is to switch on str.substring(0,2) (the month number as string) and use "01", "02", etc. as case labels. Another common solution would be to parse the month number into an int (see the other link below or search for how to do it) and then keep you case labels 1, 2, etc., as they are.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1

Hello dear junior developer :) my suggestion is: first define an enumeration for month names:

 enum MonthName{
    January(0),
    February(1),
    March(2),
    April(3),
    May(4),
    June(5),
    July(6),
    August(7),
    September(8),
    October(9),
    November(10),
    December(11);

    private int number;

    MonthName(int number){
        this.number=number;
    }

    public static String findName(int number){
        for(MonthName monthName:MonthName.values()){
            if(monthName.number==number){
                return monthName.name();
            }
        }
        throw new RuntimeException("Invalid Month!");
    }
}

then define a class for your desire format:

class MyDateFormat{
    private int day;
    private String month;
    private int year;

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }
}

and finally create a method for separating your input string (8/15/2018):

 public MyDateFormat getFormatedDate(String dateString) throws ParseException {

    SimpleDateFormat dateFormat=new SimpleDateFormat("MM/dd/yyyy");
    Date date = dateFormat.parse(dateString);

    Calendar calendar=Calendar.getInstance();
    calendar.setTime(date);

    MyDateFormat myDateFormat=new MyDateFormat();
    myDateFormat.setDay(calendar.get(Calendar.DAY_OF_MONTH));
    myDateFormat.setMonth(MonthName.findName(calendar.get(Calendar.MONTH)));
    myDateFormat.setYear(calendar.get(Calendar.YEAR));

    return myDateFormat;
}

Good Luck :)

mina.ats
  • 11
  • 1
  • thanks! i'll try this other method too but i think it's a little consfusing, i'll just figure it out thanks! – Emhz Jul 23 '18 at 08:33
  • your welcome! the SimpleDateFormat could solve most of your problem but for retrieving month name you should write more codes. instead of using enum you can use an array of string that contains month names. – mina.ats Jul 23 '18 at 09:57
  • We already have a `Month` enum built Into Java, no need to write one. – Basil Bourque Jul 23 '18 at 13:26
  • 1
    This Answer uses terrible old date-time classes that were supplanted years ago by the *java.time* classes. Suggesting their use in 2018 is poor advice. See the [correct modern Answer](https://stackoverflow.com/a/51473315/642706) by Ole V.V. – Basil Bourque Jul 23 '18 at 13:28
  • 1
    That is right. my defined enum is redundant in java 8 – mina.ats Jul 24 '18 at 04:01
  • The code in this answer is indeed more confusing than needed (and I agree that you should avoid the old `SimpleDateFormat`, `Date` and `Calendar` classes since they are poorly designed; [java.time, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) is recommended). – Ole V.V. Jul 24 '18 at 07:05
0

Please try this.

try {
    String str ="8/15/2018";
    Date date=new SimpleDateFormat("MM/dd/yyyy").parse(str);
    System.out.println("Hello! This is day " + new SimpleDateFormat("dd").format(date) +" of the month of " + new SimpleDateFormat("MMMM").format(date) +" in the year of our Lord " + new SimpleDateFormat("yyyy").format(date));
} catch (ParseException e) {
    e.printStackTrace();
}
Suneet Patil
  • 608
  • 9
  • 21
  • @Emhz, you can use this, it will solve your issue in simple steps – Suneet Patil Jul 24 '18 at 06:14
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Jul 24 '18 at 07:00