dateConversionProblem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: dateConversionProblem

  1. #1
    newToJava Guest

    dateConversionProblem


    Problem:
    I need to read in any date( format 00/00/00 ) from a JTextField and and
    tokenizer 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 token into a int.

    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

    New to newsgroups, too? Posting your question in several places at the same
    time is called "cross-posting" and is considered bad etiquette.

    "newToJava" <mprog@mediaone.net> wrote in message
    news:3ac2b4ab$1@news.devx.com...
    >
    > Problem:
    > I need to read in any date( format 00/00/00 ) from a JTextField and and
    > tokenizer 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 token into a int.
    >
    > 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 = "file://";
    > 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" );
    >
    > file://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 );
    > }
    > }
    > );
    > }
    > }
    >
    >
    >
    >




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