Solved

Replacing Deprecated Methods

Posted on 2001-06-27
18
484 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:rac13
  • 12
  • 5
18 Comments
 
LVL 1

Expert Comment

by:kuro
ID: 6233740
> 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.
 
0
 

Author Comment

by:rac13
ID: 6233754
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.
0
 
LVL 5

Expert Comment

by:Calron
ID: 6233891
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).
0
 

Author Comment

by:rac13
ID: 6233972
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.
0
 
LVL 5

Expert Comment

by:Calron
ID: 6234018
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.                      
0
 

Author Comment

by:rac13
ID: 6234039
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
0
 
LVL 5

Expert Comment

by:Calron
ID: 6234061
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();


         
         
         
         
0
 

Author Comment

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

I cannot open up the file and read.
0
 
LVL 5

Expert Comment

by:Calron
ID: 6234076
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.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:rac13
ID: 6234090
0
 

Author Comment

by:rac13
ID: 6234094
0
 

Author Comment

by:rac13
ID: 6234096
0
 

Author Comment

by:rac13
ID: 6234099
0
 

Author Comment

by:rac13
ID: 6234104
There is an error:

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

I'll sent in later
0
 

Author Comment

by:rac13
ID: 6234117
There is an error:

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

I'll sent in later

0
 
LVL 5

Accepted Solution

by:
Calron earned 20 total points
ID: 6234139
You can also send it by email: lauchenauer@abacus.ch
0
 

Author Comment

by:rac13
ID: 6234203
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;
    }
}
0
 

Author Comment

by:rac13
ID: 6234206
Thanks alot for your effort and time. :)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now