Code Optimization
-
I just finished writing this program, what it does is takes your name and assigns each letter a number and adds them up, ie. Tanner would give you 27 The program then checks to see if the number is one or two digits, which in this case it is two (2 and 7) It then adds those two digits together. Comments explain everything What I would like input on is if anyone could give me a couple pointers on how to optimize my code, make it faster or take away some of the writing to reduce it's length
import javax.swing.*;
public class bruce_tanner_A2Q2
{
public static void main (String [] args)
{
int firstNameTotal = 0;
int secondNameTotal, thirdNameTotal;
int currentLetter;String input = JOptionPane.showInputDialog("Numerology Calculator -\\nEnter a Name"); int inputLength = input.length(); //Checks how long the string is to be used as a maximum in the for loop //For loop which for(int i = 0;i < inputLength;i++) { currentLetter = (int)input.charAt(i) % 60; // Converts charAt's return into an integer, modulus 60 for less writing //Each if statement checks for uppercase and lowercase of each letter //A, J, S if (currentLetter == 5 || currentLetter == 14 || currentLetter == 23 || currentLetter == 37 || currentLetter == 46 || currentLetter == 55) { firstNameTotal++; } //B, K, T else if (currentLetter == 6 || currentLetter == 15 || currentLetter == 24 || currentLetter == 38 || currentLetter == 47 || currentLetter == 56) { firstNameTotal = firstNameTotal + 2; } //C, L, U else if (currentLetter == 7 || currentLetter == 16 || currentLetter == 25 || currentLetter == 39 || currentLetter == 48 || currentLetter == 57) { firstNameTotal = firstNameTotal + 3; } //D, M, V else if (currentLetter == 8 || currentLetter == 17 || currentLetter == 26 || currentLetter == 40 || currentLetter == 49 || currentLetter == 58) { firstNameTotal = firstNameTotal + 4; } //E, N, W else if (currentLetter == 9 || currentLetter == 18 || currentLetter == 27 || currentLetter == 41 || currentLetter == 50 || currentLetter == 59) { firstNameTotal = firstNameTotal + 5; } //F, O, X else if (currentLetter == 10 || currentLetter == 19 || currentLetter == 28 || currentLetter == 42 || currentLetter == 51 || currentLetter == 0) { firstNameTotal = firstNameTotal + 6; } //G, P, Y else if (currentLetter == 11 || currentLetter == 2
-
I just finished writing this program, what it does is takes your name and assigns each letter a number and adds them up, ie. Tanner would give you 27 The program then checks to see if the number is one or two digits, which in this case it is two (2 and 7) It then adds those two digits together. Comments explain everything What I would like input on is if anyone could give me a couple pointers on how to optimize my code, make it faster or take away some of the writing to reduce it's length
import javax.swing.*;
public class bruce_tanner_A2Q2
{
public static void main (String [] args)
{
int firstNameTotal = 0;
int secondNameTotal, thirdNameTotal;
int currentLetter;String input = JOptionPane.showInputDialog("Numerology Calculator -\\nEnter a Name"); int inputLength = input.length(); //Checks how long the string is to be used as a maximum in the for loop //For loop which for(int i = 0;i < inputLength;i++) { currentLetter = (int)input.charAt(i) % 60; // Converts charAt's return into an integer, modulus 60 for less writing //Each if statement checks for uppercase and lowercase of each letter //A, J, S if (currentLetter == 5 || currentLetter == 14 || currentLetter == 23 || currentLetter == 37 || currentLetter == 46 || currentLetter == 55) { firstNameTotal++; } //B, K, T else if (currentLetter == 6 || currentLetter == 15 || currentLetter == 24 || currentLetter == 38 || currentLetter == 47 || currentLetter == 56) { firstNameTotal = firstNameTotal + 2; } //C, L, U else if (currentLetter == 7 || currentLetter == 16 || currentLetter == 25 || currentLetter == 39 || currentLetter == 48 || currentLetter == 57) { firstNameTotal = firstNameTotal + 3; } //D, M, V else if (currentLetter == 8 || currentLetter == 17 || currentLetter == 26 || currentLetter == 40 || currentLetter == 49 || currentLetter == 58) { firstNameTotal = firstNameTotal + 4; } //E, N, W else if (currentLetter == 9 || currentLetter == 18 || currentLetter == 27 || currentLetter == 41 || currentLetter == 50 || currentLetter == 59) { firstNameTotal = firstNameTotal + 5; } //F, O, X else if (currentLetter == 10 || currentLetter == 19 || currentLetter == 28 || currentLetter == 42 || currentLetter == 51 || currentLetter == 0) { firstNameTotal = firstNameTotal + 6; } //G, P, Y else if (currentLetter == 11 || currentLetter == 2
for (int i = 0;i < inputLength; i++) {
currentLetter = (int)input.charAt(i) % 60; //No need for this.
currentLetter = input[i]; //Replaced by this insteadswitch (currentLetter) { case 'A': case 'J': case 'S': firstNameTotal += 1; break; case 'B': case 'K': case 'T': firstNameTotal += 2; break; //etc...
}
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
for (int i = 0;i < inputLength; i++) {
currentLetter = (int)input.charAt(i) % 60; //No need for this.
currentLetter = input[i]; //Replaced by this insteadswitch (currentLetter) { case 'A': case 'J': case 'S': firstNameTotal += 1; break; case 'B': case 'K': case 'T': firstNameTotal += 2; break; //etc...
}
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Oh dear.. cases totally slipped my mind. Also, I never knew that you could do something like
currentLetter = input[i]
. I'm assuming that that will access the String as a character array and return the letter at position i?TannerB wrote:
I'm assuming that that will access the String as a character array and return the letter at position i?
yup. the [] operator gets the character at position passed in parameter (0-based). also, a thing I shown in my sample, which i'm not sure you noticed, is using the characters literals instead of their ascii code... this is very handy when you come later and read back your code
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
for (int i = 0;i < inputLength; i++) {
currentLetter = (int)input.charAt(i) % 60; //No need for this.
currentLetter = input[i]; //Replaced by this insteadswitch (currentLetter) { case 'A': case 'J': case 'S': firstNameTotal += 1; break; case 'B': case 'K': case 'T': firstNameTotal += 2; break; //etc...
}
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
Hello everybody, I would implement it like this to avoid if or switch statements :
int incrementAmount;
for(int i = 0;i < inputLength;i++){
//input[i] gives a "The type of the expression must
//be an array type but it resolved to String", but
//there is no need for '%60'currentLetter = input.charAt(i); incrementAmount = (Character.toUpperCase(currentLetter)-'A')%9+1; firstNameTotal += incrementAmount;
}
Also we can replace :
incrementAmount = (Character.toUpperCase(currentLetter)-'A')%9+1;
By :
incrementAmount = (Character.toUpperCase(currentLetter)-'A')%('J'-'A')+1;
just to be more readable, that means "restart counting after the letter 'J'", which is better than "restart counting after the 9th letter". I also just noticed that you don't manage the case where thirdNameTotal is equal to 0.
-
Hello everybody, I would implement it like this to avoid if or switch statements :
int incrementAmount;
for(int i = 0;i < inputLength;i++){
//input[i] gives a "The type of the expression must
//be an array type but it resolved to String", but
//there is no need for '%60'currentLetter = input.charAt(i); incrementAmount = (Character.toUpperCase(currentLetter)-'A')%9+1; firstNameTotal += incrementAmount;
}
Also we can replace :
incrementAmount = (Character.toUpperCase(currentLetter)-'A')%9+1;
By :
incrementAmount = (Character.toUpperCase(currentLetter)-'A')%('J'-'A')+1;
just to be more readable, that means "restart counting after the letter 'J'", which is better than "restart counting after the 9th letter". I also just noticed that you don't manage the case where thirdNameTotal is equal to 0.
yup, that's a pretty smart implementation... i didn't took too much time to think on a better optimization, but yours is good
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]