Numerology
posted 16 years ago
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi Geetha,
Welcome to the Ranch. What have you got so far? Have you given any thought on how you would approach this problem?
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
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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("------------");
}
}
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("------------");
}
}
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 16 years ago
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
posted 16 years ago
Very true. But a Map might be difficult for a beginner's level.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
| No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |








