Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

beerExpert.java uses unchecked or unsafe operations

 
Jerry Bustamente
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simply put. Java 1.5 has Generics. The message you get is just warning, it still compiles.

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot Mark!

Jerry Bustamente
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to Java In General (beginner) forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic