While
class While_Ex1
{
// Error : unreported exception : main 옆에 throw Exception 선언
public static void main(String[] args) throws Exception
{
//일반적으로 while 문은 반복횟수를 모를 때 사용함.
//일반적으로 for문은 반복횟수를 알 때 사용함
int ch;
int count = 0;
while(true)
{
//키보드로 부터 값을 입력받기
ch = System.in.read(); // 문자 1개씩 입력 받기
if(ch==-1)break;
System.out.printf("%c", ch);
count++;
//Thread.sleep(3000);
}
System.out.printf("반복횟수:%d\n", count);
}
}
// continue : (쉬지않고)계속되다, (쉬지않고)계속하다
// \r : 키보드 포커스를 맨 앞으로 이동
// \n : 키보드 엔터기능
import java.io.FileReader;
class While_Ex2
{
public static void main(String[] args) throws Exception
{
//파일 읽기 객체
FileReader fr = new FileReader("While_Ex2.java");
int count = 0;
//수행코드
while(true)
{
int readChar = fr.read();//파일로부터 1문자 읽어오기
if(readChar==-1)break;
System.out.printf("%c", readChar);
count++;
//Thread.sleep(10);
}
// fr : EOF(-1)
System.out.printf("\n 총 문자수:%d(개)\n", count);
//닫기
fr.close();
}
}
// unreported exception FilenotFoundException : 파일을 읽는데 파이일이 없으면 어떻게 할 것인가?
// unreported exception IOException : 파일을 못읽으면 어떻게 할 것인가?
import java.io.FileReader;
class While_Ex2
{
public static void main(String[] args) throws Exception
{
//파일 읽기 객체
FileReader fr = new FileReader("While_Ex2.java");
int count = 0;
int alpha_upper_COUNT=0;//대문자 갯수 저장변수
int alpha_lower_COUNT=0;//소문자 갯수 저장변수
int number_count=0;//숫자 갯수 저장변수
int hangul_count=0;//한글 갯수
//수행코드
while(true)
{
int readChar = fr.read();//파일로부터 1문자 읽어오기
if(readChar==-1)break;
System.out.printf("%c", readChar);
//읽어온 문자(readChar) 대문자냐?
if(readChar>='A' && readChar<='Z') //대문자 범위에 있냐?
{
alpha_upper_COUNT++;
}
//읽어온 문자(readChar) 소문자냐?
if(readChar>='a' && readChar<='z') //대문자 범위에 있냐?
{
alpha_lower_COUNT++;
}
number_count = alpha_upper_COUNT + alpha_lower_COUNT;
//한글체크
if( (readChar & 0x8000) == 0x8000 )
{
hangul_count++;
}
count++;
//Thread.sleep(10);
}
// fr : EOF(-1)
/*
'0'=>48
'A'=>65
'Z'=>90
'a'=>97
*/
System.out.printf("\n 총 문자수:%d(개)\n", count);
System.out.printf("\n 총 대문자수:%d(개)\n", alpha_upper_COUNT);
System.out.printf("\n 총 소문자수:%d(개)\n", alpha_lower_COUNT);
System.out.printf("\n 총 문자수:%d(개)\n", number_count);
System.out.printf("\n 한글 갯수:%d(개)\n", hangul_count);
//닫기
fr.close();
}
}
// unreported exception FilenotFoundException : 파일을 읽는데 파이일이 없으면 어떻게 할 것인가?
// unreported exception IOException : 파일을 못읽으면 어떻게 할 것인가?