Search code examples
javaswingvariablesstaticlocaldate

How to make variables usable by other methods


I'm trying to make a program that calculates the number of days between the day today and the day of birth (well, i'm still learning so the idea is not very bright). The program takes the date written by user from the keyboard and calculates the difference which is later outputted. I've made a getdifference method that calculates that.

The issue is, that i need a variable for day, month and year (inputted by user) which is later used by getdifference method. I know, that variables inside methods are local, so eclipse told me to convert them into static variables - I guess that's not the best idea, because they are (at least I think so) initiated as default values and the answer would always be 0.

The question is - how is it possible to make a value that might not be static and can be used in a method ? I've tried with setters,getters, but I think that could be an easier way, just couldn't find it. I have started coding not that long time ago, so any directions and tips would be appreciated, because to be honest I don't know any java developer, so cannot just give someone my code to check.

public class HowManyDaysTest {


     static String yearinStr;
     static String monthinStr;
     static String dayinStr;
     static String strLong;
     static long daysBetween;
     Temporal birthday;

     public void getDifference() {
     LocalDate today = LocalDate.now();
     try{

     birthday = LocalDate.of(Integer.valueOf(dayinStr),Integer.valueOf(monthinStr), Integer.valueOf(yearinStr)); 
     }
     catch(NumberFormatException ex) {
         System.out.println("Error");
     }
     try{
     daysBetween = ChronoUnit.DAYS.between(birthday, today); }

     catch(NullPointerException e) {
     System.out.println("Error"); }
     strLong = Long.toString(daysBetween);
     System.out.println(strLong);
     System.out.println(daysBetween);
     System.out.println(birthday);
     }


public HowManyDaysTest() {

    JFrame frame= new JFrame("How many days?");
    JLabel question= new JLabel("Please enter your date of birth");
    JTextField d = new JTextField("DD");
    JTextField m = new JTextField("MM");
    JTextField y = new JTextField("YYYY");
    JLabel ans= new JLabel();
    frame.setVisible(true);
    frame.setSize(500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(question,BorderLayout.NORTH);
    frame.add(d,BorderLayout.WEST);
    frame.add(m,BorderLayout.CENTER);
    frame.add(y,BorderLayout.EAST);
    frame.add(ans,BorderLayout.SOUTH);

    d.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent arg0) {
            d.setText("");

        }

        @Override
        public void focusLost(FocusEvent e) {
            // TODO Auto-generated method stub

        }

    });

    m.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent arg0) {
            m.setText("");

        }

        @Override
        public void focusLost(FocusEvent e) {
            // TODO Auto-generated method stub

        }

    });
    y.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent arg0) {
            y.setText("");

        }

        @Override
        public void focusLost(FocusEvent e) {
            // TODO Auto-generated method stub

        }

    });
    d.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            dayinStr = d.getText();
            Integer.parseInt(dayinStr);

        }

    });
    m.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            monthinStr = m.getText();


        }

    });
    y.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            yearinStr = y.getText();
            odp.setText(strLong);

        }

    });


}



public static void main(String[] args) {

    HowManyDaysTest app = new HowManyDaysTest();
    app.getDifference();

}

}

Solution

  • Declare the day, month and year text fields as instance variables and access them in getDifference() method.