Source for de.webdings.tools.CharToInt

   1: /* CharToInt.java - Copyright (c) 2005 by Stefan Thesing
   2:  <p>This file is part of Webdings Tools.</p>
   3:  <p>Webdings Tools is free software; you can redistribute it and/or modify
   4:  it under the terms of the GNU General Public License as published by
   5:  the Free Software Foundation; either version 2 of the License, or
   6:  (at your option) any later version.</p>
   7: <p>Webdings Tools is distributed in the hope that it will be useful,
   8: but WITHOUT ANY WARRANTY; without even the implied warranty of
   9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10: GNU General Public License for more details.</p>
  11: <p>You should have received a copy of the GNU General Public License
  12: along with Webdings Tools; if not, write to the<br>
  13: Free Software Foundation, Inc.,<br>
  14: 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA<br>
  15: */
  16: package de.webdings.tools;
  17: 
  18: /**
  19:  * CharToInt is used to convert textual represenations
  20:  * of integer numbers into primitive data type int.
  21:  * 
  22:  * @author Stefan Thesing<br>
  23:  * Website: <a href="http://www.webdings.de">http://www.webdings.de</a>
  24:  * @version 1.0.1 10.08.2005
  25:  */
  26: public class CharToInt {
  27:     /**
  28:      * @param c A char representation of an int number
  29:      * @return int value of the represented number
  30:      * @throws NumberFormatException
  31:      * @throws Exception if c is not a number character.
  32:      */
  33:     public static int convert(char c) throws NumberFormatException 
  34:     {
  35:         int ascii_wert = c;
  36:         if(ascii_wert<48 || ascii_wert>57) {
  37:           throw new NumberFormatException("Error: " + c + "is not a number!");
  38:         } else {
  39:           return ascii_wert-48;
  40:         }
  41:     }
  42:     
  43:     /**
  44:      * @param s A char array representation of an int number
  45:      * @return int value of the represented number
  46:      * @throws NumberFormatException
  47:      * @throws Exception if s contains a non-number character.
  48:      */
  49:     public static int convert(char[] s) throws NumberFormatException 
  50:     {
  51:         int i;
  52:         int ergebnis = 0;
  53:         for(i=0;i<s.length;++i) {
  54:           ergebnis = ergebnis*10 + convert(s[i]);
  55:         }
  56:         return ergebnis;
  57:     }
  58:     
  59:     /**
  60:      * @param s A String representation of an int number
  61:      * @return int value of the represented number
  62:      * @throws NumberFormatException
  63:      * @throws Exception if s contains a non-number character.
  64:      */
  65:     public static int convert(String s) throws NumberFormatException 
  66:     {
  67:         return convert(s.toCharArray());
  68:     }
  69:     
  70:     /**
  71:      * @param s A StringBuffer representation of an int number
  72:      * @return int value of the represented number
  73:      * @throws NumberFormatException
  74:      * @throws Exception if s contains a non-number character.
  75:      */
  76:     public static int convert(StringBuffer s) throws NumberFormatException 
  77:     {
  78:         return convert(new String(s));
  79:     }
  80: }

© 2005 by Stefan Thesing;
Verbatim copying and redistribution of this entire page are permitted provided this notice is preserved.