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;
}
}
}
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?
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!
Re: Swing Dialog Boxes --- Have no clue why its not working
Code Java:
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;
}
}
}
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.
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.
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.
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.
Code java:
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;
}
}
}
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.
Re: Swing Dialog Boxes --- Have no clue why its not working
Thanks for helping me! I appreciate it!