File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Numerology Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Numerology" Watch "Numerology" New topic
Author

Numerology

Geetha Sharanya
Greenhorn

Joined: Jun 12, 2009
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:
"ABOUT"=1+2+15+21+20=59
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10248
    
    8

Hi Geetha,

Welcome to the Ranch. What have you got so far? Have you given any thought on how you would approach this problem?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Geetha Sharanya
Greenhorn

Joined: Jun 12, 2009
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

Joined: Jul 26, 2007
Posts: 10248
    
    8

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
Marshal

Joined: Mar 22, 2005
Posts: 41508
    
  53
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.


Ping & DNS - my free Android networking tools app
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10248
    
    8

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

Joined: Oct 13, 2005
Posts: 38353
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Numerology