Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Swing Dialog Boxes --- Have no clue why its not working

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy Swing Dialog Boxes --- Have no clue why its not working

    Please help me with my source code.

    import java.util.Scanner;

    public class ParisProject
    {
    public static void main(String[]args)
    {

    // declare and construct variables
    Scanner sc = new Scanner(System.in);
    String date, year;
    int day, month;
    boolean leapYear;

    // print prompts and get input
    System.out.println("\tARE YOU BORN ON A LEAPYEAR?");
    date=JOptionPane.showInputDialog(null,"Please enter your birthday in this format, mm/dd/yyyy.");
    date = sc.nextLine();
    day=JOptionPane.showInputDialog(null,"Please enter the day you were born on in this format, dd.");
    day= Integer.parseInt(formatDay(date));
    month=JOptionPane.showInputDialog(null,"Please enter the month you were born on in this format, mm.");
    month= flag(formatMonth(date));
    year=JOptionPane.showInputDialog(null,"Please enter the year you were born on in this format, yyyy.");
    year=formatYear(date);

    // calculations
    leapYear=leapYear(date);

    // output
    JOptionPane.showMessageDialog(null,"It is "+leapYear+" that the year "+year+" is a leap year",JOptionPane.PLAIN_MESSAGE);

    switch(month)
    {
    case 1:System.out.println("The date is: January "+day+", "+year); break;
    case 2:if(day<=28){
    System.out.println("The date is: February "+day+", "+year); }
    break;
    case 3:System.out.println("The date is: March "+day+", "+year); break;
    case 4:System.out.println("The date is: April "+day+", "+year); break;
    case 5:System.out.println("The date is: May "+day+", "+year); break;
    case 6:System.out.println("The date is: June "+day+", "+year); break;
    case 7:System.out.println("The date is: July "+day+", "+year); break;
    case 8:System.out.println("The date is: August "+day+", "+year); break;
    case 9:System.out.println("The date is: September "+day+", "+year); break;
    case 10:System.out.println("The date is: October "+day+", "+year); break;
    case 11:System.out.println("The date is: November "+day+", "+year); break;
    case 12:System.out.println("The date is: December "+day+", "+year); break;
    default: System.out.println("What planet are you living on with that kind of date? Please, try again!");
    }





    public static int formatMonth(String q)
    {
    String month = q.substring(0,2);
    int m = flag(Integer.parseInt(month));
    return m;
    }

    public static int flag(int month)
    {
    if(month==1)
    month=1;
    if(month==2)
    month=2;
    if(month==3)
    month=3;
    if(month==4)
    month=4;
    if(month==5)
    month=5;
    if(month==6)
    month=6;
    if(month==7)
    month=7;
    if(month==8)
    month=8;
    if(month==9)
    month=9;
    if(month==10)
    month=10;
    if(month==11)
    month=11;
    if(month==12)
    month=12;
    return month;
    }




    public static String formatYear(String q)
    {
    String year;
    year = q.substring(6,10);
    return year;
    }




    public static boolean leapYear(String q)
    {
    String year=q.substring(6,10);
    int leap=Integer.parseInt(year);
    {
    if ((leap%4)==0 && (leap%100)!=0)
    return true;
    if ((leap%4)==0&&(leap%100)==0&&(leap%400)==0)
    return true;
    else
    return false;
    }



    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,159
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default Re: Swing Dialog Boxes --- Have no clue why its not working

    When posting code, make sure you use the code tags to preserve formatting.

    What does "not working" mean?

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    jap2008 (November 18th, 2010)

  4. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Swing Dialog Boxes --- Have no clue why its not working

    It was running until i tried using swing Dialog boxes in my source code. So, now its not compling which means it still needs debugging.. but I do not know where I went wrong? or how to get it to work with the swing dialog boxes. Please help!

  5. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Swing Dialog Boxes --- Have no clue why its not working

    import java.util.Scanner;
     
    public class ParisProject
    {
      public static void main(String[]args)
      {
     
          // declare and construct variables
          String date, year;
          int day, month;
          boolean leapYear;
     
          // print prompts and get input
          System.out.println("\tARE YOU BORN ON A LEAPYEAR?");
          date=JOptionPane.showInputDialog(null,"Please enter your birthday in this format, mm/dd/yyyy.");
              date = sc.nextLine();
          day=JOptionPane.showInputDialog(null,"Please enter the day you were born on in this format, dd.");
              day= Integer.parseInt(formatDay(date));
          month=JOptionPane.showInputDialog(null,"Please enter the month you were born on in this format, mm.");
              month= flag(formatMonth(date));
          year=JOptionPane.showInputDialog(null,"Please enter the year you were born on in this format, yyyy.");
              year=formatYear(date);
     
          // calculations
          Scanner sc = new Scanner(System.in);
          leapYear=leapYear(date);
     
          // output
          JOptionPane.showMessageDialog(null,"It is "+leapYear+" that the year "+year+" is a leap year",JOptionPane.PLAIN_MESSAGE);
     
     
    switch(month)
    {
          case 1:System.out.println("The date is: January "+day+", "+year); break;
      case 2:if(day<=28){
        System.out.println("The date is: February "+day+", "+year); }
            break;
          case 3:System.out.println("The date is: March "+day+", "+year); break;
          case 4:System.out.println("The date is: April "+day+", "+year); break;
          case 5:System.out.println("The date is: May "+day+", "+year); break;
          case 6:System.out.println("The date is: June "+day+", "+year); break;
          case 7:System.out.println("The date is: July "+day+", "+year); break;
          case 8:System.out.println("The date is: August "+day+", "+year); break;
          case 9:System.out.println("The date is: September "+day+", "+year); break;
          case 10:System.out.println("The date is: October "+day+", "+year); break;
          case 11:System.out.println("The date is: November "+day+", "+year); break;
          case 12:System.out.println("The date is: December "+day+", "+year); break;
          default: System.out.println("What planet are you living on with that kind of date? Please, try again!");
    }
     
     
     
     
     
    public static int formatMonth(String q)
    {
    String month = q.substring(0,2);
    int m = flag(Integer.parseInt(month));
    return m;
    }
     
     public static int flag(int month)
        {
          if(month==1)
            month=1;
             if(month==2)
            month=2;
             if(month==3)
            month=3;
             if(month==4)
            month=4;
            if(month==5)
            month=5;
            if(month==6)
            month=6;
            if(month==7)
            month=7;
            if(month==8)
            month=8;
            if(month==9)
            month=9;
            if(month==10)
            month=10;
            if(month==11)
            month=11;
            if(month==12)
            month=12;
            return month;
      }
     
     
     
     
    public static String formatYear(String q)
    {
    String year;
    year = q.substring(6,10);
    return year;
    }
     
     
     
     
    public static boolean leapYear(String q)
    {
    String year=q.substring(6,10);
    int leap=Integer.parseInt(year);
    {
    if ((leap%4)==0 && (leap%100)!=0)
      return true;
    if ((leap%4)==0&&(leap%100)==0&&(leap%400)==0)
      return true;
    else
      return false;
    }
     
     
     
    }
    }

  6. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Swing Dialog Boxes --- Have no clue why its not working

    By default, the value the user inputted is a String. You are first setting the int day to a String value.

  7. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Swing Dialog Boxes --- Have no clue why its not working

    Your showMessageDialog

    JOptionPane.showMessageDialog(null,"It is "+leapYear+" that the year "+year+" is a leap year",JOptionPane.PLAIN_MESSAGE);

    Doesn't have the correct parameters required for that method.

    Also, you never defined the method formatDay.

  8. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Swing Dialog Boxes --- Have no clue why its not working

    date=JOptionPane.showInputDialog(null,"Please enter your birthday in this format, mm/dd/yyyy.");
    date = sc.nextLine();

    No.

    They enter it in the dialog box. You don't need to get it from the console.

  9. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Swing Dialog Boxes --- Have no clue why its not working

    I got it to compile, though it probably doesn't work correctly as I had to change some things around.
     
    import java.util.Scanner;
     
    import javax.swing.JOptionPane;
     
    public class ParisProject
    {
      public static void main(String[]args)
      {
     
          // declare and construct variables
          String date, year, day2, month2, year2;
          int day, month;
          boolean leapYear;
     
          // print prompts and get input
          System.out.println("\tARE YOU BORN ON A LEAPYEAR?");
          date=JOptionPane.showInputDialog(null,"Please enter your birthday in this format, mm/dd/yyyy.","ARE YOU BORN ON A LEAPYEAR?", JOptionPane.PLAIN_MESSAGE );
             //  date = sc.nextLine();
          day2=JOptionPane.showInputDialog(null,"Please enter the day you were born on in this format, dd.");
              day= Integer.parseInt((day2));
          month2=JOptionPane.showInputDialog(null,"Please enter the month you were born on in this format, mm.");
              month= flag(formatMonth(date));
          year=JOptionPane.showInputDialog(null,"Please enter the year you were born on in this format, yyyy.");
              year=formatYear(date);
     
          // calculations
          Scanner sc = new Scanner(System.in);
          leapYear=leapYear(date);
     
          // output
          JOptionPane.showMessageDialog(null,"It is "+leapYear+" that the year "+year+" is a leap year", "Huh?", JOptionPane.PLAIN_MESSAGE);
     
     
    switch(month)
    {
          case 1:System.out.println("The date is: January "+day+", "+year); break;
      case 2:if(day<=28){
        System.out.println("The date is: February "+day+", "+year); }
            break;
          case 3:JOptionPane.showMessageDialog(null, "The date is: March "+day+", "+year, "Title", JOptionPane.INFORMATION_MESSAGE); break;
          case 4:
        	  System.out.println("The date is: April "+day+", "+year); break;
          case 5:System.out.println("The date is: May "+day+", "+year); break;
          case 6:System.out.println("The date is: June "+day+", "+year); break;
          case 7:System.out.println("The date is: July "+day+", "+year); break;
          case 8:System.out.println("The date is: August "+day+", "+year); break;
          case 9:System.out.println("The date is: September "+day+", "+year); break;
          case 10:System.out.println("The date is: October "+day+", "+year); break;
          case 11:System.out.println("The date is: November "+day+", "+year); break;
          case 12:System.out.println("The date is: December "+day+", "+year); break;
          default: System.out.println("What planet are you living on with that kind of date? Please, try again!");}
    }
     
     
     
     
     
    public static int formatMonth(String q)
    {
    String month = q.substring(0,2);
    int m = flag(Integer.parseInt(month));
    return m;
    }
     
     public static int flag(int month)
        {
          if(month==1)
            month=1;
             if(month==2)
            month=2;
             if(month==3)
            month=3;
             if(month==4)
            month=4;
            if(month==5)
            month=5;
            if(month==6)
            month=6;
            if(month==7)
            month=7;
            if(month==8)
            month=8;
            if(month==9)
            month=9;
            if(month==10)
            month=10;
            if(month==11)
            month=11;
            if(month==12)
            month=12;
            return month;
      }
     
     
     
     
    public static String formatYear(String q)
    {
    String year;
    year = q.substring(6,10);
    return year;
    }
     
     
     
     
    public static boolean leapYear(String q)
    {
    String year=q.substring(6,10);
    int leap=Integer.parseInt(year);
    {
    if ((leap%4)==0 && (leap%100)!=0)
      return true;
    if ((leap%4)==0&&(leap%100)==0&&(leap%400)==0)
      return true;
    else
      return false;
    }
     
     
     
    }
    }

  10. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Swing Dialog Boxes --- Have no clue why its not working

    Well, it looks like it's working so far.

    It's very picky with the format.

  11. The Following User Says Thank You to javapenguin For This Useful Post:

    jap2008 (November 18th, 2010)

  12. #10
    Junior Member
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Swing Dialog Boxes --- Have no clue why its not working

    Thanks for helping me! I appreciate it!

Similar Threads

  1. Simple Input Dialog Question
    By tabutcher in forum Java Theory & Questions
    Replies: 0
    Last Post: March 1st, 2010, 10:10 PM
  2. Replies: 0
    Last Post: February 2nd, 2010, 07:20 AM
  3. how to write an if statement which evaluates 3 dialogue boxes
    By humdinger in forum Loops & Control Statements
    Replies: 6
    Last Post: January 14th, 2010, 12:28 PM
  4. [SOLVED] Help with dialog box
    By 03EVOAWD in forum AWT / Java Swing
    Replies: 5
    Last Post: August 4th, 2009, 11:06 AM
  5. Replies: 4
    Last Post: January 26th, 2009, 11:03 PM