-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnePointFive.java
More file actions
74 lines (58 loc) · 1.74 KB
/
OnePointFive.java
File metadata and controls
74 lines (58 loc) · 1.74 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Nicole Kulakowski
// Question 1.5
// Implement a method to perform basic string compression
// using the counts of repeated characters. For example,
// the string aabcccccaaa would become a2b1c5a3. If the
// "compressed" string would not become smaller than the
// original string, your method should return the original
// string.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class OnePointFive{
private String input;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private ArrayList<String> finalString = new ArrayList<String>();
public static void main(String[] args){
OnePointFive opf = new OnePointFive();
System.out.print("Enter string:\t");
opf.parseString();
}
public void parseString(){
try{
input = br.readLine();
input+="\0";
System.out.println(input);
char currentChar = '\0';
int currentCount = 1;
int idxFinal = 0;
for(int i = 0; i<input.length(); i++){
// if the next char will be different
if( i != 0 && input.charAt(i) != currentChar){
idxFinal++;
}
// if current is equal to previous
if (currentChar == input.charAt(i)){
currentCount++;
}
// new character
else{
currentCount = 1;
currentChar = input.charAt(i);
}
try{
finalString.set(idxFinal, currentChar+Integer.toString(currentCount));
} catch(IndexOutOfBoundsException e){
finalString.add(idxFinal, currentChar+Integer.toString(currentCount));
}
}
for(int i = 0; i<finalString.size()-1 ; i++){
System.out.print(finalString.get(i));
}
System.out.println();
} catch(IOException e){
System.out.println("readLine failed");
}
}
}