Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Numerology

 
Geetha Sharanya
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I want to write a java program to do this function:
If A=1, B=2,.... so on... when a string is entered, the total value of the string should be displayed.
for example:
"ABOUT"=1+2+15+21+20=59
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Geetha,

Welcome to the Ranch. What have you got so far? Have you given any thought on how you would approach this problem?
 
Geetha Sharanya
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually this is my friend's doubt.. The logic she has come up with is as following:

import java.util.*;
import java.io.*;
public class numerology
{
public static void main(String args[])
{
int a=1;
int b=2;
int ab=0;
int idx=0; int tokenCount;
String words[]=new String [500];
String message="a b a b a b";
StringTokenizer st=new StringTokenizer(message);
tokenCount= st.countTokens();
System.out.println("Number of tokens = " + tokenCount);
while (st.hasMoreTokens()) // is there stuff to get?
{
words[idx]=st.nextToken();
idx++;
}
for (idx=0;idx<tokenCount; idx++)
{
if(words[idx]=="a")
{
ab=ab+1;
}
else
if(words[idx]=="b")
{
ab=ab+2;
}
}
if(ab<5)
{
System.out.print("**************");
}
else
{
System.out.println("------------");
}
}
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it out and got a bunch of stars! I was expecting some magic number which would make me rich and famous!

On a serious note, one way to approach the problem would be
1) Define a collection of characters. Since you are interested in the index of the characters, I would suggest a java.util.List
2)String has a charAt method which you can use to extract individual characters from the index string
3)Once you get the individual character, look up its index. The List has a useful indexOf() method which you can choose.
4) Figure out all indices and let the user know the magic number.

In future, please use code tags
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
words[idx]=="a"

String equality is tested with the "equals" method, not with "==". If, on the other hand, you're after character equality -which makes more sense here- then you need to use single quotes instead of double quotes.

If this was my problem, then a "Map<Character, Integer>" would feature in it prominently. It would have the characters I wanted to count as keys ('a', 'b', etc.) and the numbers assigned to them as values (1, 2, etc.). That way I could avoid hardcoding either the characters or the numbers in the code that does the counting; those would only occur in the code that sets up the Map, thereby making changes much easier.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
words[idx]=="a"

If this was my problem, then a "Map<Character, Integer>" would feature in it prominently.


Very true. But a Map might be difficult for a beginner's level.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a much simpler way to do it, when you find out that you can get a String to a char[] array, and also you find out that chars are actually numbers and you can do arithmetic with them.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic