简体   繁体   中英

Convert time to English word

I'm implementing a program that returns time from the input of an English name for a point in time. For instance, "ten minutes, past two", or "half past three". The program has to prompt the user once.

System.out.println("time is hours:minutes")

The user should not enter hours and then enter minutes separately. The input has to be hours colon minutes. For the method body I get a compile error on the array return.

public class TimeLab {

 public static void main(String[] args) {

  Scanner in = new Scanner(System.in);
  Date d = new Date();
  SimpleDateFormat SDF = new SimpleDateFormat("hh:mm");
  System.out.println("Enter the time");
  String sTime = SDF.format(d).toString();
  sTime = in .nextLine();
  int hours = Integer.parseInt(sTime);
  int minutes = Integer.parseInt(sTime);


  // output the current name of time, hours, minutes.

  // System.out.println("Current hours:minutes is " + sTime);

  System.out.println("Time is: " + getTimeName(hours, minutes));

  // prompt user for hours and minutes, make sure that the user is
  // notified we are using 12 hour time
  // filter against anything <1 and >12 for hours, <0 and >60 for minutes
  //
  // output the name of the imput time.
 }
 public static String getTimeName(int hours, int minutes) {

  if ((hours >= 1 && hours <= 12) && (minutes >= 0 && minutes <= 59)) {

   String hour_mint[] = {
    "",
    "One",
    "Two",
    "Three",
    "Four",
    "Five",
    "Six",
    "Seven",
    "Eight",
    "Nine",
    "Ten",
    "Eleven",
    "Twelve",
    "Thirteen",
    "Fourteen",
    "Fifteen",
    "Sixteen",
    "Seventeen",
    "Eighteen",
    "Nineteen",
    "Twenty",
    "Twenty one",
    "Twenty two",
    "Twenty three",
    "Twenty four",
    "Twenty five",
    "Twenty six",
    "Twenty seven",
    "Twenty eight",
    "Twenty nine"
   };

   String a;
   if (hours == 12)
    a = hour_mint[1]; // put 'one' if hour is 12
   else
    a = hour_mint[hours + 1]; // if hour is not 12 then store an hour ahead of given hour 

   System.out.print("time is : " + hours + ":" + minutes + ".");

   if (minutes == 0)
    System.out.println(hour_mint[hours] + "o'clock");
   else if (minutes == 15)
    System.out.println("Quarter past " + hour_mint[hours]);
   else if (minutes == 30)
    System.out.println("Half past" + hour_mint[hours]);
   else if (minutes == 45)
    System.out.println("Quarter to" + a);
   else if (minutes < 30) // for minutes between 1-29
    System.out.println(hour_mint[minutes] + " " + "past" + hour_mint[hours]);
   else // between 31-59
    System.out.println(hour_mint[60 - minutes] + " " + "to " + a);
  } else
   System.out.println("invalid time ");
  //String hour_mint = null;
  return hour_mint;
 }
}

ok I get it right, now.

public class TimeToEnglish{


  public static void main(String[] args) 
  { 

    Scanner in = new Scanner(System.in); 
    Date d = new Date();
    SimpleDateFormat SDF = new SimpleDateFormat ("hh:mm");
    System.out.println("Enter the time");
    String sTime = SDF.format(d).toString();
    sTime=in.nextLine();
    String sHours = sTime.substring(0,2);
    String sMinutes = sTime.substring(3);
    try{ int hours = Integer.parseInt(sHours);
      int minutes = Integer.parseInt(sMinutes);
      System.out.println("Time is: "+getTimeName(hours, minutes));
    }

      catch( NumberFormatException e){
        System.out.println("invalid input");//affiche une erreur ici
      }
    }
    public static String getTimeName(int hours, int minutes){
      String time_name = ""; 

      if((hours>=1 && hours<=12) && (minutes>=0 && minutes<=59)){

        String hour_mint []={"", "One", "Two", "Three", "Four", "Five", "Six","Seven", "Eight", "Nine","Ten",
          "Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen",
          "Twenty","Twenty one", "Twenty two", "Twenty three", "Twenty four", "Twenty five",
          "Twenty six","Twenty seven","Twenty eight", "Twenty nine"};


        String a;
        if (hours==12)
          a = hour_mint [1];// put 'one' if hour is 12
        else 
          a = hour_mint[hours+1]; // if hour is not 12 then store an hour ahead of given hour 

        System.out.print("time is : "+hours+":"+minutes+".");

        if (minutes==0)
          time_name = hour_mint[hours]+"o'clock";
        else if (minutes==15)
          time_name = "Quarter past "+hour_mint[hours];
        else if (minutes==30)
          time_name = "Quarter past "+hour_mint[hours];
        else if (minutes==45)
          time_name = "Quarter to"+a;
        else if (minutes<30) // for minutes between 1-29
          time_name = hour_mint[minutes]+" past "+hour_mint[hours];      
        else // between 31-59
          time_name = hour_mint[60-minutes]+" to "+a;

      }
      else 
        time_name = "invalid time format";

      return time_name;
    }
  }

So for the return problem. You can write

public static String[] getTimeName(int hours, int minutes) {

to return the Array. For the time i am not sure what you want to do, but you could read in the string and then split at ":". Then you have the two separate values which can be parsed to an int.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM