Errors in Map class

Hello Everyone.

Well I working on a program that uses Map class and I have some errors.

They are listed as.

on line 110

Type safety: The method put(Object, Object) belongs to the raw type HashMap. References to

generic type HashMap<K,V> should be parameterized

line 115

pe safety: The expression of type Collection needs unchecked conversion to conform to

Collection<ComputerInvent>

line 123

Type mismatch: cannot convert from Object to Computer_listing

Here is my code is far

import java.io.*;

import java.util.*;

class Computer_listing{

private String id;

private String name;

private Integer harddrive;

private Integer memory;

privatefloat speed;

public Computer_listing(String idx){

id = idx;

}// close the computer_listing loop

/**

* Constructors.

*/

public Computer_listing(String cd, String cn, Integer hd, Integer me,

float sp){

id = cd;

name = cn;

harddrive = hd;

memory = me;

speed = sp;

}// close public EmployeeClass

/*

* accessors make info available

*/

public String getId(){

return id;

}

public String getName(){

return name;

}

public Integer getHardrive(){

return harddrive;

}

public Integer getMemory(){

return memory;

}

publicfloat getSpeed(){

return speed;

}

/*

* Mutator Methods

*/

publicvoid setId(String cd){

id = cd;

}

publicvoid setName(String cn){

name = cn;

}

publicvoid setHarddrive(Integer hd){

harddrive = hd;

}

publicvoid setMemory(Integer me){

memory = me;

}

publicvoid setSpeed(float sp){

speed = sp;

}

}// close class

publicclass ComputerInvent{

publicstaticvoid main(String[] args){

HashMap hs =new HashMap();

Scanner kbd =new Scanner(System.in);

kbd.useDelimiter("\n");

while (true){

String msg =null;

Computer_listing compdb =null;

System.out.println("Enter ComputerID - Stop to end");

msg = kbd.next();

if (msg.equalsIgnoreCase("STOP")){

break;

}else{

compdb =new Computer_listing(msg);

}

System.out.println("Enter Computer Name");

compdb.setId(kbd.next());

System.out.println("Enter HardDrive");

compdb.setHarddrive(kbd.nextInt());

System.out.println("Enter Memory");

compdb.setMemory(kbd.nextInt());

System.out.println("Enter Speed");

compdb.setSpeed(kbd.nextFloat());

// add to data structure

hs.put(msg, hs);//line 110

}// close while loop

System.out.println("Done");

int i = 1;

Collection<ComputerInvent> values = hs.values();//line115

for (ComputerInvent temp : values){

System.out.println("Items are " + i +"" + temp.getClass());

i++;

}// close for loop

System.out.println("Enter ID to find");

String findit = kbd.next();

Computer_listing x =null;

if ((x = hs.get(findit)) !=null){//line 123

System.out.println("Found it");

System.out.println("Name is " + x.getClass());

}else{

System.out.println("Did not find it");

}// close else

}// close public static void

}// close public class ComputerInvent

Can someone please help me in solving this.

thanks

sandyR

[7302 byte] By [SandyReda] at [2007-11-27 2:11:29]
# 1
You are not using generics. You don't have to, but they are good to know about: http://java.sun.com/docs/books/tutorial/extra/generics/index.html
DrLaszloJamfa at 2007-7-12 2:04:41 > top of Java-index,Java Essentials,New To Java...
# 2

The code should have thrown hundreds of errors i wonder how u managed with few

import java.io.*;

import java.util.*;

class Computer_listing {

private String id;

private String name;

private Integer harddrive;

private Integer memory;

private float speed;

public Computer_listing(String idx) {

this.id = idx;

}

public Computer_listing(String cd, String cn, Integer hd, Integer me,

float sp) {

this.id = cd;

this.name = cn;

this.harddrive = hd;

this.memory = me;

this.speed = sp;

}

public String getId() {

return this.id;

}

public String getName() {

return this.name;

}

public Integer getHardrive() {

return this.harddrive;

}

public Integer getMemory() {

return this.memory;

}

public float getSpeed() {

return this.speed;

}

public void setId(String cd) {

this.id = cd;

}

public void setName(String cn) {

this.name = cn;

}

public void setHarddrive(Integer hd) {

this.harddrive = hd;

}

public void setMemory(Integer me) {

this.memory = me;

}

public void setSpeed(float sp) {

this.speed = sp;

}

}

public class ComputerInvent{

public static void main(String s[]){

HashMap<String,Computer_listing> hs = new HashMap<String,Computer_listing>();

Scanner kbd = new Scanner(System.in);

kbd.useDelimiter("\n");

while(true){

String msg = null;

Computer_listing compdb = null;

System.out.println("Enter <ComputerID> - Type <Stop> to end");

msg = kbd.next();

if (msg.equalsIgnoreCase("STOP"))

break;

else

compdb = new Computer_listing(msg);

System.out.println("Enter Computer Name:");

compdb.setId(kbd.next());

System.out.println("Enter HardDrive:");

compdb.setHarddrive(kbd.nextInt());

System.out.println("Enter Memory:");

compdb.setMemory(kbd.nextInt());

System.out.println("Enter Clock Speed:");

compdb.setSpeed(kbd.nextFloat());

hs.put(msg,compdb);

}

System.out.println("<Done>");

Collection<Computer_listing> values = hs.values();

int i = 0;

for (Computer_listing temp : values) {

System.out.println("Name["+ i + "]:" + temp.getName());

i++;

}

System.out.println("Enter ID to find");

String findit = kbd.next();

Computer_listing x = null;

if ((x = hs.get(findit)) != null) {

System.out.println("Found it");

System.out.println("Name is " + x.getName());

} else {

System.out.println("Did not find it");

}

}

}

Hope that works :)

Anyways i'd advice you learn basics of using java.util Classes first and then go about using generics if you are a starter.

REGARDS,

RaHuL

RahulSharnaa at 2007-7-12 2:04:41 > top of Java-index,Java Essentials,New To Java...
# 3
Thanks Rahul,I was re-read the info on data structures and now it make sense.sandyR
SandyReda at 2007-7-12 2:04:41 > top of Java-index,Java Essentials,New To Java...
# 4

Thanks for the help but now I get a scanner error when I run it

The errors above have been fixted but now the scanner is wrong when I run the program

An error will show up on these

error code is.

at java.util.Scanner.next(Scanner.java:1431)

at java.util.Scanner.nextFloat(Scanner.java:2267)

at computerInv.ComputerInvent.main(ComputerInvent.java:128)

System.out.println("Enter HardDrive");// this is line 128

compdb.setHarddrive(kbd.nextInt());

System.out.println("Enter Memory");//if I take out line 128 and 129 I get the same error

compdb.setMemory(kbd.nextInt());

System.out.println("Enter Speed");//like wise on this one

compdb.setSpeed(kbd.nextFloat());

Can you please tell me why and or show me how to correct this.

thanks

SandyReda at 2007-7-12 2:04:41 > top of Java-index,Java Essentials,New To Java...