| Author |
Hashing Problem
|
Iain Palmer
Ranch Hand
Joined: Jul 28, 2004
Posts: 56
|
|
I am writing a program that get a name and sorts its hash code out I've been given a code fragment that gets the first characters ASCII code but I can't seem to get it to loop the through the entire string all I get is the first one .Ive tried a for loop,if statment and do while loop import javax.swing.JOptionPane; class Student { String name; String course; int hashTotal; public Student() { } public Student(String name) { this.name = name; } public Student(String name,String course) { this.name = name; this.course = course; } public int getDetails(String name) { if( name != null && name.length() > 0) { char letter = name.charAt( 0 ); //get the character from String int asciiValue = letter; //get the ASCII value of the character System.out.print("The character '"+ letter); System.out.println("' has an ASCII value of "+ asciiValue); } return hashTotal; } public static void main(String [] args) { Student s = new Student(); s.getDetails(); } }
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
Here's a very common form of loop: Are you familiar with the parts of this? "for" says we're going to repeat the code block inside the brackets that follow. "int i = 0" sets up an index starting at 0. Using "i" came from the ancient days when i was a special integer variable in Fortran. It's not magic, be everybody does it. "i < string.length()" is an expression that must be true to enter the loop body. So this will loop as long as i is not more than string length. "i++" increments i after each trip through the loop body, right before we test the middle expression again. So this will start with charAt(0), then repeat for charAt(1), then charAt(2) and so on until we have used all the chars in the String. Let us see what you can make from this template.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Iain Palmer
Ranch Hand
Joined: Jul 28, 2004
Posts: 56
|
|
|
I've tried a for loop by putting it after the if statment but all i got was the first character of the string would it help if i changed the char varaible to string?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16690
|
|
Originally posted by Iain Palmer: I've tried a for loop by putting it after the if statment but all i got was the first character of the string would it help if i changed the char varaible to string?
I think it would help if you show us the code that you tried. I am not sure what you mean by "putting it after the if". Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Iain Palmer
Ranch Hand
Joined: Jul 28, 2004
Posts: 56
|
|
Here is the code import javax.swing.JOptionPane; class Student { String name; String course; int hashTotal; public Student() { } public Student(String name) { this.name = name; } public Student(String name,String course) { this.name = name; this.course = course; } public int getDetails(String name) { name = JOptionPane.showInputDialog("Please enter a letter"); System.out.println(name.toUpperCase()); if( name != null && name.length() > 0) { for (int i = 0 ;i<name.length();i++) { char letter = name.charAt( 0 ); //get the character from String int asciiValue = letter; //get the ASCII value of the character System.out.print("The character '"+ letter); System.out.println("' has an ASCII value of "+ asciiValue); } } return hashTotal; } }
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
|
This is the kind of bug you can stare at for hours and not see, or walk in fresh (like me) and spot in a second. Believe me, I'm on the "staring for hours" side of things often enough, too. You're incrementing i from 0 to the length, but look at your call to charAt(). You're always using 0 instead of i. See what changing that does.
|
 |
David Harkness
Ranch Hand
Joined: Aug 07, 2003
Posts: 1646
|
|
Having been in this position many, many times, I can tell you exactly what happens next: Iain smacks his forehead and shouts, "D'oh!"
|
 |
Iain Palmer
Ranch Hand
Joined: Jul 28, 2004
Posts: 56
|
|
Thank you i had tried it before but I was using char letter = name.charAt([i]) Because I thought an array had to be referenced [i] Its now working fine
|
 |
Maureen Charlton
Ranch Hand
Joined: Oct 04, 2004
Posts: 218
|
|
|
Are you a Computeach student doing the hashing assignment by any chance?
|
 |
 |
|
|
subject: Hashing Problem
|
|
|