2013-10-17 2 views
0

안녕하세요. 내 Arduino에서 Serial comunication을 사용하여 처리하고 있습니다. 나는 코드의이 부분에 문제가 :NumberFormatExcepcion : 입력 문자열 :

void draw(){ 

    //read the string. 
    pot = arduino.readStringUntil(10); 

    //check for null values before casting to int 
    if(pot != null){ 
    num = Integer.parseInt(pot); 

    //draw depending on values 
    rect(0,0,100,100); 
    text(pot, 0,0); 
    } 
} 

라인 NUM =있는 Integer.parseInt (냄비); 항상 문제가됩니다. 내가 사용하는 문자열에는 항상 문제가 있습니다. 레이저 오류 메시지는 입니다. NumberFormatExcepcion : 입력 문자열 : "111" 끝에있는 숫자는 내가 읽고 자하는 번호입니다 (정확합니다). 하지만 어쨌든 그 문자열을 int로 변환 할 수는 없습니다. 오류 메시지의 숫자 int는 항상 끝에 공백이 있습니다. 나는 그것을 삭제하려고했지만 나는 할 수 없다. 내가 사용 냄비 = pot.substring (0, pot.length() - 1);냄비 = pot.replace ("", "");. 그러나 그것은 작동하지 않습니다.

답변

1

String 클래스의 trim 메서드를 사용해보십시오.

if(pot != null){ 
    pot = pot.trim() 
    num = Integer.parseInt(pot); 

이 정보가 도움이되기를 바랍니다.

+0

처리에는 자체 [int()] (http://processing.org/reference/intconvert_.html) 변환 방법이 있습니다. 두 경우 모두 완벽하게 작동합니다. –