We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,201 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

not working

import javax.swing.* ;
 import java.awt.event.*;
 import java.awt.*;
 import java.io.*;
 import java.util.Calendar;
 import java.util.*;
 import java.text.*;
 import java.util.Date;


  /*
  <applet code="gui.class" width=250 height=100>
  </applet>
  */


 public class gui1  extends  JApplet  implements  ActionListener
 {
     JTextField t1,t2,t3,t4,t5;
     JLabel l1,l2,l3,l4,l5,l6;
     JButton b1,b2;
     Calendar c1,c2,c3;
     JPanel p,p1,p2;
     Date sd,sd1;
     SimpleDateFormat sdf;
     public void init()
     {
        SimpleDateFormat sdf = new SimpleDateFormat("DD/MM/YYYY");

         final  JTextField t1 = new JTextField(10);
         final  JTextField t2 = new JTextField(10);
         final  JTextField t3 = new JTextField(10);
         final  JTextField t4 = new JTextField(10);
         final  JTextField t5 = new JTextField(10);

         JLabel l6 = new JLabel("fine");
         JLabel l1 = new JLabel("Book Name");
         JLabel l2 = new JLabel("Author Namae");
         JLabel l3 = new JLabel("Issue Date");
         JLabel l4 = new JLabel("Last Date");
         JLabel l5 = new JLabel("Submitted Date");

          JButton b1=new JButton("Issue");
          JButton b2=new JButton("Clear");

          setLayout(new BorderLayout());

          JPanel p=new JPanel();
           p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));  
           p.add(l1);

           p.add(l2);

           p.add(l3);


           p.add(l4);
           p.add(l5);
            JPanel p1=new JPanel();
             p1.setLayout(new BoxLayout(p1,BoxLayout.Y_AXIS));
            p1.add(t1);
           p1.add(t3);
            p1.add(t2);
           p1.add(t4);

           p1.add(t5);

       add(p,BorderLayout.WEST);
       add(p1,BorderLayout.EAST);

        JPanel p2=new JPanel();
         p2.add(b1);
         p2.add(b2);
          p2.add(l6);         
         add(p2,BorderLayout.SOUTH);

          b1.addActionListener(this);
          b2.addActionListener(this);
     }

     public void actionPerformed(ActionEvent e)
     {
         String s,s1,s2,s3;
    s=e.getActionCommand();

    if(s.equals("Issue"))
    {
        t1.setText("");
        t2.setText("");
        t3.setText(""); 
        t4.setText("");
        t5.setText("");
    }

      if(s.equals("Clear"))
    {

           s2=t4.getText();
           s3=t5.getText();
           try{
           sd=sdf.parse(s2);
           sd1=sdf.parse(s3);

         long day1=(sd1.getTime() - sd.getTime());
        long day=day1/(24*60*60*1000);                


      if(day <= 0)
      {
          l6.setText("no fine");
      }
      else
      {
          l6.setText("fine of Rs" +day);
      }
           } //try close
           catch (ParseException se)
           {
               System.out.println("Exception :"+se);
           }
           } //else close
    }
     }


This is my new code i have just added the card layout tats all sir **taywin**
4
Contributors
29
Replies
3 Days
Discussion Span
7 Months Ago
Last Updated
30
Views
Question
Answered
anandschiru
Light Poster
38 posts since Oct 2012
Reputation Points: -1
Solved Threads: 0
Skill Endorsements: 0

Update lines 30~34 (remove the part final JTextField) because they are local & you added these local variables instead of the class variables.

 // should look similar to below
 t1 = new JTextField(10);
 t2 = new JTextField(10);
 t3 = new JTextField(10);
 t4 = new JTextField(10);
 t5 = new JTextField(10);

Lines 36~41, remove the leading part JLabel from each line.

// should look similar to below
l6 = new JLabel("fine");
l1 = new JLabel("Book Name");
l2 = new JLabel("Author Namae");
l3 = new JLabel("Issue Date");
l4 = new JLabel("Last Date");
l5 = new JLabel("Submitted Date");

Lines 43-44, remove the leading part JButton from each line.

// should look similar to below
b1=new JButton("Issue");
b2=new JButton("Clear");

Line 48, remove the leading part JPanel from the line (same with lines 59 and 71)...

// should look similar to below
p=new JPanel();

It is obvious that you have no idea of variable scope. I blame your instructor that he/she does not teach students about variable scope before letting them code!

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

sir the issue button is working but clear button is not sirtaywin

anandschiru
Light Poster
38 posts since Oct 2012
Reputation Points: -1
Solved Threads: 0
Skill Endorsements: 0

Please explain what "not working" whenever you ask a question. Often times, I won't know what the meaning of "not working" and it is difficult to assume.

Now, my assumption from your "not working" is from many causes.

1) Line 28, the pattern string of a SimpleDateFormat does NOT have Y in it. Also, the D will give you the day of the year instead of the day of month. The pattern string you are looking for should be "dd/MM/yyyy".
2) Your catch() definition should be more explicit that just Exception: *. Also, the +se will produce garbage instead of any information. What you should output is to your *l6 variable saying that the input format is invalid. Don't forget to have another catch to cover all other exceptions.

//i.e.
catch (ParseException se) { l6.setText("Invalid input date format"); }
catch (Exception e) {
  l6.setText("Unknown exception");
  e.printStackTrace();
}

3) You have not handled any input if the fields are empty? If not, the program will always throw the ParseException.

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

Sir according to my parogram when i press the issue button it should take the input from last date and submitted date and then it should find the differnce between them but in my case its not happening,so what may be the reason for this.

anandschiru
Light Poster
38 posts since Oct 2012
Reputation Points: -1
Solved Threads: 0
Skill Endorsements: 0

Please look at my previous post and adjust your code accordingly. Your SimpleDateFormat should throw an exception while running because it doesn't recognize the format "DD/MM/YYYY" because the "Y" does not exist in its string pattern list.

Also, I'm guessing that your t5 field is the last date and the t4 field is the submitted date? You should and must name your variables to a meaningful name, so that it can describe itself. Putting only one letter and a number to make it unique does NOT help others especially yourself when you do debugging.

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

even after changing all the things u have told its not working anyways thanx a lot sir

anandschiru
Light Poster
38 posts since Oct 2012
Reputation Points: -1
Solved Threads: 0
Skill Endorsements: 0

Is there any otherway i can convert a date into a string and find the differece between the two dates, the problem is while parsing sir now i came to know because whn i press the clear button its going to catch block and dispalying invalid input. Taywin

anandschiru
Light Poster
38 posts since Oct 2012
Reputation Points: -1
Solved Threads: 0
Skill Endorsements: 0

The parsing will throw exception if the values in t4 and t5 do not follow the format. For example, you want 14 November, 2011 to be the value of t4 and 1 December, 2011 to be in t5, you must enter 14112011 in t4 field and 01122011 in t5. If you do not follow the format, you will get either a wrong result or the invalid input.

Below is an example of parsing input string using your string pattern. Look closely for the output.

import java.util.*;
import java.text.*;
class TestSimpleDatePattern {
  public static void main(String[] args) {
    try {
      SimpleDateFormat spd = new SimpleDateFormat("ddMMyyyy");
      Date d = spd.parse("18022011");  // correct format
      System.out.println(DateFormat.getDateTimeInstance().format(d));
      Date d1 = spd.parse("8022011");  // incomplete --> incorrect parsing
      System.out.println(DateFormat.getDateTimeInstance().format(d1));
      Date d2 = spd.parse("822011");   // incomplete --> incorrect parsing
      System.out.println(DateFormat.getDateTimeInstance().format(d2));
      Date d3 = spd.parse("Nov 21, 2012");  // Parsing Error!
      System.out.println(DateFormat.getDateTimeInstance().format(d3));
    }
    catch (ParseException se) {
      System.out.println("Parse Error!");
      se.printStackTrace();  // this will display the string which causes error
    }
    catch (Exception e) {
      System.out.println("Unknown Error!");
      e.printStackTrace();
    }
  }
}  // end class

/*
The output:
>java TestSimpleDatePattern
Feb 18, 2011 12:00:00 AM
Dec 19, 0012 12:00:00 AM
Oct 21, 0012 12:00:00 AM
Parse Error!
java.text.ParseException: Unparseable date: "Nov 21, 2012"
    at java.text.DateFormat.parse(DateFormat.java:354)
    at TestSimpleDatePattern.main(TestSimpleDatePattern.java:13)
*/
Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

actually i am following the same format, even though i am getting the invalid input sir. if you don mind can you see my parsing is correct or not.

anandschiru
Light Poster
38 posts since Oct 2012
Reputation Points: -1
Solved Threads: 0
Skill Endorsements: 0

How to get a date from jtextfield ? anybody please help

anandschiru
Light Poster
38 posts since Oct 2012
Reputation Points: -1
Solved Threads: 0
Skill Endorsements: 0

.getText() and parse the result to a Date()

stultuske
Industrious Poster
4,379 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

In your original posted code, between line 100 and 101, insert this line of code to see whether the string from getText() from your text field object is correct.

System.out.println("T4: "+s2+" | T5: "+s3);

