|
-
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 );
}
}
);
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
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
|
Bookmarks