0

Hello! I have an assignment where I have to take user input from my GUI (Month, date and year) and then send it to my CalendarUtil file where the input will be handled in the static methods. The part I an stuck on is the monthNumber method. I had written a test output to make sure the month is getting in there correctly and it is. I am obviously doing something wrong during the search for the month in monthsArray. I need this method to compare the user input "month" to the monthsArray and get the subscript of it's matching month. From there I will take the month subscript and return the value of that subscript from the daysArray. The Julian date will be processed in the GUI buttonOne ActionListener. Here is my code so far. Thanks in advance!

// Import statements 
import java.awt.*;
import javax.swing.*; 
import java.awt.event.*;

class JulianDateGUI extends JFrame
{
	private JPanel panel;
	private JLabel messageLabel1;
	private JLabel messageLabel2;
	private JLabel messageLabel3;
	private JLabel messageLabel4;
	private JTextField output;
	private JTextField Month;
	private JTextField dateIn;
	private JTextField Year;
	private JButton buttonOne;
	private JButton buttonTwo;
	
	public JulianDateGUI()
	{
		//Sets the title bar text.
		setTitle("Jessica Carey");
		
		//Sets action for the close button. 
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//Adds border layout manager to the content pane. 
		setLayout(new BorderLayout());
		
		//Creates five panels.
		JPanel panel1 = new JPanel();
		JPanel panel2 = new JPanel();
		JPanel panel3 = new JPanel();
		JPanel panel4 = new JPanel();
		JPanel panel5 = new JPanel();
		
		//Creates two buttons. 
		buttonOne = new JButton("Julian Date");
		buttonTwo = new JButton("End");
		
		//Creates labels
		messageLabel1 = new JLabel("Instructions");
		messageLabel2 = new JLabel("Month:");
		messageLabel3 = new JLabel("Date:");
		messageLabel4 = new JLabel("Year:");
	
		
		//Adds the label fields to the  panels.
		panel1.add(messageLabel1);
	    panel3.add(messageLabel4);
		panel4.add(messageLabel2);
		panel5.add(messageLabel3);
		
		
		//Creates a text field. 
		Month = new JTextField(6);
		dateIn = new JTextField(6);
		Year = new JTextField(6);
		output = new JTextField(25);
		
		//Adds the buttons to the panels. 
		panel2.add(buttonOne);
		panel2.add(buttonTwo);
		
		//Adds the text fields to the panels. 
	    panel4.add(Month);
	    panel5.add(dateIn);
	    panel3.add(Year);
	    panel2.add(output);
		
	    //Adds the panels to the content pane. 
		add(panel1, BorderLayout.NORTH);
		add(panel2, BorderLayout.SOUTH);
		add(panel3, BorderLayout.EAST);
		add(panel4, BorderLayout.WEST);
		add(panel5, BorderLayout.CENTER);
		
		//Adds action listener to the buttons. 
		buttonOne.addActionListener(new ButtonOneListener());
		buttonTwo.addActionListener(new ButtonTwoListener());
		
		//Pack and display the window. 
		pack();
		setVisible(true);
}
	// Creates ActionListener class for button one. Set to private.
    private class ButtonOneListener implements  ActionListener
    {
    	public void  actionPerformed(ActionEvent e)
    	{
    		// Determines which button is selected
    		String actionCommand = e.getActionCommand();
    		
    		if(actionCommand.equals("Julian Date"))
    		{
    			String inputMonth;
    			String inputDate;
    			String inputYear;
    			boolean year;
    			int month;
    			int date;
    			int julianDate;
    			
    			inputMonth = Month.getText().toUpperCase();
    			inputDate = dateIn.getText();
    			inputYear = Year.getText();
    			
    			year = CalendarUtil.isLeapYear(Integer.parseInt(inputYear));
    			month = CalendarUtil.monthNumber(inputMonth);
    			date = CalendarUtil.numberOfDays(Integer.parseInt(inputDate));
    			
    			if(year == true && month>1)
    			{
    				julianDate = date + month +1;
    			}else julianDate = date + month;
    			
    			output.setText("The Julian Date is: " + julianDate);
	
    			
    
    		}
    		
    	}
    		
    }
    
    //Creates ActionListener class for button two. Set to private. 
    private class ButtonTwoListener implements  ActionListener
    {
    	
    	
    	public void  actionPerformed(ActionEvent e)
    		
    	{	
    		// Determines which button is selected
    	   	String actionCommand = e.getActionCommand();

    		if(actionCommand.equals("End"))
    		{
    			System.exit(0);
    		}
    	}
    }
	public static void main(String[] args) 
    {
    	
    	JulianDateGUI JDG = new JulianDateGUI();
    	
    }
	
}
class CalendarUtil extends JulianDateGUI 
{
	
	private static String[] monthsArray = new String[] {"JAN", "FEB", "MAR", "APR", "MAY", "JUN",
						"JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
						
	private static int[] daysArray = new int[] {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
	
	public static boolean isLeapYear(int year)
	{
		boolean resultYear;
     	// is a leap year if statement is true
    	if((year % 4 == 0 && year % 100 != 0) || (year % 4 == 0 && year % 400 == 0 && year % 100 == 0))
     	{
     		resultYear = true;
     		return resultYear;
     	} else resultYear = false;
		
		return resultYear;
	}
	
	// THIS IS WHERE I NEED HELP!!!!:[
	public static int monthNumber(String month)
	{
		
		int index;
		for (index = 0;index<12; index++)
		{
			if (monthsArray[index] == month) 
			{
    			return index;
    		}else index = -1;
		}
		return index;
	}
	
	public static int numberOfDays(int date)
	{
		
			if (date > 31 && date < 1) 
			{
    			return -1;
			}else return date;
	}

}
1
Contributor
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Ikah
0

Nevermind, I got it to work!!!!!!! Here's the correct method if anyone is interested:

    public static int monthNumber(String month)
    {

        int index;
        for (index = 0;index<monthsArray.length; index++)
        {
            if (monthsArray[index].startsWith(month)) 
            {
                index = daysArray[index];
                return index;
            }
        }
        return index;
}

Edited by Nick Evan: Fixed formatting

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.