Advertisement
Lisek94

Rozpoznawator liczb

Oct 17th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. public class trening {
  2.     public static void main(String[] args) {
  3.         System.out.println(trening.strToInt("12e5"));
  4.     }
  5.  
  6.     public static int strToInt(String str) {
  7.         StringBuilder resultsInString = new StringBuilder();
  8.         Integer results = 0;
  9.         int ascii = 0;
  10.         int iMultiplier = 1;
  11.         try {
  12.  
  13.             Character odczyt = str.charAt(0);
  14.             ascii = odczyt;
  15.  
  16.             if (ascii == 45) {
  17.                 for (int i = 1; i < str.length(); i++) {
  18.                     odczyt = str.charAt(i);
  19.                     ascii = odczyt;
  20.                     if (ascii > 47 && ascii < 58) {
  21.                         resultsInString.append(odczyt);
  22.                     } else if (ascii == 69 || ascii == 101) {
  23.                         odczyt = str.charAt(++i);
  24.                         ascii = odczyt;
  25.                         if (ascii > 47 && ascii < 58)
  26.                             iMultiplier = (int) Math.pow(10, Double.parseDouble(odczyt.toString()));
  27.                         break;
  28.                     } else {
  29.  
  30.                         break;
  31.                     }
  32.                 }
  33.                 int finalResults = Integer.parseInt(resultsInString.toString());
  34.                 results = finalResults * iMultiplier * -1;
  35.  
  36.             } else if (ascii > 47 && ascii < 58) {
  37.                 odczyt = str.charAt(0);
  38.                 ascii = odczyt;
  39.                 resultsInString.append(odczyt);
  40.                 for (int i = 1; i < str.length(); i++) {
  41.                     odczyt = str.charAt(i);
  42.                     ascii = odczyt;
  43.                     if (ascii > 47 && ascii < 58) {
  44.                         resultsInString.append(odczyt);
  45.                     } else if (ascii == 69 || ascii == 101) {
  46.                         odczyt = str.charAt(++i);
  47.                         ascii = odczyt;
  48.                         if (ascii > 47 && ascii < 58)
  49.                             iMultiplier = (int) Math.pow(10, Double.parseDouble(odczyt.toString()));
  50.                         break;
  51.                     } else {
  52.                         break;
  53.                     }
  54.                 }
  55.                 int finalResults = Integer.parseInt(resultsInString.toString());
  56.                 results = finalResults * iMultiplier;
  57.             }
  58.  
  59.             else if (ascii == 43) {
  60.                 for (int i = 1; i < str.length(); i++) {
  61.                     odczyt = str.charAt(i);
  62.                     ascii = odczyt;
  63.                     if (ascii > 47 && ascii < 58) {
  64.                         resultsInString.append(odczyt);
  65.                     } else if (ascii == 69 || ascii == 101) {
  66.                         odczyt = str.charAt(++i);
  67.                         ascii = odczyt;
  68.                         if (ascii > 47 && ascii < 58)
  69.                             iMultiplier = (int) Math.pow(10, Double.parseDouble(odczyt.toString()));
  70.                         break;
  71.                     } else {
  72.                         break;
  73.                     }
  74.                 }
  75.                 int finalResults = Integer.parseInt(resultsInString.toString());
  76.                 results = finalResults * iMultiplier;
  77.             }
  78.  
  79.         } catch (NumberFormatException e) {
  80.             results = 0;
  81.         }
  82.  
  83.         return results;
  84.  
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement