package dk.dtu.brainfuckcomp;

import java.io.IOException;

public class Brainfuck {
	
	private int[] arr;
	private int lenght;
	private int pointer;
	
	public Brainfuck(int n) {
		pointer = 0;
		lenght = n;
		arr = new int[n];
	}
	
	public void parse(String str) throws UnknownSymbolExeption {
		parseHelper(str, false);
	}
	
	private void parseHelper(String str, boolean loop) throws UnknownSymbolExeption {
		if (loop) {
			while (arr[pointer] != 0) {
				parseString(str);
			}			
		} else {
			parseString(str);
		}
	}
	
	private void parseString(String str) throws UnknownSymbolExeption {
		int loopMode = 0;
		String loopString = "";
		for (char a: str.toCharArray()) {
			if (loopMode>0 && (loopMode != 1 || a != ']')) {
				loopString += a;
			}
			
			if (a == '[') {
				loopMode++;
				continue;
			}
			
			if (a == ']') {
				loopMode--;
			}
			
			if (loopString.length() > 0 && loopMode == 0) {
				parseHelper(loopString, true);
				loopString = "";
				continue;
			}
			
			if (loopMode == 0) {
				parseChar(a);
			}
		}
	}
	
	private void parseChar(char a) throws UnknownSymbolExeption {
		switch (a) {
		case '>': 	incPointer();
					break;
		case '<':	decPointer(); 	
					break;
		case '+': 	incValue();
					break;
		case '-': 	decValue();
					break;
		case '.': 	printCurr();
					break;
		case ',': 	inputChar();
					break;
		default: 	throw new UnknownSymbolExeption();
		}
	}

	private void incValue() {
		arr[pointer] += 1;
	}
	
	private void decValue() {
		arr[pointer] -= 1;
	}
	
	private void incPointer() {
   		pointer += 1;
   		if (pointer >= lenght) {
   			throw new NullPointerException();
   		}
	} 
	
	private void decPointer() {
   		pointer -= 1;
   		if (pointer < 0) {
   			throw new NullPointerException();
   		}
	} 
	
	private void printCurr() {
		System.out.print((char) arr[pointer]);
	}

	private void inputChar() {
        System.out.println("Enter a Character:");
        try {
            arr[pointer] = System.in.read();
        }
        catch (IOException e){
            System.out.println("Error reading from user");
        }
	}
	
	public String toString() {
		String str = "\n[";
		
		for (int n: arr) {
			str += " " + n + " ";
		}
		
		str += "]\n  ";
		
		for (int i = 0; i < pointer; i++) {
			str += "  ";
			for (int j = 0; j < String.valueOf(arr[i]).length(); j++) {
				str += " ";
			}
		}
		return str += "^";
	}
 
}

@@ -40,22 +40,20 @@
 ava.
-io.IOException
+util.Scanner
 ;%0A%0Ap
@@ -141,16 +141,62 @@
 ointer;%0A
+%09private Scanner sc = new Scanner(System.in);%0A
 %09%0A%09publi
@@ -1441,25 +1441,24 @@
 n();%0A%09%09%7D%0A%09%7D%0A
-%0A
 %09private voi
@@ -1970,168 +1970,50 @@
     
-try %7B%0A            arr%5Bpointer%5D = System.in.read();%0A        %7D%0A        catch (IOException e)%7B%0A            System.out.println(%22Error reading from user%22);%0A        %7D
+arr%5Bpointer%5D = sc.next().toCharArray()%5B0%5D;
 %0A%09%7D%0A

Change to use Scanner for input