-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathstringToInteger.java
More file actions
41 lines (30 loc) · 1.46 KB
/
stringToInteger.java
File metadata and controls
41 lines (30 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* File: StringToInteger.java
* ---------------------------------
* დაწერეთ stringToInteger მეთოდი, რომელსაც გადაეცემა სტრინგი და აბრუნებს მთელ რიცხვს.
* ჩათვალეთ, რომ გადაცემული სტრინგი მხოლოდ ციფრებისგან შედგება და არ იწყება 0-ით.
* მაგალითად “234” გადაცემის შემთხვევაში მეთოდმა უნდა დააბრუნოს 234. ასევე ჩათვალეთ,
* რომ დაბრუნებული მნიშვნელობა დადებითია და ეტევა int-ში.
*/
import acm.program.*;
public class ConsoleProgrammSample extends ConsoleProgram {
//Function bellow transforms received string into the Integer
private int stringToInteger(String num) {
int result = 0;
for(int i = 0; i < num.length(); i++) {
result *= 10;
result += (num.charAt(i) - '0');
}
return result;
}
public void run() {
//Getting input from user
String num = readLine("Enter number in string: ");
//Checking if received string is not empty
if(num.length() != 0) {
int result = stringToInteger(num);
println("Converted: " + result);
}else {
println("No valid string received.");
}
}
}