//예외 발생시키기

import java.util.Scanner;

public class ExceptionTest4

{

//강제로 예외 발생시키기

//1~10 사이의 값 입력 --> 1~10 사이의 값이 아니면 예외 발생시킴

public ExceptionTest4(){

start();

}

public void start(){

try{

Scanner scan = new Scanner(System.in);

System.out.print("1~10 사이의 값 입력 = ");

int num = scan.nextInt();

if(num<1 || num>10){ //1~10 사이가 아닐 때

throw new Exception("1~10 사이의 값을 입력하세요."); //강제로 예외발생

}


System.out.println("num = "+num*2000);

}catch(Exception e){

System.out.println(e.getMessage());

}

}



public static void main(String[] args) 

{

new ExceptionTest4();

}

}



Posted by Hyun CHO
,

테스트 화면


//메소드 예외처리


import java.util.Scanner;

import java.util.InputMismatchException;

class ExceptionTest3 

{

int a, b, c;

//메소드 예외처리

public ExceptionTest3() throws InputMismatchException, ArithmeticException{

start();

}

public void start() throws InputMismatchException, ArithmeticException{

output();

}

public void output() throws InputMismatchException, ArithmeticException{

input();

c = a / b;

System.out.println("c = "+c);

}

public void input() throws InputMismatchException{

Scanner scan = new Scanner(System.in);

System.out.print("a = ");

a = scan.nextInt();

System.out.print("b = ");

b = scan.nextInt();

}

}

=====================================================================

메인 화면

import java.util.InputMismatchException;

public class ExceptionTest3Main 

{

public static void main(String[] args)

{

try{

ExceptionTest3 et3 = new ExceptionTest3();

}catch(Exception e){

System.out.println("예외 발생");

}

}

}



Posted by Hyun CHO
,

class Exceptiontest 

{

public static void main(String[] args) 

{

try{

//실행문; - 정상프로그래밍 코드

System.out.println(args[0]); //런타임 에러

}catch(ArrayIndexOutOfBoundsException aioobe){

//실행문; - 예외 발생시 실행

System.out.println("args 배열에서 예외발생...");

}

//실행문;

System.out.println("예외범위 실행 후...");

}

}


=====================================================================


//예외처리하기 : try-catch문


import java.util.Scanner;

import java.io.*;

class Exceptiontest2 

{

Scanner scan = new Scanner(System.in);

public Exceptiontest2(){

start();

}

public void start(){

int[] num = new int [3];

try{

System.out.print("수1 = ");

num[0] = scan.nextInt();

System.out.print("수2 = ");

num[1] = scan.nextInt();


num[2] = num[0] / num[1];

System.out.println(num[0]+" / "+num[1]+" = "+num[2]);


//파일의 내용 읽어오기

File f = new File("z:/KOSMO/java/class3/InnerMain.java");

FileInputStream fis = new FileInputStream(f);


byte data[] = new byte[1024];

int cnt = fis.read(data);

System.out.println(new String(data));


}catch(ArithmeticException ae){ // 예외 발생 시

//ae.printStackTrace();

System.out.println(ae.getMessage());

}catch(ArrayIndexOutOfBoundsException aioobe){

System.out.println(aioobe.getMessage());

}catch(FileNotFoundException fnfe){

System.out.println("Stream 객체생성시 에러 발생...");

}catch(IOException ie){

System.out.println("IOException 에러 발생...");

}catch(Exception e){

e.printStackTrace();

}finally{ //예외 발생과 상관없이 무조건 실행되는 곳

System.out.println("finally 실행...");

}

}



public static void main(String[] args) 

{

new Exceptiontest2();

}

}



Posted by Hyun CHO
,

//익명 클래스로 팝업창(좌표) 만들기


import java.awt.Frame;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

class AnonymousTest2

{

Frame frm;

public AnonymousTest2(){

frm = new Frame("익명의 내부 클래스");

//frm.setSize(800, 600);

frm.setBounds(200, 100, 800, 600); //x, y 축 좌표 기준


frm.show();


//이벤트 등록

frm.addWindowListener(new WindowAdapter(){

//WindowAdapter 클래스의 추상메소드 오버라이딩

public void windowClosing(WindowEvent we){

frm.dispose();

System.exit(0);

}

});

}

public static void main(String a[]){

AnonymousTest2 at2 = new AnonymousTest2();

}

}



'응용 SoftWare > JAVA' 카테고리의 다른 글

[예외처리] 메소드 예외처리  (0) 2016.12.05
[예외처리] try-catch  (0) 2016.12.05
[익명 클래스] 오버라이딩을 이용한 팝업창 만들기  (0) 2016.12.05
내/외부 클래스  (0) 2016.12.05
[예제] String  (0) 2016.12.02
Posted by Hyun CHO
,

//오버라이딩을 이용한 팝업창 만들기

//Frame : 상속 또는 객체생성


import java.awt.Frame;

import java.awt.event.WindowListener;

import java.awt.event.WindowEvent;

public class AnonymousTest implements WindowListener

{

Frame frm;

public AnonymousTest(){ //생성자

frm = new Frame("연습용 창열기"); //윈도우 객체생성

//Frame 크기

frm.setSize(800, 600);

//화면 출력

frm.show();


//이벤트 등록 : addWindowsListener()

frm.addWindowListener(this);

//인터페이이스 파일 오버라이딩 : Windowlister

}

//윈도우 이벤트 오버라이딩

public void windowActivated(WindowEvent we){}

public void windowClosed(WindowEvent we){}

public void windowClosing(WindowEvent we){

frm.dispose(); //자원해제

System.exit(0); //프로그램종료

}

public void windowDeactivated(WindowEvent we){}

public void windowDeiconified(WindowEvent we){}

public void windowIconified(WindowEvent we){}

public void windowOpened(WindowEvent we){}


public static void main(String s[]){

AnonymousTest at = new AnonymousTest();

}

}



'응용 SoftWare > JAVA' 카테고리의 다른 글

[예외처리] try-catch  (0) 2016.12.05
[익명 클래스] 팝업창(좌표) 만들기  (0) 2016.12.05
내/외부 클래스  (0) 2016.12.05
[예제] String  (0) 2016.12.02
StringTokenizer 종류와 사용  (0) 2016.12.02
Posted by Hyun CHO
,

테스트 클래스


public class OuterClass

{

String name = "조현";

int number = 81;

public void output(){

System.out.println("name = "+name+", number = "+number);

//내부 클래스에서 선언된 변수는 사용할 수 없다.

//System.out.pringln("tel = "+tel);

}


//내부 클래스 : 외부 클래스와 상속관계. 외부 클래스 변수를 사용할 수 있다.

//외부와 내부에 같은 변수가 존재할 경우 자신이 속한 클래스 변수를 사용한다.

public class InnerClass

{

String tel = "010-9142-1348";

int age = 35;

int number = 20;

public void innerOutput(){

//외부와 내부 클래스에 같은 변수가 존재할 경우 '외부클래스명.this.변수명' 으로 사용한다.

//for(int i=1; i<=Outer.this.number; i++){

for(int i=1; i<=number; i++){

System.out.println("i = "+i);

}

}

}

}


=====================================================================

메인 클래스

public class  InnerMain

{

public static void main(String[] args) 

{

//내부 클래스 사용하기

//내부 클래스를 사용하기 위해서는 외부 클래스 객체가 있어야 한다.


//외부 클래스 객체생성

OuterClass oc = new OuterClass();

oc.output(); //실행 확인


//내부 클래스 객체생성 (= 외부클래스명.내부클래스명 객체명 = 외부클래스객체명.new 내부클래스명)

OuterClass.InnerClass oi = oc.new InnerClass();

oi.innerOutput();

}

}



Posted by Hyun CHO
,

/*

조건 : split 메소드 사용불가


실행결과

이메일 = hyun@bycho.kr


아이디 = hyun

도메인 = bycho.kr

*/

=====================================================================

방법 1

import java.util.Scanner;

import java.util.StringTokenizer;

public class StringEx1 

{

Scanner scan = new Scanner(System.in);

StringEx1(){

System.out.print("이메일 = ");

String mail = scan.next();

System.out.println();


StringTokenizer st = new StringTokenizer(mail, "@");


System.out.println("아이디 = "+st.nextToken());

System.out.println("도메인 = "+st.nextToken());


}


public static void main(String[] args) 

{

StringEx1 se = new StringEx1();

}

}

=====================================================================

방법 2 - StringTokenizer 상용 없이

import java.util.Scanner;

class StringEx1T 

{

public StringEx1T(){}


public void start(){

String email = input("이메일");

int index = email.indexOf("@");

String id = email.substring(0, index);

String domain = email.substring(index+1);


output("아이디", id);

output("도메인", domain);

}

public void output(String title, String contens){

System.out.println(title+" = "+contens);

}

//문자열 입력

//hyun@bycho.kr -> -1

public String input(String msg){

Scanner scan = new Scanner(System.in);

System.out.print(msg+" = ");

String email = scan.next();

if(email.indexOf("@")==-1){

System.out.println("이메일 주소를 잘못 입력하셨습니다.");

System.exit(0); //프로그램 종료

}

return email;

}

public static void main(String[] args) 

{

StringEx1T se1t = new StringEx1T();

se1t.start();

}

}



'응용 SoftWare > JAVA' 카테고리의 다른 글

[익명 클래스] 오버라이딩을 이용한 팝업창 만들기  (0) 2016.12.05
내/외부 클래스  (0) 2016.12.05
StringTokenizer 종류와 사용  (0) 2016.12.02
StringBuffer 종류와 사용  (0) 2016.12.02
String 종류와 사용  (0) 2016.12.02
Posted by Hyun CHO
,

//StringTokenizer 종류와 사용

import java.util.StringTokenizer;

class  StringTokenizerTest

{

StringTokenizerTest(){

String tel = "010-9142-1348";

StringTokenizer st = new StringTokenizer(tel, "-");

System.out.println("countTokens()="+st.countTokens());


while(st.hasMoreTokens()){ //토큰이 있는지 확인

System.out.println(st.nextToken());

}

}

public static void main(String[] args) 

{

StringTokenizerTest stt = new StringTokenizerTest();

}

}



'응용 SoftWare > JAVA' 카테고리의 다른 글

내/외부 클래스  (0) 2016.12.05
[예제] String  (0) 2016.12.02
StringBuffer 종류와 사용  (0) 2016.12.02
String 종류와 사용  (0) 2016.12.02
interface 클래스  (0) 2016.12.02
Posted by Hyun CHO
,

//StringBuffer 종류와 사용


class StringBufferTest 

{

public StringBufferTest(){}

public void start(){

StringBuffer sb1 = new StringBuffer("Spring FrameWork");

StringBuffer sb2 = new StringBuffer(50);


System.out.println("sb1.capacity()="+sb1.capacity());

System.out.println("sb2.capacity()="+sb2.capacity());


//객체에 문자열 추가

sb1.append("Programing");

System.out.println("sb1="+sb1);


//문자열을 중간에 추가

sb1.insert(sb1.indexOf("P"), "(스프링프레임웍)");

System.out.println("sb1.insert()="+sb1);


//문자열 뒤집기

sb1.reverse();

System.out.println("sb1.reverse()="+sb1);


//문자열 일부삭제

sb1.delete(3,5);

System.out.println("sb1.delete()="+sb1);

}

public static void main(String[] args) 

{

StringBufferTest sbt = new StringBufferTest();

sbt.start();

}

}



'응용 SoftWare > JAVA' 카테고리의 다른 글

[예제] String  (0) 2016.12.02
StringTokenizer 종류와 사용  (0) 2016.12.02
String 종류와 사용  (0) 2016.12.02
interface 클래스  (0) 2016.12.02
추상 클래스  (0) 2016.12.02
Posted by Hyun CHO
,

//String 메소드 종류와 사용


class StringTest 

