aspose file tools*
The moose likes Beginning Java and the fly likes beerExpert.java uses unchecked or unsafe operations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "beerExpert.java uses unchecked or unsafe operations" Watch "beerExpert.java uses unchecked or unsafe operations" New topic
Author

beerExpert.java uses unchecked or unsafe operations

Jerry Bustamente
Ranch Hand

Joined: May 24, 2004
Posts: 90
Greetings to the Group,

I am on page 82 of Head First and am compiling BeerExpert.

C:\MyProjects\beerV1\src\com\example\model\javac BeerExpert.java

Note: BeerExpert.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

After recompiling with javac -Xlint parameter I get:

warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.List
brands.add("Jack Amber");

I get the same error for Red Moose, Jail Pale Ale and Gout Stout.

A BeerSelect.class file is still created but then when I go to page 84 and modify BeerSelect.java and compile it I get:

package com.example.model does not exist.

The source for BeerSelect.java is:

package com.example.web;

// version 2 of the servlet page 84

import com.example.model.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.util.*;

public class BeerSelect extends HttpServlet {

public void doPost (HttpServletRequest request,
HttpServletResponse response)

throws IOException, ServletException {

response.setContentType ("text/html");

PrintWriter out = response.getWriter();

out.println("Beer Selection Advice<br>");

String c = request.getParameter("color");


BeerExpert be = new BeerExpert();

List result = be.getBrands(c);

Iterator it = result.iterator();

while (it.hasNext()) {

out.print("<br>try: " + it.next());
}
}}


The source for BeerExpert.java is:

package com.example.model;

import java.util.*;

public class BeerExpert {

public List getBrands(String color) {

List brands = new ArrayList();

if (color.equals("amber")) {

brands.add("Jack Amber");
brands.add("Red Moose");
}

else {

brands.add("Jail Pale Ale");
brands.add("Gout Stout");
}

return (brands);

}
}

Before I modified it as on page 84 I successfully compiled BeerSelect.java
I was able to display the Beer Selection Page then after clicking on a color and Submit Query I received a page that read Beer Selection Advice and nothing else.

I deleted both the BeerSelect and Beer Expert class files and started over recompiling them when I received the errors above.

Since successfully display the messages on the pages in the previous paragraph I have not changed any environment variables.

This is on Tomcat 5.5.4 JDK 1.5.0

Any help greatly appreciated.

Thanks,

Jerry Bustamente
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Simply put. Java 1.5 has Generics. The message you get is just warning, it still compiles.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Generics is about type safety for your Collection, and some more. But with Collections you basically tell it what type of objects you can put into the Collection. When you have code that does not say what type you can put in the Collection, it will allow you to do this, which is the 1.4 and before way, but it will give you a warning. The reason why it won't cause an error is to allow for backwards compatibility.

Mark
Jerry Bustamente
Ranch Hand

Joined: May 24, 2004
Posts: 90
Thanks alot Mark!

Jerry Bustamente
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Moving this to Java In General (beginner) forum.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: beerExpert.java uses unchecked or unsafe operations