wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes problem with hashCode() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem with hashCode() " Watch "problem with hashCode() " New topic
Author

problem with hashCode()

Martyn Clark
Ranch Hand

Joined: Apr 16, 2005
Posts: 108
HI
i think the problem i have is with my hashCode method can anybody help me out with this? basically i have three files Student, Storage and one for the main method. It is suppose to accept user input for name and course via JOptionPane until the maximum size is met then display the list of names and courses as the end result in the student class there has to be a method to calculate the hash from the sudents name changed to upperCase it should accept the size of the Students array. and in the Storage file there is a method to addStudent to the Student object array in the method i have to get the hash index from the Student objcet passed as an argument which represents it's prefered array position in the student object array. I am struggling to find a solution for this and would much appriciate any help with this, here are the files i have at the moment:

public class Student
{

private String name;
private String course;


Student(String studentName, String studentCourse)
{
name = studentName;
course = studentCourse;
}

public int hashCode(int maxNum)
{
int addASCII= name.charAt(0);
int hash=0;
for(int i=0; i<name.length(); i++)
{
addASCII = name.charAt(i);
}
hash = addASCII % maxNum;
return hash;
}

public String getName()
{
return name;
}
public String getCourse()
{
return course;
}
}

public class Storage
{

private Student[] students;

Storage(int maxNum)
{
students = new Student[maxNum];
}

public void addStudent(Student studentObj)
{
int hash = studentObj.hashCode();
if(hash>=0 && hash<students.length)
{

students[hash]= studentObj;
}
}
public int getArraySize()
{
return students.length;
}
public void display()
{
System.out.println("Student Name\tStudent Course");
for(int i=0; i<getArraySize(); i++)
{
System.out.println(students[i]);
}
}

}

import javax.swing.*;
public class StudentTestDrive
{

/**
* @param args
*/
public static void main(String[] args)
{
Storage store = new Storage(3);

for(int i=0; i<store.getArraySize(); i++)
{
String name = JOptionPane.showInputDialog("Please enter a name!");
String course = JOptionPane.showInputDialog("Please enter a course!");
Student newStudent = new Student(name, course);
store.addStudent(newStudent);
}
store.display();
}

}

Thank you


Martyn...<br /> <br />SCJP 1.4 SCWCD 1.4
Philip Heller
author
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
You didn't mention what symptom you're seeing, but I can see one problem. The hashCode() method you call in addStudent() has no args. The version you provide in Student has an int arg. Probably that's what you intended to call, but instead you're getting the zero-args version that Student inherits from Object.


Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
Martyn Clark
Ranch Hand

Joined: Apr 16, 2005
Posts: 108
Hi Philip
i have now put an int arg in the hasCode(3) but when i display i get null, null, and the package name with @1bc4459
im having trouble working out exactly how the hash index works when i try searching the net for some examples i only seem to find examples of hashTables which i am told i dont need for this excercise.
Thank you for your reply.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with hashCode()
 
Similar Threads
Arrays: Calling input from other methods and classes
calling a method
retrieving array
Array lengths
Adding object to array