problem with .charAt()
in
Programming Questions
•
1 year ago
Hi everyone. Im new to processing (i started 5 days ago
)
I've been progamming an sketch in which you press any key on the key board and it gives you the ascii and binary values. That part is working great. However, i want to be able to represent the binary value in a graphical way, so my sketch is progammed to draw grey circles for 0 and green circles for 1(the binary values). But when i run my sketch it highlights some part with the function charAt() on it and then gives this error messages:
Exception in thread "Animation Thread" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.java:558)
at char_to_bin_.draw(char_to_bin_.java:73)
at processing.core.PApplet.handleDraw(PApplet.java:1631)
at processing.core.PApplet.run(PApplet.java:1530)
at java.lang.Thread.run(Thread.java:655)
i hope that anypne can explain me how t slove this. Thank a lot.
CODE:
- int letter = 0; //declare variables
- char car;
- char compare;
- String binaryString ;
- void setup(){
- size(1000,300); //size and background color
- background(0);
- }
- void draw(){
- background(0);
- fill(255);
- binaryString=binary(letter); //convert to bin
- if(binaryString.length()<8){ //make sure the binary string always has 8 characters
- if(binaryString.length()==7){ //to make sure we draw 8 circles
- binaryString = "0" + binaryString;
- }else{
- if(binaryString.length()==6){
- binaryString = "00" + binaryString;
- }else{
- if(binaryString.length()==5){
- binaryString = "000" + binaryString;
- }else{
- if(binaryString.length()==4){
- binaryString = "0000" + binaryString;
- }}}}}
- if(letter <300){ //write on the screen our values
- text(car,200,50);
- text (letter,390,50);
- text(binaryString,550,50 );
- }
- text("the binary is:",450,50); //write on the screen the desciption of our values
- text("the ascii value is:",250,50);
- text("the character received is:",10,50);
- for(int i=0; i<8 ; ++i){ //draw the circles with grey for 0 and green for 1
- if(binaryString.charAt(i)==0){ //this is the problematic line
- fill(100);
- }else{
- fill(0,255,0);}
- if(i==0){
- i=1;
- ellipse(i*100,height/2,80,80 );
- i=0;
- }else{
- ellipse(i*100,height/2,80,80 );}
- }
- }
- void keyPressed(){ //the part to analyse the key that was pressed
- letter=key;
- car=key;
- }
1