프로그래밍
자바 글자수 세기 프로그램
금전
2018. 7. 26. 11:00
JavaFX를 이용해서 글자수세기 프로그램을 만들었다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14 |
inputTextField.setOnKeyReleased(new EventHandler<KeyEvent> () {
@Override
public void handle(KeyEvent event) {
//바이트 수 계산
byte [] bytes = inputTextField.getText().getBytes();
int byteLength = bytes.length;
String strLength = Integer.toString(byteLength);
actiontarget.setText(strLength + " bytes");
//글자수 계산 String strLength2 = Integer.toString(inputTextField.getLength());
actiontarget2.setText(strLength2 + " 자");
}
}); |
`cs |
*
getByte() 메소드는 시스템의 기본 문자셋으로 인코딩된 바이트 배열을 리턴한다.
byte[] bytes = 문자열.getBytes();
byte[] bytes = 문자열.getBytes(Charset charset);
알파벳은 1바이트, 한글은 2바이트로 변환한다.
바이트 배열을 다시 문자열로 변환할 때는
String str = new String(byte[] bytes, String charsetName);
글자가 입력될 때마다 글자수가 카운팅되어야 하므로 키보드 이벤트 속성을 이용했다.
setOnKeyReleased는 키보드가 release 되었을 때를 의미한다.