DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  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 connot aresolve
    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
    Paul Clapham Guest

    Re: dateConversionProblem

    Well first of all, you have done a lot of extra programming to convert a
    String to a Date. The most straightforward way to do that is to create a
    SimpleDateFormat object and use its parse() method to do this conversion:

    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
    Date result = sdf.parse(theString);

    And if you want to put a String into a JTextField, you use its setText()
    method.

    PC2

    "newToJava" <mprog@mediaone.net> wrote in message
    news:3ac30f09$1@news.devx.com...
    >
    > 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).
    >

    </snip>
    > 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 connot aresolve
    > symbol Integer.parseInt() If there is a conversion
    > COULD SOME ONE STEAR ME IN THE RIGHT DIRECTION PLEASE!!!!!!.

    </snip>



Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links