After you run and tried it, look in the command window (the black background) to see what it said. If you are using an IDE, the message should appear in the debugging area (usually at the bottom?).

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

i have tried tat also sir its giving the date whc has been enterd in textfield, the problem is while pasing sir

anandschiru
Light Poster
38 posts since Oct 2012
Reputation Points: -1
Solved Threads: 0
Skill Endorsements: 0

That's OK. Taywin's code is to check that you are getting the String from the text field correctly. Now we know that's OK we can move on. Taywin has also posted a lot of code that shows how you should approach the parsing. What part of that is causing you a problem? If your code isn't parsing as you expect then you will need to post the code so we can see what's wrong with it.

JamesCherrill
... trying to help
Moderator
8,521 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30
import javax.swing.* ;
 import java.awt.event.*;
 import java.awt.*;
 import java.io.*;
 import java.util.Calendar;
 import java.util.*;
 import java.text.*;
 import java.util.Date;


  /*
  <applet code="gui.class" width=250 height=100>
  </applet>
  */


 public class gui1  extends  JApplet  implements  ActionListener
 {
     JTextField t1,t2,t3,t4,t5;
     JLabel l1,l2,l3,l4,l5,l6;
     JButton b1,b2;
     Calendar c1,c2,c3;
     JPanel p,p1,p2;

     SimpleDateFormat sdf;
     public void init()
     {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

           t1 = new JTextField(10);
           t2 = new JTextField(10);
            t3 = new JTextField(10);
           t4 = new JTextField(10);
         t5 = new JTextField(10);                 

         l6 = new JLabel("");
        l1 = new JLabel("Book Name");
         l2 = new JLabel("Author Namae");
          l3 = new JLabel("Issue Date");
          l4 = new JLabel("Last Date");
          l5 = new JLabel("Submitted Date");

          b1=new JButton("Issue");
           b2=new JButton("Clear");

          setLayout(new BorderLayout());

          p=new JPanel();
           p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));  
           p.add(l1);

           p.add(l2);

           p.add(l3);


           p.add(l4);
           p.add(l5);
            p1=new JPanel();
             p1.setLayout(new BoxLayout(p1,BoxLayout.Y_AXIS));
            p1.add(t1);
           p1.add(t3);
            p1.add(t2);
           p1.add(t4);

           p1.add(t5);

       add(p,BorderLayout.WEST);
       add(p1,BorderLayout.EAST);

         p2=new JPanel();
         p2.add(b1);
         p2.add(b2);
          p2.add(l6);         
         add(p2,BorderLayout.SOUTH);

          b1.addActionListener(this);
          b2.addActionListener(this);
     }

     public void actionPerformed(ActionEvent es)
     {
         String s,s1,s2,s3;
    s=es.getActionCommand();

    if(s.equals("Issue"))
    {
        t1.setText("");
        t2.setText("");
        t3.setText(""); 
        t4.setText("");
        t5.setText("");
    }

      if(s.equals("Clear"))
        {


               s2=t4.getText();
               s3=t5.getText();
               try{


            Date    sd= (Date) sdf.parse(s2);    **this part oly sir **
            Date    sd1= (Date) sdf.parse(s3);    **this part oly sir** 
             long day1=(sd1.getTime() - sd.getTime());
            long day=day1/(24*60*60*1000);                


          if(day <= 0)
          {
              l6.setText("no fine");
          }
          else
          {
              l6.setText("fine of Rs" +day);
          }
           }  // try close
              catch(Exception e)
              {
            l6.setText("Error : Invalid Date "); 

              }      
               } //else close
        }
         }
anandschiru
Light Poster
38 posts since Oct 2012
Reputation Points: -1
Solved Threads: 0
Skill Endorsements: 0

The pasing whc i hav done is correct or wrong

anandschiru
Light Poster
38 posts since Oct 2012
Reputation Points: -1
Solved Threads: 0
Skill Endorsements: 0

That parsing looks right, depending on how sdf has been defined. Can you post the definition of sdf, and an example of the text that's not parsing properly?

JamesCherrill
... trying to help
Moderator
8,521 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30
sdf means simpledateformat whc helps in formating and parsing the date in whc way we want it.

example is lastdate: 10/10/2012
            submitted date: 20/10/2012

            output: invalid date format
anandschiru
Light Poster
38 posts since Oct 2012
Reputation Points: -1
Solved Threads: 0
Skill Endorsements: 0

Yes, I know what a simple date format is. The question is what format are you using? What is the string in quote marks inside the parenthesis after the text SimpleDateFormat in your code?

JamesCherrill
... trying to help
Moderator
8,521 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1392 seconds using 2.8MB