Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hashing Problem

 
Iain Palmer
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Iain Palmer
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21117
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Iain Palmer
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you a Computeach student doing the hashing assignment by any chance?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic