We were asked to create an applet in which it will get the current date, then the user will input a number (n), when the user presses 'ok', the date+n after will display. Here is my code so far

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Calendar;
import java.util.Date.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class fcast extends Applet implements ActionListener
{ 
  
  int num;
  Date p;

  Label ldate = new Label("Today is: ");
  Label lfcast = new Label("No. of days before forecast: ");
  Label lfcdate = new Label("Forecast Date: ");

  TextField tdate = new TextField(" ",20);
  TextField tfcast = new TextField(" ",20);
  TextField tfcdate = new TextField(" ",20);

  Button bk = new Button("OK");
  Button bcr = new Button("CLEAR");

  Calendar d=Calendar.getInstance();
  SimpleDateFormat date = new SimpleDateFormat("MM dd yyyy");
  Date d8 = new Date();
  DateFormat df =  DateFormat.getDateInstance();

  
  
  public void init()
  {
   add(ldate); add(tdate);
   add(lfcast); add(tfcast);
   add(lfcdate); add(tfcdate);
   add(bk); add(bcr);
   bk.addActionListener(this);
   bcr.addActionListener(this);
   tdate.setText(date.format(d8));
  
   
  }

  public void actionPerformed(ActionEvent f)
  {
    
    if(f.getSource()==bk)
    {  
       {
	num=Integer.parseInt(tfcast.getText());
	d.add(Calendar.DATE, num);
	Date p=d.getTime();
	tfcdate.setText(String.valueOf(p));
       }
 	
    }
    if(f.getSource()==bcr)
    {  
       tdate.setEnabled(false);
       tfcdate.setText(" ");
       tfcast.setText(" ");
       tfcast.requestFocus();
    }
  }
}

It doesn't output, what's wrong with my code? Thanks.

I believe your main problem is that you are trying to parse a String without numbers.

When I compiled your code I got a number format exception. Here is what I did to eliminate the problem (to an extent) --

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Calendar;
import java.util.Date.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class fcast extends Applet implements ActionListener
{

  int num;
  Date p;

  Label ldate = new Label("Today is: "); // a new label ldate "Today is"
  Label lfcast = new Label("No. of days before forecast: "); // lfcast a new label "No. of days before forecast"
  Label lfcdate = new Label("Forecast Date: "); // lfcdate a new label "Forecast Date"

  TextField tdate = new TextField("",20); // a new text field tdate, length 20 (with initial value " " ?)
  TextField tfcast = new TextField("",20); // a new text field tfcast, length 20 (with initial value " " ?)
  TextField tfcdate = new TextField("",20); // a new text field tfcdate, length 20 (with initial value " " ?)

  Button bk = new Button("OK"); // a new button bk "OK"
  Button bcr = new Button("CLEAR"); // a new button bcr "CLEAR"

  Calendar d=Calendar.getInstance(); // gets a handle to the current Calendar?
  SimpleDateFormat date = new SimpleDateFormat("MM dd yyyy"); // creates a new SimpleDateFormat
  Date d8 = new Date(); // creating a new Date
  DateFormat df =  DateFormat.getDateInstance(); // static call to (default?) DateFormat



  public void init() // Applet-cycle 'constructor' - first method called in Applet creation
  {
   add(ldate); // adding Label
   add(tdate); // adding TextField
   add(lfcast); // adding  Label
   add(tfcast); // adding TextField
   add(lfcdate); // adding Label
   add(tfcdate); // adding TextField
   add(bk); // adding Button
   add(bcr); // adding Button
   bk.addActionListener(this); // setting the action listener to this class
   bcr.addActionListener(this); // setting the action listener to this class
   tdate.setText(date.format(d8)); // setting the text of TextField tdate to the SimpleDateFormat that takes a Date
  }

  public void actionPerformed(ActionEvent f)
  {

    if(f.getSource()==bk)
    {
       {
		   System.out.println("OK button clicked");
	num=Integer.parseInt(  (tfcast.getText().equalsIgnoreCase("")) ?  "0": tfcast.getText());
	d.add(Calendar.DATE, num);
	Date p=d.getTime();
	tfcdate.setText(String.valueOf(p));
       }

    }
    if(f.getSource()==bcr)
    {
		System.out.println("CLEAR button clicked");
       tdate.setEnabled(false);
       tfcdate.setText("");
       tfcast.setText("");
       tfcast.requestFocus();
    }
  }
}

--Now if I were better with Regex I would make some kind of expression that would detect a space character of any length within a given String. If it exists, replace the value to be parsed with a "0", otherwise take the risk of parsing the value.

Either that or make the Regex more efficient by parsing anything that isn't solely numbers as a "0", otherwise parse the value.

Obviously the method posted by me isn't the most efficient, but it does make a reasonable workaround for your problem.

Yehey! Thanks for your help!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.