Skip to content

Instantly share code, notes, and snippets.

@jclapp23
Last active August 29, 2015 14:13
Show Gist options
  • Save jclapp23/d250159d8c0d4752af23 to your computer and use it in GitHub Desktop.
Save jclapp23/d250159d8c0d4752af23 to your computer and use it in GitHub Desktop.
_ _ _ _ _ _ _
|_| | _||_||_ |_ ||_| _|
_| ||_ | _||_| ||_| _|
912456783
---------------------------------------------------------------------
package kata;
import java.io.IOException;
import java.util.Arrays;
public class Problem {
private static String[] lines;
public static void main(String[] args) throws IOException {
String fileName = "C:/kata.txt";
try{
ReadFile file = new ReadFile(fileName);
Problem.lines = file.OpenFile();
int i;
for ( i=0; i < Problem.lines.length; i++ ) {
System.out.println( Problem.lines[ i ] ) ;
}
OcrOperations ocr = new OcrOperations();
char[][][] ocrNumbersArray = ocr.splitLinesIntoOcrNumbersArray(Problem.lines);
String number = ocr.convertOcrLineToNumberString(ocrNumbersArray);
System.out.println(number);
}catch(IOException e){
System.out.println( e.getMessage() );
}
}
}
---------------------------------------------------------------------------------------------------
package kata;
import java.io.IOException;
import java.util.Arrays;
public class OcrOperations {
public char[][][] splitLinesIntoOcrNumbersArray(String[] linesArr){
char[][][] numbersArray;
numbersArray = new char[9][3][3];
int numberOfLines = linesArr.length;
for (int i=0; i < numberOfLines; i++){
int currLineNum = i;
String currLine = linesArr[i];
for (int cnt = 0; cnt < currLine.length() ; cnt++){
char c = currLine.charAt(cnt);
int curPos = cnt % 3;
numbersArray[Math.round(cnt/3)][currLineNum][curPos] = c;
}
}
//System.out.println(Arrays.deepToString(numbersArray[8]));
return numbersArray;
}
public String convertOcrLineToNumberString(char[][][] ocrNumbersArray){
String numStr = "";
for ( int i=0; i < ocrNumbersArray.length; i++ ) {
numStr += this.convertOcrNumberToIntegerChar(ocrNumbersArray[i]);
}
return numStr;
}
public char convertOcrNumberToIntegerChar(char[][] ocrNumberArray){
char[][] zero = {{' ', '_', ' '},
{'|', ' ', '|'},
{'|', '_', '|'}};
char[][] one = {{' ', ' ', ' '},
{' ', ' ', '|'},
{' ', ' ', '|'}};
char[][] two = {{' ', '_', ' '},
{' ', '_', '|'},
{'|', '_', ' '}};
char[][] three = {{' ', '_', ' '},
{' ', '_', '|'},
{' ', '_', '|'}};
char[][] four = {{' ', ' ', ' '},
{'|', '_', '|'},
{' ', ' ', '|'}};
char[][] five = {{' ', '_', ' '},
{'|', '_', ' '},
{' ', '_', '|'}};
char[][] six = {{' ', '_', ' '},
{'|', '_', ' '},
{'|', '_', '|'}};
char[][] seven = {{' ', '_', ' '},
{' ', ' ', '|'},
{' ', ' ', '|'}};
char[][] eight = {{' ', '_', ' '},
{'|', '_', '|'},
{'|', '_', '|'}};
char[][] nine = {{' ', '_', ' '},
{'|', '_', '|'},
{' ', '_', '|'}};
if(Arrays.deepEquals(ocrNumberArray, zero))
return '0';
if(Arrays.deepEquals(ocrNumberArray, one))
return '1';
if(Arrays.deepEquals(ocrNumberArray, two))
return '2';
if(Arrays.deepEquals(ocrNumberArray, three))
return '3';
if(Arrays.deepEquals(ocrNumberArray, four))
return '4';
if(Arrays.deepEquals(ocrNumberArray, five))
return '5';
if(Arrays.deepEquals(ocrNumberArray, six))
return '6';
if(Arrays.deepEquals(ocrNumberArray, seven))
return '7';
if(Arrays.deepEquals(ocrNumberArray, eight))
return '8';
if(Arrays.deepEquals(ocrNumberArray, nine))
return '9';
return 'E';
}
}
-------------------------------------------------------------------------------------------------------------
package kata;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class ReadFile {
private String path;
public ReadFile(String filePath){
path = filePath;
}
public String[] OpenFile() throws IOException{
FileReader fr = new FileReader(path);
BufferedReader textReader = new BufferedReader(fr);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
int i;
for (i=0; i < numberOfLines; i++){
textData[i] = textReader.readLine();
}
textReader.close();
return textData;
}
public int readLines() throws IOException{
FileReader fileToRead = new FileReader(path);
BufferedReader bf = new BufferedReader(fileToRead);
String aLine;
int numberOfLines = 0;
while ( ( aLine = bf.readLine() ) != null){
numberOfLines++;
}
bf.close();
return numberOfLines;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment