adding character values in java -


import java.awt.*; //container, gridlayout, *, or etc... import javax.swing.*; //jframe, jlabel, *, or etc... import java.awt.event.*; public class numerologyec extends jframe {     private static final int width = 400;     private static final int height = 200;      private jlabel wordjl, sumjl;     private jtextfield wordtf, sumtf;      private jbutton calculatejb, exitjb;      private calculatebuttonhandler cbhandler;     private exitbuttonhandler ebhandler;     public  numerologyec()    {        settitle ("numerology credit");        wordjl = new jlabel ("enter word: ", swingconstants.right);         sumjl = new jlabel ("sum of letters: ", swingconstants.right);         wordtf = new jtextfield(10);         sumtf = new jtextfield(10);         calculatejb = new jbutton ("calculate");        cbhandler = new calculatebuttonhandler();        calculatejb.addactionlistener (cbhandler);         exitjb = new jbutton ("exit");        ebhandler = new exitbuttonhandler();        exitjb.addactionlistener (ebhandler);         container pane = getcontentpane();        pane.setlayout (new gridlayout (3, 2));         pane.add(wordjl);        pane.add(wordtf);        pane.add(calculatejb);        pane.add(exitjb);        pane.add(sumjl);        pane.add(sumtf);          setsize(width, height);        setvisible (true);        setdefaultcloseoperation (exit_on_close);     }      private class calculatebuttonhandler  implements actionlistener     {         public void actionperformed (actionevent e)         {             double lettersum;              lettersum = sumcharvalues;           }     }      private int sumcharvalues (string input){         string total = input.tolowercase();         int result = 0;         (int = 0, n = total.length(); < n; i++) {             char c = total.charat(i);             result += (c - 'a' + 1);         }         return result;     }      private class exitbuttonhandler implements actionlistener     {         public void actionperformed (actionevent e)         {           system.exit (0);           }     }     public static void main (string[] args)    {     numerologyec rectobject = new numerologyec();     }   } 

how implement sumcharvalues function in "private class calculatebuttonhandler implements actionlistener?" im trying add value of characters. beginner here guys. in advance help

you can sum characters casting int before performing arithmetic.
e.g.

result += ((int) c) - ((int) 'a') + 1 

that may overparenthesized, should work.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -