Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Code Optimization

Code Optimization

Scheduled Pinned Locked Moved Java
cssalgorithmsperformancetutorialcode-review
6 Posts 3 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TannerB
    wrote on last edited by
    #1

    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
    
    T 1 Reply Last reply
    0
    • T TannerB

      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
      
      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      for (int i = 0;i < inputLength; i++) {
      currentLetter = (int)input.charAt(i) % 60; //No need for this.
      currentLetter = input[i]; //Replaced by this instead

      switch (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]

      T D 2 Replies Last reply
      0
      • T toxcct

        for (int i = 0;i < inputLength; i++) {
        currentLetter = (int)input.charAt(i) % 60; //No need for this.
        currentLetter = input[i]; //Replaced by this instead

        switch (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]

        T Offline
        T Offline
        TannerB
        wrote on last edited by
        #3

        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?

        T 1 Reply Last reply
        0
        • T TannerB

          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?

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          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]

          1 Reply Last reply
          0
          • T toxcct

            for (int i = 0;i < inputLength; i++) {
            currentLetter = (int)input.charAt(i) % 60; //No need for this.
            currentLetter = input[i]; //Replaced by this instead

            switch (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]

            D Offline
            D Offline
            douss
            wrote on last edited by
            #5

            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.

            T 1 Reply Last reply
            0
            • D douss

              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.

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              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]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups