Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Input:12-4-78==OUTPUT:12041978 //Input:9-4-78==OUTPUT:09041978

Input:12-4-78==OUTPUT:12041978 //Input:9-4-78==OUTPUT:09041978

Scheduled Pinned Locked Moved Java
java
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 13244969
    wrote on last edited by
    #1

    THIS IS MY CODE BUT IT IS NOT GIVING THE RIGHT OUTPUT

    package main;
    import java.util.*;

    public class Year_Conversion {
    public static void main(String[] args) {
    String s1;
    System.out.println("Input string in the following format dd-mm-yy:");
    Scanner sc=new Scanner(System.in) ;
    s1=sc.next();
    convertDate(s1);
    }
    public static String convertDate(String s1)
    {
    String ans = null;
    String[] parts=s1.split("-");
    int d=Integer.parseInt(parts[0]);
    int m=Integer.parseInt(parts[1]);
    int y=Integer.parseInt(parts[2]);
    if(d<10)
    {
    ans=ans+"0"+d;
    }
    else
    {
    ans=ans+d;
    }
    if(m<10)
    {
    ans=ans+"0"+m;
    }
    else
    {
    ans=ans+m;
    }
    ans=ans+"19"+y;
    return ans;
    }
    }

    M Richard DeemingR J 3 Replies Last reply
    0
    • U User 13244969

      THIS IS MY CODE BUT IT IS NOT GIVING THE RIGHT OUTPUT

      package main;
      import java.util.*;

      public class Year_Conversion {
      public static void main(String[] args) {
      String s1;
      System.out.println("Input string in the following format dd-mm-yy:");
      Scanner sc=new Scanner(System.in) ;
      s1=sc.next();
      convertDate(s1);
      }
      public static String convertDate(String s1)
      {
      String ans = null;
      String[] parts=s1.split("-");
      int d=Integer.parseInt(parts[0]);
      int m=Integer.parseInt(parts[1]);
      int y=Integer.parseInt(parts[2]);
      if(d<10)
      {
      ans=ans+"0"+d;
      }
      else
      {
      ans=ans+d;
      }
      if(m<10)
      {
      ans=ans+"0"+m;
      }
      else
      {
      ans=ans+m;
      }
      ans=ans+"19"+y;
      return ans;
      }
      }

      M Offline
      M Offline
      Michael_Davies
      wrote on last edited by
      #2

      What output do you get? Rather than convert to integer to determine if you need to prefix a zero why not use strings length, if it is 1 you need a zero...

      1 Reply Last reply
      0
      • U User 13244969

        THIS IS MY CODE BUT IT IS NOT GIVING THE RIGHT OUTPUT

        package main;
        import java.util.*;

        public class Year_Conversion {
        public static void main(String[] args) {
        String s1;
        System.out.println("Input string in the following format dd-mm-yy:");
        Scanner sc=new Scanner(System.in) ;
        s1=sc.next();
        convertDate(s1);
        }
        public static String convertDate(String s1)
        {
        String ans = null;
        String[] parts=s1.split("-");
        int d=Integer.parseInt(parts[0]);
        int m=Integer.parseInt(parts[1]);
        int y=Integer.parseInt(parts[2]);
        if(d<10)
        {
        ans=ans+"0"+d;
        }
        else
        {
        ans=ans+d;
        }
        if(m<10)
        {
        ans=ans+"0"+m;
        }
        else
        {
        ans=ans+m;
        }
        ans=ans+"19"+y;
        return ans;
        }
        }

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3
        1. DON'T SHOUT! Typing in all capitals on the internet is considered shouting. Typing your entire message in capitals is rather rude. 2) If you want someone to help you get the "right output", then you need to tell us what the "right output" is. Show us several examples of what you type in, what the expected output is, and what the actual output is. Without that information, we can't help you.

        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        • U User 13244969

          THIS IS MY CODE BUT IT IS NOT GIVING THE RIGHT OUTPUT

          package main;
          import java.util.*;

          public class Year_Conversion {
          public static void main(String[] args) {
          String s1;
          System.out.println("Input string in the following format dd-mm-yy:");
          Scanner sc=new Scanner(System.in) ;
          s1=sc.next();
          convertDate(s1);
          }
          public static String convertDate(String s1)
          {
          String ans = null;
          String[] parts=s1.split("-");
          int d=Integer.parseInt(parts[0]);
          int m=Integer.parseInt(parts[1]);
          int y=Integer.parseInt(parts[2]);
          if(d<10)
          {
          ans=ans+"0"+d;
          }
          else
          {
          ans=ans+d;
          }
          if(m<10)
          {
          ans=ans+"0"+m;
          }
          else
          {
          ans=ans+m;
          }
          ans=ans+"19"+y;
          return ans;
          }
          }

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          Member 13276968 wrote:

          String ans = null;

          Should be empty string not null Also noting that the code you have is not printing the result.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups