'Java'에 해당되는 글 11건

  1. 2010.09.07 정규식을 이용한 주민등록번호 추출
  2. 2010.08.31 java
  3. 2010.08.26 쓰레드
  4. 2010.08.24 오토박싱/언박싱
  5. 2010.08.23 Stream
  6. 2010.08.23 Exception
  7. 2010.08.19 자바 주요 클래스
  8. 2010.08.17 static vs final
  9. 2010.08.17 내부 클래스 용도
  10. 2010.08.16 주민등록번호 추출

정규식을 이용한 주민등록번호 추출

|
정규식을 이용한 주민등록 번호 추출 프로그램
RRNTest.java
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//\d\d(0[1-9]|1[0-2])([0-2][1-9]|3[0-1])[1-4]\d{6}

//Regular Expression
class RRN {
	String rrn;
	String PatternStr = "\\d\\d(0[1-9]|1[0-2])([0-2][1-9]|3[0-1])[1-4]\\d{6}";
	Pattern pat = Pattern.compile(PatternStr);

	Matcher mat;
	boolean found;
	boolean more = true;

	public String FindRRN(String str) {
		mat = pat.matcher(str);
		found = mat.find();
		if (found)
		{
			rrn = mat.group();
			more = true;
			//end()는 mat.group() 후에 사용 가능
			str = str.substring(mat.end());
		}
		else
		{
			rrn = "Not found";
			more = false;
		}
		return str;
	}
}

public class RRNTest {
	public static void main(String[] args) {
		String str = "8204254093614abc128714cd7903241081511AE@5605047145666#";
		boolean more = true;
		RRN r = new RRN();
		while (r.more) {
			str = r.FindRRN(str);
			if (r.rrn != "Not found") System.out.println(r.rrn);
		}
	}
}


-

'Java' 카테고리의 다른 글

java  (0) 2010.08.31
쓰레드  (0) 2010.08.26
오토박싱/언박싱  (0) 2010.08.24
Stream  (0) 2010.08.23
Exception  (0) 2010.08.23
And

java

|
레이아웃 변경하기
setLayout(new CardLayout());
Container c = getContentPane();
c.setLayout(new GridLayout());

쓰레드
일시 정지 상태로 만드는 것 : suspend, join, sleep, wait
깨우는 것 : resume, notify

'Java' 카테고리의 다른 글

정규식을 이용한 주민등록번호 추출  (0) 2010.09.07
쓰레드  (0) 2010.08.26
오토박싱/언박싱  (0) 2010.08.24
Stream  (0) 2010.08.23
Exception  (0) 2010.08.23
And

쓰레드

|
run() 메소드는 Runnable 인터페이스에 존재
Thread 클래스는 Runnable 인터페이스를 임플러먼츠한다.
따라서 Thread클래스를 사용하여 쓰레드를 사용하려면 run() 메소드를 기술해줘야 하고, start() 메소드로 실행해야 한다. start() 메소드는 run() 메소드를 실행한다. 그냥 run()을 실행하면 멀티쓰레드로 동작하지 않는다.

Runnable 인터페이스를 임플러먼츠한 후,

객체를 생성하여 그 객체를 
Thread t = new Thread(rr, "Thread1"); 에 넘기고
t.start()로 실행하면 start()가 rr의 run()을 실행함

쓰레드 돌릴때 run()에 반드시 sleep(1)이라도 주어야 cpu 부하 줄어들고, 멀티 쓰레드시 효율이 높다

'Java' 카테고리의 다른 글

정규식을 이용한 주민등록번호 추출  (0) 2010.09.07
java  (0) 2010.08.31
오토박싱/언박싱  (0) 2010.08.24
Stream  (0) 2010.08.23
Exception  (0) 2010.08.23
And

오토박싱/언박싱

|
int n = 10;
Integer wrapN = new Integer(n); //박싱

n = wrapN.intValue(); //언박싱

wrapN = n; //오토 박싱
n = wrapN; //오토 언박싱

'Java' 카테고리의 다른 글

java  (0) 2010.08.31
쓰레드  (0) 2010.08.26
Stream  (0) 2010.08.23
Exception  (0) 2010.08.23
자바 주요 클래스  (0) 2010.08.19
And

Stream

|
Object
ㄴInputStream
   ㄴByteArrayInputStream
   ㄴFileInputStream
   ㄴFilterInputStream
      ㄴBufferedInputStream
   ㄴObjectInputStream
   ㄴPipedInputStream
   ㄴSequenceInputStream
   ㄴStringBufferInputStream
ㄴOutputStream
   ㄴByteArrayOutputStream
   ㄴFileOutputStream
   ㄴFilterOutputStream
      ㄴBufferedOutputStream
      ㄴPrintStream
   ㄴObjectOutputStream
   ㄴPipedOutputStream


'Java' 카테고리의 다른 글

쓰레드  (0) 2010.08.26
오토박싱/언박싱  (0) 2010.08.24
Exception  (0) 2010.08.23
자바 주요 클래스  (0) 2010.08.19
static vs final  (0) 2010.08.17
And

Exception

|
1. 정수를 0으로 나누는 경우 : ArithmeticException
2. 배열의 첨자가 음수 값을 가지는 경우 : IndexOutOfBoundException
3. 배열의 첨자가 배열의 크기를 벗어나는 경우 : IndexOutOfBoundException
4. 유효하지 않은 형변환
5. 입출력 시에 쓰기/읽기 오류가 발생하는 경우
6. 레퍼런스 변수가 null인 상태에서 객체를 참조할 경우 : NullPointerexception

               java.lang.object
               java.lang.Throwable
java.lang.Error             java.lnag.Exception
                                 java.lang.RuntimeException

java.lang.Throwable : 모든 예외의 최상위 클래스
java.lang.Error : 복구가 어렵거나 불가능한 예외 상황으로 일반적으로 오류 메시지를 출력하고 실행이 중단된다.
OutOfMemoryError, StackOverflowError, LinkageError
java.lang.Exception : 예외 처리를 반드시 해야 한다.
ClassNotFoundException, IOException, InterruptedException
java.lang.RuntimeException : 실행 중에 발생할 수 있는 예외 클래스로 예외 처리를 하지 않아도 무방하다.
IllegalArgumentException, IndexOutOfBoundsException, NullPointerException

try {
method();
} catch (Exception e) {
...
} finally {
...
}

'Java' 카테고리의 다른 글

오토박싱/언박싱  (0) 2010.08.24
Stream  (0) 2010.08.23
자바 주요 클래스  (0) 2010.08.19
static vs final  (0) 2010.08.17
내부 클래스 용도  (0) 2010.08.17
And

자바 주요 클래스

|
Object obj;
boolean equals() : 두 객체의 내용이 같은지 비교한다. (사용자 정의 클래스의 객체인 경우에는 오버라이딩하여 각 필드마다 같은지 비교해야 한다.??)
== 연산자는 참조변수가 가리키는 인스턴스가 같은지 비교한다.
String str1 = "java";
String str2 = "java";
String str3 = new String("java");

str1 == str2 : true
str1 == str3 : false
str2 == str3 : false

str1.equals(str2) : true
str1.equals(str3) : true
str2.equals(str3) : true

Class getClass() : 객체의 클래스 이름을 Class 형으로 반환
int hashCode() : 객체의 해시 코드를 반환
notify()
notifyAll()
String toString() : 객체의 문자열을 반환
wait()

String 클래스
한 번 생성된 객체는 절대 변하지 않는다. --> 메모리 낭비 심함
String str1 = "Java";
str1.replace("Java", "C"); -> 힙에 새로운 String 객체 "C"가 생성
println(str1); -> 여전히 "Java"를 출력
str1 = str1.replace("Java", "C"); -> str1이 "C"를 가리키게 한다.
println(str1); -> "C"를 출력

StringBuffer 클래스
동적 문자열 처리가 가능하다. 속도가 몇 십 배 느려질 수 있다.

'Java' 카테고리의 다른 글

Stream  (0) 2010.08.23
Exception  (0) 2010.08.23
static vs final  (0) 2010.08.17
내부 클래스 용도  (0) 2010.08.17
주민등록번호 추출  (0) 2010.08.16
And

static vs final

|
static : 클래스 필드 정의시 사용
final : 상수로 만들 때 사용

인스턴수마다 상수를 갖는다는 것은 별 의미가 없다. 어차피 같은 값이므로.
따라서 상수는 static final 로 선언한다.


죄송합니다. lmb543.egloos.com의 아임머신님은 
글 내용이 다른 곳에 복사되는 것을 원하지 않습니다.
출처링크를 사용해주세요.

[자바문법] static, final 수정자

'Java' 카테고리의 다른 글

Exception  (0) 2010.08.23
자바 주요 클래스  (0) 2010.08.19
내부 클래스 용도  (0) 2010.08.17
주민등록번호 추출  (0) 2010.08.16
자바 소수점 출력  (0) 2010.08.13
And

내부 클래스 용도

|
http://thdwns2.springnote.com/pages/539147

1. 인스턴스 내부 클래스
Ex0608.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Outer {
	int outData = 100;
	class Inner {
		public void printInner() {
			System.out.println(outData);
		}
	}
}

public class Ex0608 {
	public static void main(String args[]) {
		Outer out = new Outer();
		Outer.Inner in = out.new Inner();
		in.printInner();
	}
}

2. static 내부 클래스
원래 클래스 앞에는 static이 붙을 수 없다. 하지만 내부 클래스일 경우는 데이터 타입처럼 사용되기 때문에 가능한데 의미는 Outer 클래스의 인스턴스가 없어도 생성 가능한 내부 클래스라는 것이다. static이므로 이미 Outer 클래스 생성시에 정의되어 있는 상태이다. 
Ex0609.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Outer {
	static int outStaticData = 100;
	static class Inner {
		public void printInner() {
			System.out.println(outStaticData);
		}
	}
}

public class Ex0609 {
	public static void main(String args[]) {
		Outer.Inner in = new Outer.Inner();
		in.printInner();
	}
}

3. 지역 클래스
Ex0610.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Outer {
	int outData = 100;
	Object outerMethod() {
		final int localFinalData = 200;
		class Inner extends Object {
			public String toString() {
				return "내부 클래스의 메소드 : " + outData + 
				", " + localFinalData;
			}
		}
		return new Inner();
	}
}
public class Ex0610 {
	public static void main(String args[]) {
		Outer out = new Outer();
		Object obj = out.outerMethod();
		System.out.println(obj.toString());
	}
}

4. 내부 무명 클래스
Ex0611.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Outer {
	int outData = 100;
	
	Object outerMethod() {
		final int localfinalData = 200;
		
		return (new Object() {
			public String toString() {
				return "내부 클래스의 메소드 : " + outData + 
				" " + localfinalData;
			}
		});
	}
}
public class Ex0611 {
	public static void main(String[] args) {
		Outer out = new Outer();
		Object obj = out.outerMethod();
		System.out.println(obj.toString());
	}
}

'Java' 카테고리의 다른 글

Exception  (0) 2010.08.23
자바 주요 클래스  (0) 2010.08.19
static vs final  (0) 2010.08.17
주민등록번호 추출  (0) 2010.08.16
자바 소수점 출력  (0) 2010.08.13
And

주민등록번호 추출

|

RRNTest.java
by Parsing
class RRN {
	String[] validNum = new String[30];
	public void ValidateNumbers(String str) {
		//System.out.println(str.length());
		int i = 0, j = 0, k = 0;
		while (i < str.length()-12) {
			for (j = 0; j < 13; ++j) {
				if (!(str.charAt(i+j) >= '0' && str.charAt(i+j) <= '9')) {
					break;
				}
				else if (j == 12) {
					validNum[k] = str.substring(i, i+13);
					k++;
				}
			}
			++i;
		}
	}
	
	public void Validate() {
		for (int i = 0; i < validNum.length; ++i) {
			if (validNum[i] != null && validNum[i] != "Invalid") {
				System.out.println(ValidateYY(validNum[i]));
			}
		}
	}
	
	public String ValidateYY(String str) {
		return ValidateMM(str);
	}
	
	public String ValidateMM(String str) {
		if (str.charAt(2) == '0' && 
				str.charAt(3) >= '0' && str.charAt(3) <= '9') {
			return ValidateDD(str);
		}
		else if (str.charAt(2) == '1' && 
				str.charAt(3) >= '0' && str.charAt(3) <= '2') {
			return ValidateDD(str);
		}
		return "Invalid";
	}
	
	public String ValidateDD(String str) {
		if (str.charAt(4) == '0' && 
				str.charAt(5) >= '0' && str.charAt(5) <= '9')
		{
			return ValidateRemain(str);
		}
		else if (str.charAt(4) == '1' && 
				str.charAt(5) >= '0' && str.charAt(5) <= '9')
		{
			return ValidateRemain(str);
		}
		else if (str.charAt(4) == '2' && 
				str.charAt(5) >= '0' && str.charAt(5) <= '9')
		{
			return ValidateRemain(str);
		}
		else if (str.charAt(4) == '3' && 
				str.charAt(5) >= '0' && str.charAt(5) <= '1')
		{
			return ValidateRemain(str);
		}
		return "Invalid";
	}
	
	public String ValidateRemain(String str) {
		return str;
	}
}

public class RRNTest {
	public static void main(String[] args) {
		String str = "abc128714cd7903241081511AE@560547145666#";
		
		RRN r = new RRN();
		r.ValidateNumbers(str);
		r.Validate();
	}
}

by Regular Expression
C++pasted just now: 
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class RRN {
	String rrn;
	String PatternStr = "\\d\\d(0[1-9]|1[0-2])([0-2][1-9]|3[0-1])[1-4]\\d{6}";
	//Pattern pat = Pattern.compile("\\w\\w");
	//Pattern pat = Pattern.compile("(?i)]*[src] *= *[\"\']{0,1}([^\"\'\\ >]*)");
	Pattern pat = Pattern.compile(PatternStr);

	Matcher mat;
	boolean found;

	public String FindRRN(String str) {
		mat = pat.matcher(str);
		found = mat.find();
		if (found)
			rrn = mat.group();
		else
			rrn = "Not found";
		return rrn;
	}
}

public class RRNTest {
	public static void main(String[] args) {
		String str = "abc128714cd7903241081511AE@560547145666#";
		String result;
		RRN r = new RRN();
		result = r.FindRRN(str);
		System.out.println(result);
	}
}




-end

'Java' 카테고리의 다른 글

Exception  (0) 2010.08.23
자바 주요 클래스  (0) 2010.08.19
static vs final  (0) 2010.08.17
내부 클래스 용도  (0) 2010.08.17
자바 소수점 출력  (0) 2010.08.13
And
prev | 1 | 2 | next