{

char email[] = {'h','y','u','n','@','b','y','c','h','o','.','k','r'};

String tel = "010-9142-1348";

public StringTest(){

String name = "조현";

String tel = new String();

String addr = new String("서울시 양천구 신정동");


String email2 = new String(email);

int num = 1234;

//valueOf는 int를 문자열로 리턴

String num2 = new String(String.valueOf(num));


System.out.println("email2"+email2);

System.out.println("num2"+num2);


//String을 char[]배열로 얻어오기

char addr2[] = addr.toCharArray();

for(int i=0; i<addr2.length; i++){

System.out.println("addr2["+i+"]="+addr2[i]);

}

String str1 = "str1 조현";

String str2 = "str2 조현";

String str3 = new String("str3 조현");

String str4 = new String("str4 조현");


if(str1.equals(str2)){

System.out.println("str1과 str2는 같다.");

}else{

System.out.println("str1과 str2는 다른다.");

}

if(str3.equals(str4)){

System.out.println("str3과 str4는 같다.");

}else{

System.out.println("str3과 str4는 다른다.");

}

if(str1.equals(str3)){

System.out.println("str1과 str3은 같다.");

}else{

System.out.println("str1과 str3은 다르다.");

}


str1 = str1 + "입니다.";

str1 = str1 + "?";


start();

}


public StringTest(int a){}

public void start(){

String str = "Java Programing...";

String str1 = "language";

char a1 = str.charAt(5); //index 위치의 char 구하기

System.out.println("str.charAt(5)"+ a1);


int a2 = str.compareTo("JAVA"); //문자 크기 비교

System.out.println("str.compareTo="+a2);


int a3 = str.compareToIgnoreCase("java");

System.out.println("str.compareToIgnoreCase()"+a3);


String result = str.concat(str1); //문자열 정렬

System.out.println("str.concat(str1)"+result);


String emailStr = String.copyValueOf(email); //char 배열을 String

System.out.println("String.copyValueOf(email)"+emailStr);


//

byte strByte[] = str.getBytes(); //문자열을 아스키코드로 구하여 배열로 리턴

//System.out.ptinln(strByte);

//for(int i=0; i<strBytes.length; i++){

// System.out.println(strByte[i]);

//}

for(byte s : strByte){

System.out.println(s);

}

//특정문자의 위치 인덱스를 구한다.

int index = str.indexOf("a");

System.out.println("str.indexOf(\"a\")="+index);


index = str.indexOf("a",5);

System.out.println("str.indexOf(\"a,5\")="+index);


int lastIndex = str.lastIndexOf("r");

System.out.println("str.indexOf(\"r\")="+lastIndex);


//문자열 길이

int length = str.length();

System.out.println("str.length()="+length);


//문자열 치환

str = str.replace("Java","자바");

System.out.println("str.replace(\"JAVA\",\"자바\")"+str);


//문자열 조각내기

String tel2[] = tel.split("-");

for(String t : tel2){

System.out.println(t);

}


//일부문자열 얻어오기

String s1 = str.substring(4);

System.out.println("str.substring(4)="+s1);


String s2 = str.substring(3,8);

System.out.println("str.substring(3,8)="+s2);


//대소문자 바꾸기

String s3 = str.toLowerCase();

System.out.println("str.toLowerCase()"+s3);

String s4 = str.toUpperCase();

System.out.println("str.toUpperCase()"+s4);


String s5 = str.replace(" ","");

System.out.println("str.replace="+s5);


//문자열 좌우 공백문자 제거

String txt = "   abc   def   ";

String txtTrim = txt.trim();


System.out.println("txt.trim()["+txtTrim+"]");


System.out.println("valueOf="+String.valueOf(1234));

}


public static void main(String[] args)

{

new StringTest();

}

}




'응용 SoftWare > JAVA' 카테고리의 다른 글

StringTokenizer 종류와 사용  (0) 2016.12.02
StringBuffer 종류와 사용  (0) 2016.12.02
interface 클래스  (0) 2016.12.02
추상 클래스  (0) 2016.12.02
final  (0) 2016.12.01
Posted by Hyun CHO
,