• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Hashing Problem

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}

}
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23942
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you a Computeach student doing the hashing assignment by any chance?
 
He puts the "turd" in "saturday". Speaking of which, have you smelled this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic