dateConversionProblem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: dateConversionProblem

  1. #1
    newToJava Guest

    dateConversionProblem


    Problem:
    I need to read in any date( format 00/00/00 ) from a JTextField and
    tokenize it into the day year month to be converted into ints to use in the
    java date methods. I seperated my tokens fine and it works. I then created
    a getDayToken(String) method to convert the tokens into
    ints( month, day, year).

    public int getDayToken( String gdt ){

    int day;
    StringTokenizer dayToken =
    new StringTokenizer( gdt);
    try {
    while( dayToken.hasMoreTokens() );
    {
    dayToken.nextToken();
    dayToken.nextToken();
    }
    }
    catch( NoSuchElementException e )
    {
    System.out.println( e );
    }
    day = Integer.parseInt( dayToken );
    return( day );
    }

    After the method is called the jdk1.3 compiler complains that a java.lang.string
    can't be applied to a javax.swing.JTextField. are they
    not both considered strings. I also can't convert a java.util StringTokenizer
    object into a string using Integer.parseInt()compile says can't resolve symbol
    Integer.parseInt() If there is a conversion
    COULD SOME ONE STEAR ME IN THE RIGHT DIRECTION PLEASE!!!!!!.

    The full code to test my method is below:

    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;

    public class TokenTest extends JFrame{

    private JLabel prompt;
    private JTextField input;
    private JTextArea output;
    public String stringTokenMonth, stringTokenDay, stringTokenYear;


    public TokenTest()
    {
    super( "testing the Tokeniser" );

    Container c = getContentPane();
    c.setLayout( new FlowLayout() );

    prompt = new JLabel(
    "Enter a date and press enter: " );
    c.add(prompt);

    input = new JTextField( 20 );
    input.addActionListener(
    new ActionListener() {
    public void actionPerformed( ActionEvent ae )
    {
    String delim = new String();

    delim = "//";
    String stringToTokenize = ae.getActionCommand();
    StringTokenizer tokens =
    new StringTokenizer( stringToTokenize, delim );

    output.setText( "Number of Elements: " +
    tokens.countTokens() +
    "\nThe tokens are: \n" );

    while( tokens.hasMoreTokens() ){
    stringTokenMonth = tokens.nextToken();
    stringTokenDay = tokens.nextToken();
    stringTokenYear = tokens.nextToken();
    }

    output.append( stringTokenMonth + "\n" );
    output.append( stringTokenDay + "\n" );
    output.append( stringTokenYear + "\n" );

    //Test method call
    output.append("method getDayToken( String )\n" + getDayToken( input
    ) );
    }
    }
    );

    c.add( input );

    output = new JTextArea( 10, 20 );
    output.setEditable( false );
    c.add( new JScrollPane( output ) );

    setSize(275, 400 );
    show();
    }

    public int getDayToken( String gdt ){

    int day;



    StringTokenizer dayToken =
    new StringTokenizer( gdt);

    try {
    while( dayToken.hasMoreTokens() );
    {
    dayToken.nextToken();
    dayToken.nextToken();
    }
    }

    catch( NoSuchElementException e )
    {
    System.out.println( e );
    }


    day = Integer.parseInt( dayToken );
    return( day );
    }




    public static void main( String args[] )

    {
    TokenTest app = new TokenTest();

    app.addWindowListener(
    new WindowAdapter() {
    public void windowClosing( WindowEvent e )
    {
    System.exit( 0 );
    }
    }
    );
    }
    }


  2. #2
    Ako Guest

    Re: dateConversionProblem


    I've just had a quick look at your code, there are lots of logical errors
    but I have to its a good attempt for some one new to java.
    here are some points for you to note and if I have time I will look at it
    properly and get it to work.
    1- no JTextField is not a String, it is a different class. but if you
    want to get the string that is been typed into your JTextField you can
    invoke the method getText() on your JTextField object
    eg in your case input.getText();
    2-the other error in your code is you cant treat the dayToken which is of
    type StringTokenizer, you should use it like this
    dayToken.nextToken();
    3- changing those things still wont mean that your program will work
    you have added the action listener dirctly to the JTextField, this
    means that your actionPerformed gets called every time the user clicks
    or types inside your JTextField.
    It would be better if you add a Button and register that with the
    actionListener so when the user finishes typing he can click the button
    then inside your acrionPerformed method get the text of the JTextField.
    this way you will have the whole string and you can do what ever you
    want with it.


    Good Luck

    Ako

    "newToJava" <mprog@mediaonae.net> wrote:
    >
    >Problem:
    >I need to read in any date( format 00/00/00 ) from a JTextField and
    >tokenize it into the day year month to be converted into ints to use in

    the
    >java date methods. I seperated my tokens fine and it works. I then created
    >a getDayToken(String) method to convert the tokens into
    >ints( month, day, year).
    >
    >public int getDayToken( String gdt ){
    >
    > int day;
    > StringTokenizer dayToken =
    > new StringTokenizer( gdt);
    > try {
    > while( dayToken.hasMoreTokens() );
    > {
    > dayToken.nextToken();
    > dayToken.nextToken();
    > }
    > }
    > catch( NoSuchElementException e )
    > {
    > System.out.println( e );
    > }
    > day = Integer.parseInt( dayToken );
    > return( day );
    > }
    >
    >After the method is called the jdk1.3 compiler complains that a java.lang.string
    >can't be applied to a javax.swing.JTextField. are they
    >not both considered strings. I also can't convert a java.util StringTokenizer
    >object into a string using Integer.parseInt()compile says can't resolve

    symbol
    >Integer.parseInt() If there is a conversion
    >COULD SOME ONE STEAR ME IN THE RIGHT DIRECTION PLEASE!!!!!!.
    >
    >The full code to test my method is below:
    >
    >import javax.swing.*;
    >import java.util.*;
    >import java.awt.*;
    >import java.awt.event.*;
    >
    >public class TokenTest extends JFrame{
    >
    > private JLabel prompt;
    > private JTextField input;
    > private JTextArea output;
    > public String stringTokenMonth, stringTokenDay, stringTokenYear;
    >
    >
    > public TokenTest()
    > {
    > super( "testing the Tokeniser" );
    >
    > Container c = getContentPane();
    > c.setLayout( new FlowLayout() );
    >
    > prompt = new JLabel(
    > "Enter a date and press enter: " );
    > c.add(prompt);
    >
    > input = new JTextField( 20 );
    > input.addActionListener(
    > new ActionListener() {
    > public void actionPerformed( ActionEvent ae )
    > {
    > String delim = new String();
    >
    > delim = "//";
    > String stringToTokenize = ae.getActionCommand();
    > StringTokenizer tokens =
    > new StringTokenizer( stringToTokenize, delim );
    >
    > output.setText( "Number of Elements: " +
    > tokens.countTokens() +
    > "\nThe tokens are: \n" );
    >
    > while( tokens.hasMoreTokens() ){
    > stringTokenMonth = tokens.nextToken();
    > stringTokenDay = tokens.nextToken();
    > stringTokenYear = tokens.nextToken();
    > }
    >
    > output.append( stringTokenMonth + "\n" );
    > output.append( stringTokenDay + "\n" );
    > output.append( stringTokenYear + "\n" );
    >
    > //Test method call
    > output.append("method getDayToken( String )\n" + getDayToken( input
    >) );
    > }
    > }
    > );
    >
    > c.add( input );
    >
    > output = new JTextArea( 10, 20 );
    > output.setEditable( false );
    > c.add( new JScrollPane( output ) );
    >
    > setSize(275, 400 );
    > show();
    > }
    >
    > public int getDayToken( String gdt ){
    >
    > int day;
    >
    >
    >
    > StringTokenizer dayToken =
    > new StringTokenizer( gdt);
    >
    > try {
    > while( dayToken.hasMoreTokens() );
    > {
    > dayToken.nextToken();
    > dayToken.nextToken();
    > }
    > }
    >
    > catch( NoSuchElementException e )
    > {
    > System.out.println( e );
    > }
    >
    >
    > day = Integer.parseInt( dayToken );
    > return( day );
    > }
    >
    >
    >
    >
    > public static void main( String args[] )
    >
    > {
    > TokenTest app = new TokenTest();
    >
    > app.addWindowListener(
    > new WindowAdapter() {
    > public void windowClosing( WindowEvent e )
    > {
    > System.exit( 0 );
    > }
    > }
    > );
    > }
    >}
    >



  3. #3
    Ako Guest

    Re: dateConversionProblem


    here is your coding doing what you want it to do, I have only made few changes,
    all the changes are commented.


    good Luck

    Ako


    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;

    public class TokenTest extends JFrame{

    private JLabel prompt;
    private JTextField input;
    private JTextArea output;
    public String stringTokenMonth, stringTokenDay, stringTokenYear;
    private JButton ok ; //add this button

    public TokenTest()
    {
    super( "testing the Tokeniser" );

    Container c = getContentPane();
    c.setLayout( new FlowLayout() );

    prompt = new JLabel(
    "Enter a date and press enter: " );
    c.add(prompt);

    input = new JTextField( 20 );

    ok = new JButton("OK"); //instanciate the button and make it your
    c.add(ok); // your listener
    ok.addActionListener(
    new ActionListener() {
    public void actionPerformed( ActionEvent ae )
    {
    String delim = new String();

    delim = "//";
    String stringToTokenize = input.getText();
    StringTokenizer tokens =
    new StringTokenizer( stringToTokenize, delim );

    output.setText( "Number of Elements: " +
    tokens.countTokens() +
    "\nThe tokens are: \n" );

    while( tokens.hasMoreTokens() ){
    stringTokenMonth = tokens.nextToken();
    stringTokenDay = tokens.nextToken();
    stringTokenYear = tokens.nextToken();
    }

    output.append( stringTokenMonth + "\n" );
    output.append( stringTokenDay + "\n" );
    output.append( stringTokenYear + "\n" );
    //you dont need the following line line
    //Test method call
    //output.append("method getDayToken( String )\n" + getDayToken( input.getText())
    );
    }
    }
    );

    c.add( input );

    output = new JTextArea( 10, 20 );
    output.setEditable( false );
    c.add( new JScrollPane( output ) );

    setSize(275, 400 );
    show();
    }

    public int getDayToken( String gdt ){

    int day;



    StringTokenizer dayToken =
    new StringTokenizer( gdt);

    try {
    while( dayToken.hasMoreTokens() );
    {
    dayToken.nextToken();
    dayToken.nextToken();
    }
    }

    catch( NoSuchElementException e )
    {
    System.out.println( e );
    }


    day = Integer.parseInt( dayToken.nextToken() );
    return( day );
    }




    public static void main( String args[] )

    {
    TokenTest app = new TokenTest();

    app.addWindowListener(
    new WindowAdapter() {
    public void windowClosing( WindowEvent e )
    {
    System.exit( 0 );
    }
    }
    );
    }
    }



  4. #4
    Mike Hewitt Guest

    Re: dateConversionProblem


    newToJava,
    There a couple things to do.

    First off, you must call getText() on the JTextField object to get the value
    in the field ( a String).

    Secondly, check you logic for parsing the different parts of your date format.

    There is an easier solution to your problem.
    Check out the javadoc on the SimpleDateFormat class. You can easily parse
    many forms of date strings using it and get back a populated Date object.

    Hope this helps.
    - Mike

    Mike Hewitt
    Senior Consultant
    Integrated Enterprise Solutions, Inc.
    mikehewitt@ies-fl.com

    "newToJava" <mprog@mediaonae.net> wrote:
    >
    >Problem:
    >I need to read in any date( format 00/00/00 ) from a JTextField and
    >tokenize it into the day year month to be converted into ints to use in

    the
    >java date methods. I seperated my tokens fine and it works. I then created
    >a getDayToken(String) method to convert the tokens into
    >ints( month, day, year).
    >
    >public int getDayToken( String gdt ){
    >
    > int day;
    > StringTokenizer dayToken =
    > new StringTokenizer( gdt);
    > try {
    > while( dayToken.hasMoreTokens() );
    > {
    > dayToken.nextToken();
    > dayToken.nextToken();
    > }
    > }
    > catch( NoSuchElementException e )
    > {
    > System.out.println( e );
    > }
    > day = Integer.parseInt( dayToken );
    > return( day );
    > }
    >
    >After the method is called the jdk1.3 compiler complains that a java.lang.string
    >can't be applied to a javax.swing.JTextField. are they
    >not both considered strings. I also can't convert a java.util StringTokenizer
    >object into a string using Integer.parseInt()compile says can't resolve

    symbol
    >Integer.parseInt() If there is a conversion
    >COULD SOME ONE STEAR ME IN THE RIGHT DIRECTION PLEASE!!!!!!.
    >
    >The full code to test my method is below:
    >
    >import javax.swing.*;
    >import java.util.*;
    >import java.awt.*;
    >import java.awt.event.*;
    >
    >public class TokenTest extends JFrame{
    >
    > private JLabel prompt;
    > private JTextField input;
    > private JTextArea output;
    > public String stringTokenMonth, stringTokenDay, stringTokenYear;
    >
    >
    > public TokenTest()
    > {
    > super( "testing the Tokeniser" );
    >
    > Container c = getContentPane();
    > c.setLayout( new FlowLayout() );
    >
    > prompt = new JLabel(
    > "Enter a date and press enter: " );
    > c.add(prompt);
    >
    >

    // changed here
    input = new JTextField( 20 );
    JButton buttonTest = new JButton("Test");
    c.add(buttonTest);
    > buttonTest.addActionListener(
    > new ActionListener() {
    > public void actionPerformed( ActionEvent ae )
    > {
    > String delim = new String();
    >
    > delim = "//";

    // changed here
    > //String stringToTokenize = ae.getActionCommand();

    String stringToTokenize = input.getText();
    > StringTokenizer tokens =
    > new StringTokenizer( stringToTokenize, delim );
    >
    > output.setText( "Number of Elements: " +
    > tokens.countTokens() +
    > "\nThe tokens are: \n" );
    >
    > while( tokens.hasMoreTokens() ){
    > stringTokenMonth = tokens.nextToken();
    > stringTokenDay = tokens.nextToken();
    > stringTokenYear = tokens.nextToken();
    > }
    >
    > output.append( stringTokenMonth + "\n" );
    > output.append( stringTokenDay + "\n" );
    > output.append( stringTokenYear + "\n" );
    >
    > //Test method call

    // changed here
    > // output.append("method getDayToken( String )\n" + getDayToken( input)

    );
    output.append("method getDayToken( String )\n" + getDayToken( input.getText())
    );
    > }
    > }
    > );
    >
    > c.add( input );
    >
    > output = new JTextArea( 10, 20 );
    > output.setEditable( false );
    > c.add( new JScrollPane( output ) );
    >
    > setSize(275, 400 );
    > show();
    > }

    // I don't understand your logic here. If the day token is the first,
    // why are you extracting multiple tokens?

    > public int getDayToken( String gdt ){
    >
    > int day;

    // add a variable
    String dayString = null;
    >

    // don't forget the delimiters!

    > StringTokenizer dayToken =
    > new StringTokenizer( gdt, "/");
    > try {

    /* change here
    > while( dayToken.hasMoreTokens() );
    > {
    > dayToken.nextToken();
    > dayToken.nextToken();
    > }

    */
    dayString = dayToken.nextToken();
    > }
    >
    > catch( NoSuchElementException e )
    > {
    > System.out.println( e );
    > }
    >
    >

    // change here
    day = Integer.parseInt( dayString );
    > return( day );
    > }
    >
    >
    >
    >
    > public static void main( String args[] )
    >
    > {
    > TokenTest app = new TokenTest();
    >
    > app.addWindowListener(
    > new WindowAdapter() {
    > public void windowClosing( WindowEvent e )
    > {
    > System.exit( 0 );
    > }
    > }
    > );
    > }
    >}
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center