D:\CurrentProjects\SearsInterviewCode\src\java\test_classes\ConvertString_2_NumericalValue.java |
1
2
3
4
5
6 package test_classes;
7
8 import constants.*;
9
10
11
12 @author
13
14 public class ConvertString_2_NumericalValue
15 {
16 public ConvertString_2_NumericalValue()
17 {
18
19 }
20
21
22
23 public static boolean isStringValidNumber(String passedStringValue)
24 {
25 boolean decimalPointFoundFalg = false;
26 int startingIndex = 0;
27
28 if( (passedStringValue.charAt(0) == '-')
29 ||
30 (passedStringValue.charAt(0) == '+')
31 )
32 {
33
34 startingIndex = 1;
35 }
36
37 for(int count = startingIndex; count < passedStringValue.length(); count++)
38 {
39 char tempChar = passedStringValue.charAt(count);
40 switch(tempChar)
41 {
42 case '0':
43 case '1':
44 case '2':
45 case '3':
46 case '4':
47 case '5':
48 case '6':
49 case '7':
50 case '8':
51 case '9':
52 break;
53 case ',':
54 if(decimalPointFoundFalg)
55 {
56 return(false);
57 }
58 break;
59 case '.':
60 if(decimalPointFoundFalg)
61 {
62 return(false);
63 }
64 decimalPointFoundFalg = true;
65 break;
66 default:
67 return(false);
68 }
69 }
70 return (true);
71 }
72
73
74
75 public static boolean isStringPostiveNumber(String passedStringValue)
76 {
77 if(passedStringValue.charAt(0) == '-')
78 return (false);
79 else
80 return (true);
81
82 }
83
84
85
86 public static double getStringDoubleValue(String passedStringValue)
87 {
88 double totalValue = 0;
89 int startingIndex = 0;
90 boolean breakFromTopLoop = false;
91
92 if( (passedStringValue.charAt(0) == '-')
93 ||
94 (passedStringValue.charAt(0) == '+')
95 )
96 {
97 startingIndex = 1;
98 }
99
100
101 int count = 0;
102 for(count = startingIndex; count < passedStringValue.length(); count++)
103 {
104 if(breakFromTopLoop)
105 break;
106 char tempChar = passedStringValue.charAt(count);
107 switch(tempChar)
108 {
109 case '0':
110 case '1':
111 case '2':
112 case '3':
113 case '4':
114 case '5':
115 case '6':
116 case '7':
117 case '8':
118 case '9':
119 totalValue = totalValue * 10 + (tempChar - '0');
120 break;
121 case ',':
122 break;
123 case '.':
124 breakFromTopLoop = true;
125 break;
126 default:
127 }
128 }
129
130
131 int decimalPosition = 0;
132
133 for(int loop = count; loop < passedStringValue.length(); loop++)
134 {
135 char tempChar = passedStringValue.charAt(loop);
136 switch(tempChar)
137 {
138 case '0':
139 case '1':
140 case '2':
141 case '3':
142 case '4':
143 case '5':
144 case '6':
145 case '7':
146 case '8':
147 case '9':
148 double tempDecimal = tempChar - '0';
149 for(int index = 0; index <= decimalPosition; index++)
150 {
151 tempDecimal = tempDecimal /10;
152 }
153 totalValue += tempDecimal;
154 break;
155 default:
156 }
157 decimalPosition++;
158 }
159 if( passedStringValue.charAt(0) == '-')
160 totalValue = - totalValue;
161 return (totalValue);
162 }
163
164
165
166 public static void main (String[] args)
167 {
168 String localString = "+234567.09878765";
169 System.out.println("string ======>:\t" + localString);
170 if(ConvertString_2_NumericalValue.isStringValidNumber(localString))
171 {
172 System.out.println("======valid number=================");
173 }
174 else
175 System.out.println("========Not valid number ==================");
176 double myValue = ConvertString_2_NumericalValue.getStringDoubleValue(localString);
177 System.out.println("string ======>:\t" + myValue);
178 }
179 }
180
181