Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Numerology

Geetha Sharanya
Greenhorn
Posts: 2

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:

Maneesh Godbole
Saloon Keeper
Posts: 11185
15
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
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: 11185
15
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: 42968
73
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: 11185
15
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: 50245
79
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.