Link to home
Start Free TrialLog in
Avatar of rac13
rac13

asked on

Replacing Deprecated Methods

I am trying out the examples in a book that is using JDK1.1, so of the methods are deprecated, so I replace it myself.
But it seems that somewhere is wrong and I'm not sure where. Please help. Thanks

import java.applet.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.applet.Applet;
import java.awt.event.*;
import java.net.URL;
import com.oreilly.servlet.HttpMessage;

public class DaytimeApplet extends Applet
       implements ActionListener{
      
      TextField httpText, httpObject, socketText, socketObject, RMIObject;
      Button refresh;
      
      public void init(){
            
            setLayout(new BorderLayout());
            
            Panel west = new Panel();
            west.setLayout(new GridLayout(5,1));
            west.add(new Label("HTTP text: ", Label.RIGHT));
            west.add(new Label("HTTP object: ", Label.RIGHT));
            west.add(new Label("Socket text: ", Label.RIGHT));
            west.add(new Label("Socket object: ", Label.RIGHT));
            west.add(new Label("RMI object: ", Label.RIGHT));
            add("West", west);
            
            Panel center = new Panel();
            center.setLayout(new GridLayout(5,1));
            
            httpText = new TextField();
            httpText.setEditable(false);
            center.add(httpText);
            
            httpObject = new TextField();
            httpObject.setEditable(false);
            center.add(httpObject);
            
            socketText = new TextField();
            socketText.setEditable(false);
            center.add(socketText);
            
            socketObject = new TextField();
            socketObject.setEditable(false);
            center.add(socketObject);
            
            RMIObject = new TextField();
            RMIObject.setEditable(false);
            center.add(RMIObject);
            
            add("Center", center);
            
            Panel south = new Panel();
            refresh = new Button("Refresh");
            refresh.addActionListener(this);
            south.add(refresh);
            add("South", south);
            
      }
      
      public void start(){
            refresh();
      }
      
      private void refresh(){
            
            httpText.setText(getDateUsingHttpText());
            httpObject.setText(getDateUsingHttpObject());
            socketText.setText(getDateUsingSocketText());
            socketObject.setText(getDateUsingSocketObject());
            RMIObject.setText(getDateUsingRMIObject());
      }
      
      private String getDateUsingHttpText(){
            
            try{
                  URL url = new URL(getCodeBase(), "/servlet/DaytimeServlet");
                  HttpMessage msg = new HttpMessage(url);
                  InputStream in = msg.sendGetMessage();
                  BufferedReader result = new BufferedReader(new?InputStreamReader(in));
                  String date = result.readLine();
                  
                  in.close();
                  
                  return date;
            }
            
            catch (Exception e){
                  e.printStackTrace();
                  return null;
            }      
            
            return "unavailable";
      }
      
      private String getDateUsingHttpObject(){
            
                  try{
                  URL url = new URL(getCodeBase(), "/servlet/DaytimeServlet");
                  HttpMessage msg = new HttpMessage(url);
                  
                  Properties props = new Properties();
                  props.put("format", "object");
                  
                  InputStream in = msg.sendGetMessage(props);
                  ObjectInputStream result = new ObjectInputStream(in);
                  Object obj = result.readObject();
                  Date date = Date(obj);
                  
                  return date.toString();
            }
            
            catch (Exception e){
                  e.printStackTrace();
                  return null;
            }      
            
            return "unavailable";
      }
      
      private String getDateUsingSocketText(){
            return "unavailable";
      }
      
      private String getDateUsingSocketObject(){
            return "unavailable";
      }
      
      private String getDateUsingRMIObject(){
            return "unavailable";
      }
      
      public boolean actionPerformed(ActionEvent event){
            
            if(event.getSource().equals(refresh))
            {
                  refresh();
                  return true;
            }
            return false;
      }
}

Error:

------------------- Compiler Output --------------

DaytimeApplet_own.java:81: illegal character: \160
                  BufferedReader result = new BufferedReader(new?InputStreamReader(in));
                                                                      ^
DaytimeApplet_own.java:81: '(' or '[' expected
                  BufferedReader result = new BufferedReader(new?InputStreamReader(in));
                                                                                             ^
DaytimeApplet_own.java:10: class DaytimeApplet is public, should be declared in a file named DaytimeApplet.java
public class DaytimeApplet extends Applet
       ^
DaytimeApplet_own.java:109: cannot resolve symbol
symbol  : method Date  (java.lang.Object)
location: class DaytimeApplet
                  Date date = Date(obj);
                                    ^
4 errors
Avatar of kuro
kuro
Flag of Japan image

> DaytimeApplet_own.java:81: illegal character: \160
>                BufferedReader result = new BufferedReader(new?InputStreamReader(in));
>                                                                      ^
> DaytimeApplet_own.java:81: '(' or '[' expected
>                BufferedReader result = new BufferedReader(new?InputStreamReader(in));

Those errors are caused by containing illegal charracter.
Check your source code carefully.

>                                                                                          ^
> DaytimeApplet_own.java:10: class DaytimeApplet is public, should be declared in a file named DaytimeApplet.java
> public class DaytimeApplet extends Applet
>       ^

This is caused by the file name 'DaytimeApplet_own.java'.
The public class DaytimeApplet must be defined in 'DaytimeApplet.java'.

> DaytimeApplet_own.java:109: cannot resolve symbol
> symbol  : method Date  (java.lang.Object)
> location: class DaytimeApplet
>                Date date = Date(obj);

This error is caused by the type of the argument 'obj' not by using deprecated method.
'obj' is declared as Object.
You can compil it if you cast it to String or long, of course,
it must be an instance of one of them.
 
Avatar of rac13
rac13

ASKER

I have looked very carefully, but still cannot find the illegal character. And after I had changed the name, iy become this error:

DaytimeApplet2.java:10: actionPerformed(java.awt.event.ActionEvent) in DaytimeApplet2 cannot implement actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener; attempting to use incompatible return type
found   : boolean
required: void
public class DaytimeApplet2 extends Applet


and for the last error, what do you mean by casting it to string? I did cast it after I assgin the date. Can't it be done this way?

I'm sorry, I very new to servlets.
DaytimeApplet_own.java:81: illegal character: \160
               BufferedReader result = new BufferedReader(new?InputStreamReader(in));
                                                           The Problem here is the question mark after new. It should go as follows:

BufferedReader result = new BufferedReader(new InputStreamReader(in));
               


DaytimeApplet_own.java:10: class DaytimeApplet is public, should be declared in a file named DaytimeApplet.java
public class DaytimeApplet extends Applet
     
Means that the whole file has to be saved under the name DaytimeApple.java


DaytimeApplet_own.java:109: cannot resolve symbol
symbol  : method Date  (java.lang.Object)
location: class DaytimeApplet
               Date date = Date(obj);
                                   
You have two problems here. Try the following:

Date date = new Date((String)obj);

First you have to create a new Date and assign it. Second the Date class does not have a constructor that takes just an object, but it either wants a long or a String, so cast the object to string. (That's what I changed).
Avatar of rac13

ASKER

When I change the date, this is the error:

DaytimeApplet2.java:109: warning: Date(java.lang.String) in java.util.Date has been deprecated
Date date = new Date((String)obj);
                  ^

DaytimeApplet_own.java:81: illegal character: \160
              BufferedReader result = new BufferedReader(new?InputStreamReader(in));
                                                          I don't have a question mark in it.
Sorry, my mistake, I didn't check for the constructor of the Date class with string. Use the following:

Date date = DateFormat.getDateInstance().parse((String)obj);

yout will have to import the following class for it to work:

import java.text.DateFormat;


I am not sure what's wrong with this:
DaytimeApplet_own.java:81: illegal character: \160
             BufferedReader result = new BufferedReader(new?InputStreamReader(in));
                                 
I see a question mark. You probably copied this code from somewhere. It is possible that there is a character there that doesn't show up in your editing environment that java doesn't like. I would try to delete this line and rewrite it, because there is really nothing wrong with it.                      
Avatar of rac13

ASKER

Thanks, I have got the date solved, but there is still 3 errors that still exist and I don't understand.

--------------- Compiler Output ------------
DaytimeApplet2.java:82: illegal character: \160
               BufferedReader result = new BufferedReader(new InputStreamReader(in));
 
DaytimeApplet2.java:82: '(' or '[' expected
               BufferedReader result = new BufferedReader(new InputStreamReader(in));
                                                           DaytimeApplet2.java:11: actionPerformed(java.awt.event.ActionEvent) in DaytimeApplet2 cannot implement actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener; attempting to use incompatible return type
found   : boolean
required: void
public class DaytimeApplet2 extends Applet
When you implement ActionListeners make sure you copy the required methods exaclty as required (look at the API documentation).

you wrote:

public boolean actionPerformed(ActionEvent event){

it should be:

public void actionPerformed(ActionEvent event){


I can't reproduce the first error that you get with the illegal character. I copied your code and I got it to compile fine. The problem could lie with the HttpMessage class, which I don't have. Check what that class returns in the method sendGetMessage();


         
         
         
         
Avatar of rac13

ASKER

What could be wrong? I download the class file from http://www.servlets.com/

I cannot open up the file and read.
No I checked out the class description from that page and that class should not be causing the problem.

Why don't you post your current source code , so I can copy it and check it on my compiler to see if the error occurs again.
Avatar of rac13

ASKER

Avatar of rac13

ASKER

Avatar of rac13

ASKER

Avatar of rac13

ASKER

Avatar of rac13

ASKER

There is an error:

Error (-2) - the comment your provided was not added due to a database problem.

I'll sent in later
Avatar of rac13

ASKER

There is an error:

Error (-2) - the comment your provided was not added due to a database problem.

I'll sent in later

ASKER CERTIFIED SOLUTION
Avatar of Calron
Calron

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rac13

ASKER

Answer provided by Calron:

import java.applet.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.applet.Applet;
import java.awt.event.*;
import java.net.URL;
import java.text.DateFormat;
import com.oreilly.servlet.HttpMessage;

public class DaytimeApplet2 extends Applet
    implements ActionListener{

    TextField httpText, httpObject, socketText, socketObject, RMIObject;
    Button refresh;

    public void init(){

        setLayout(new BorderLayout());

        Panel west = new Panel();
        west.setLayout(new GridLayout(5,1));
        west.add(new Label("HTTP text: ", Label.RIGHT));
        west.add(new Label("HTTP object: ", Label.RIGHT));
        west.add(new Label("Socket text: ", Label.RIGHT));
        west.add(new Label("Socket object: ", Label.RIGHT));
        west.add(new Label("RMI object: ", Label.RIGHT));
        add("West", west);

        Panel center = new Panel();
        center.setLayout(new GridLayout(5,1));

        httpText = new TextField();
        httpText.setEditable(false);
        center.add(httpText);

        httpObject = new TextField();
        httpObject.setEditable(false);
        center.add(httpObject);

        socketText = new TextField();
        socketText.setEditable(false);
        center.add(socketText);

        socketObject = new TextField();
        socketObject.setEditable(false);
        center.add(socketObject);

        RMIObject = new TextField();
        RMIObject.setEditable(false);
        center.add(RMIObject);

        add("Center", center);

        Panel south = new Panel();
        refresh = new Button("Refresh");
        refresh.addActionListener(this);
        south.add(refresh);
        add("South", south);

    }

    public void start(){
        refresh();
    }

    private void refresh(){

        httpText.setText(getDateUsingHttpText());
        httpObject.setText(getDateUsingHttpObject());
        socketText.setText(getDateUsingSocketText());
        socketObject.setText(getDateUsingSocketObject());
        RMIObject.setText(getDateUsingRMIObject());
    }

    private String getDateUsingHttpText(){

        try{
            URL url = new URL(getCodeBase(),
"/servlet/DaytimeServlet");
            HttpMessage msg = new HttpMessage(url);
            InputStream in = msg.sendGetMessage();
            BufferedReader result = new BufferedReader(new
InputStreamReader(in));
            String date = result.readLine();

            in.close();

            return date;
        }

        catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    private String getDateUsingHttpObject(){

            try{
            URL url = new URL(getCodeBase(),
"/servlet/DaytimeServlet");
            HttpMessage msg = new HttpMessage(url);

            Properties props = new Properties();
            props.put("format", "object");

            InputStream in = msg.sendGetMessage(props);
            ObjectInputStream result = new
ObjectInputStream(in);
            Object obj = result.readObject();
            Date date =
DateFormat.getDateInstance().parse((String)obj);

            return date.toString();
        }

        catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    private String getDateUsingSocketText(){
        return "unavailable";
    }

    private String getDateUsingSocketObject(){
        return "unavailable";
    }

    private String getDateUsingRMIObject(){
        return "unavailable";
    }

    public void actionPerformed(ActionEvent event){

        if(event.getSource().equals(refresh))
        {
            refresh();
            //return true;
        }
    //    return false;
    }
}
Avatar of rac13

ASKER

Thanks alot for your effort and time. :)