|
I have this code here:
it uses text files and hash tables to keep some student records:
can somebody help me with the 'FindEntry' Method, i don't really get why it doesn't work....
public class HashTable {
public static class StudentRecord implements Serializable {
public String name,family;
public int stNr,flag;
public static final int SIZE=68;
public StudentRecord() {
name=" ";family=" ";stNr=0;flag=0;
}
public StudentRecord(String name,String family,int stNr,int flag){
this.name=name;
this.family=family;
this.stNr=stNr;
this.flag=flag;
}
public StudentRecord(int key,String value){
stNr=key;
setValue(value);
}
public String getValue(){
return (family+"#"+name+"#");
}
public void setValue(String value){
String t[]=new String[2];
int i=0;
StringTokenizer tokens=new StringTokenizer(value,"#");
while(tokens.hasMoreTokens()){
t[i]=tokens.nextToken();
i++;
}
this.family=t[0];
this.name=t[1];
}
public void write(RandomAccessFile file)throws IOException{
writeName(file,name);
writeName(file,family);
file.writeInt(stNr);
file.writeInt(flag);
}
public void writeName(RandomAccessFile file,String str)throws IOException{
StringBuffer buffer=null;
if(str!=null)
buffer=new StringBuffer(str);
else
buffer=new StringBuffer(15);
buffer.setLength(15);
file.writeChars(buffer.toString());
}
public void read(RandomAccessFile file)throws IOException{
name=readName(file);
family=readName(file);
stNr=file.readInt();
flag=file.readInt();
}
public String readName(RandomAccessFile file)throws IOException{
char name[]=new char[15];
for(int i=0;i<name.length;i++)
name[i]=file.readChar();
return new String(name).replace('\0',' ');
}
}
protected static class DefaultEqualityTester implements EqualityTester {
....
}
protected int n = 0;
protected int N;
File filename1=new File("D:\\b.txt");
RandomAccessFile file=new RandomAccessFile(filename1,"rw");
File filename2=new File("D:\\c.txt");
RandomAccessFile file2=new RandomAccessFile(filename2,"rw");
protected EqualityTester T;
protected int scale, shift;
public HashTable() throws FileNotFoundException, IOException {
N =1023;
T = new DefaultEqualityTester();
java.util.Random rand = new java.util.Random();
scale = rand.nextInt(N-1) + 1;
shift = rand.nextInt(N);
StudentRecord record=new StudentRecord();
for(int i=0;i<N;i++)
record.write(file);
}
public HashTable(int bN, EqualityTester tester) throws FileNotFoundException, IOException {
N = bN;
T = tester;
java.util.Random rand = new java.util.Random();
scale = rand.nextInt(N-1) + 1;
shift = rand.nextInt(N);
StudentRecord record=new StudentRecord();
for(int i=0;i<N;i++)
record.write(file);
}
protected int findEntry(Object key) throws InvalidKeyException, IOException {
int avail = 0;
checkKey(key);
int i = hashValue(key);
int j = i;
StudentRecord currentRecord=new StudentRecord();
do {
file.seek(i*StudentRecord.SIZE);
if (currentRecord == null) return -i - 1;
if (currentRecord.flag ==0) { avail = i; i = (i + 1) % N;
}
else if (T.isEqualTo(key,currentRecord.stNr))
return i;
else
i = (i + 1) % N;
} while (i != j);
return -avail - 1;
}
public Object get (Object key) throws InvalidKeyException, IOException {
StudentRecord currentRecord=new StudentRecord();
int i = findEntry(key);
if (i < 0) return null;
file.seek(i*StudentRecord.SIZE);
return currentRecord.getValue();
}
public void put (int key, String value) throws InvalidKeyException, IOException {
StudentRecord currentRecord = new StudentRecord(key, value);
if (n >= N/2) rehash();
int i = findEntry(key);
if (i < 0) {
file.seek((-i-1)*StudentRecord.SIZE);
currentRecord.write(file);
n++;
}
else {
file.seek(i*StudentRecord.SIZE);
currentRecord.write(file);
}
}
public void remove (Object key) throws InvalidKeyException, IOException {
int i = findEntry(key);
if (i < 0)
else{
StudentRecord currentRecord=new StudentRecord();
while(true){
do{
currentRecord.read(file);
}while(currentRecord.stNr!=key);
currentRecord.flag=0;
}
}
n--;
}.
.
.
.
thanks
|
|
|
|
|
0. Code is unformated
1. It is not urgent for ME.
If you want help, then help us. Format your code so that it is readable. Change the title so tha it is meaningful. Let us know what is happening and what is expected.
The more focused the question, the more liekly you are to get a response.
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
ok...so are you able to help now?
|
|
|
|
|
Still urgent is it?
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
i am doing web application.....how to connect the mysql database using javascript...actually i have got in jsp
|
|
|
|
|
Can you specify you requirement ....
I think Java script is for Client side processing.
what if user has disabled JavaScript???
Your database should be consistent, You can call Action/Servlet form JavaScript.
It's not enough to be the best, when you have capability to be great... Jai Shri Ram.
|
|
|
|
|
i am creating an application and i am stuck with my table. i want to be able to navigate through the table by clicking on either a "previous" or "next" button. the table should show display 10 records at a time. any help> thanks
|
|
|
|
|
Hi everyone,
Im having a problem on uploading a text file using jsp. Can anybody give me an idea or sample codes that i can study. Thank you guys in advance.
|
|
|
|
|
hi,jspsmart may help you.But now is not updated.
you can find it on below site:
http://www.whitehouse.net.cn/blog/article/jspsmart_download.html
|
|
|
|
|
How to get the URL of a web page from the address bar in Java that the user is currently working on dynamically i.e.., a program that can get the current working url to the user stored in a string?
Could anybody tell me how to get the URL of the address bar?
Thanks,
Sravanthi
modified on Wednesday, June 17, 2009 3:32 PM
|
|
|
|
|
<title> New Document </TITLE>
</head>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
alert(location);
</script>
</body>
</html>
|
|
|
|
|
I believe this is OK. I need to know if it is possible to draw graphics based on various conditions during run-time, but want some advice before I proceed further.
Thanks.
AF Pilot
|
|
|
|
|
You should be able to get the graphic object jButton.getGraphic() and manipulate it as you need. You should handle it during the refresh of the form as that ensures it is done at the right time.
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
Thank you, Nagy!
AF Pilot
|
|
|
|
|
Hey everyone,
i'm was wandering how i could implement a 'Serializable Hashtable'?
since i'm working on a program that needs to read some information (student profile such as name, student ID, contact detail etc) from a text file, in the form of hash table, and show, edit or even delete the info from that file.
I'd appriciate any sort of help.
thanks
|
|
|
|
|
java.util.HashMap implements Serializable.
So as long as the data items in there also do that then it works.
Note that a serialized object should not be considered 'text'. If you want something like a cvs file then you should probably just focus on that.
|
|
|
|
|
Hi,
I am trying to find a good reference on how to create a pop up social bookmarks menu. This site has exactly what I want. You can see it by clicking on any article and hovering over the 'Share' link at the top. A window pops up with links to facebook, diggit....etc. Does anyone know of a good example on how to do this and how to send the information to each of these sites?
Thanks!
Lisa
|
|
|
|
|
I'm using JDK5 (1.5.0_14) for compiling my programs, but my customers runs JDK6 (1.6.0_06) on that pc (say vista). Someone said my programs recompiled with JDK6 because target environgment is JDK6.
I doubt it, but weakend.
Can anybody explain these?
And I must say, I'm using jni dll targeting jni1.4 with above codes, that has some trouble; sometimes vm crashes in gc.
|
|
|
|
|
I want to know in what direction have i to go to program a look and feel?
|
|
|
|
|
This is a rather vague question. Do you mean a UI layout? Code layout? Function flow?
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
This is fully documented by Sun and there are a number of books that cover it. Try typing "java look and feel" into Google. It's a fairly big subject so don't expect too much of a detailed answer here. The easiest option might be to look at the Synth look and feel which takes away a lot of the hard work involved in coding a new L&F.
|
|
|
|
|
|
I am from C# background. I would suggest use ArrayList and then it may have a sort() function to either do the ascending/descending.
|
|
|
|
|
Hi,
you could start with some collection (a Vector, an array, ...) initialized with all the symbols in sorted order; then remove each symbol one enters; when done, list what remains.
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
|
|
|
|
|
Have a look at the <code>java.util.SortedSet<E></code> class for unique items.
Or add the elements to an <code>java.util.ArrayList<E></code> and then pass that through <code>java.util.Collections.sort()</code>.
Panic, Chaos, Destruction.
My work here is done.
modified on Tuesday, June 16, 2009 5:22 AM
|
|
|
|