23.01.13(Java)

MH S·2023년 1월 13일

Java

목록 보기
6/16

예외처리




package ch10;

import java.io.FileNotFoundException;
import java.io.FileReader;

public class ExceptionEx5 {
	public static void main(String[] args) {
		try {
			FileReader fr = myRead("aaa.txt");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static FileReader myRead(String name) throws FileNotFoundException {
		FileReader fr = new FileReader(name);
		return fr;
	}
}
package ch10;

public class ExceptionEx6 {
	public static void main(String[] args) {
		try {
			exce3();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void exce1() throws Exception{}
	public static void exce2() throws Exception {
		exce1();
	}
	public static void exce3() throws Exception {
		exce2();
	}
}
package ch10;

public class ExceptionEx7 {
	int a=100;
	public void m(int b) throws Exception{
		if(b==0)
			throw new Exception("a는 0으로 나누면 안돼요.");
		else
			System.out.println(a+" / "+b+" = "+(a/b));
	}
	public static void main(String[] args) {
		ExceptionEx7 et= new ExceptionEx7();
		try {
			et.m(10);
			et.m(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}




Object




package ch11;

import java.lang.*;//생략

class Point{}
public class ObjectEx1 {
	public static void main(String[] args) {
		Point p = new Point();
		System.out.println("클래스 이름 : "+p.getClass());
		System.out.println("해쉬코드 : "+p.hashCode());
		//toString 생략가능
		System.out.println("객체문자열 : "+p.toString());
		Point p1 = new Point();
		System.out.println("클래스 이름 : "+p1.getClass());
		System.out.println("해쉬코드 : "+p1.hashCode());
		//toString 생략가능
		System.out.println("객체문자열 : "+p1.toString());
		String s= new String("금요일");
		System.out.println("객체문자열 : "+s.toString());
	}
}
package ch11;

class Point2 {
	int x, y;

	public Point2(int x, int y) {
		this.x = x;
		this.y = y;
	}

	@Override
	public String toString() {
		return "(" + x + "," + y + ")";
	}
}

public class ObjectEx2 {
	public static void main(String[] args) {
		Point2 p = new Point2(10, 20);
		System.out.println(p);
	}
}




String




package ch11;

class Point3 {
}

public class StringEx1 {
	public static void main(String[] args) {
		Point3 p1 = new Point3();
		Point3 p2 = new Point3();
		System.out.println(p1 == p2);
		int a = 10;
		int b = 10;
		System.out.println(a == b);
		System.out.println("------------");
		
		//new 연산자 없이 객체를 생성하는 유일한 클래스
		//new 없이 만들어진 String 객체는 String 저장소에 만들어지고
		//생성할때 동일한 문자열 값이 있으면 참조를 하게된다.
		String s1 = new String("Java");
		String s2 = new String("Java");
		String s3 = "Java";//생성
		String s4 = "Java";//재사용
		System.out.println(s1==s2);
		System.out.println(s3==s4);
		System.out.println(s1==s3);
	    System.out.println("------------");
	    //객체의 문자열의 내용을 비교
	    System.out.println(s1.equals(s2));
	    System.out.println(s1.equals(s3));
	    System.out.println(s3.equals(s4));
	    
	    System.out.println(p1.equals(p2));
	    System.out.println("------------");
	    String s5 = "APPLE";
	    String s6 = "apple";
	    
	    System.out.println(s5.equals(s6));
	    System.out.println(s5.equalsIgnoreCase(s6));
	}
}
package ch11;

public class StringEx2 {
	public static void main(String[] args) {
		String str = "Java Programming";

		int len = str.length();
		System.out.println("len : " + len);
		System.out.println(str.toLowerCase());
		System.out.println(str.toUpperCase());

		String str2 = str.substring(5);
		String str3 = str.substring(5, 10);
		System.out.println("str2 : " + str2);
		System.out.println("str3 : " + str3);

		// 8번째 문자
		char c1 = str.charAt(8);
		System.out.println("8번째 문자 : " + c1);
		// 짝수 자리 문자만 출력하시오.
		for (int i = 0; i < str.length(); i++) {
			if (i % 2 == 0)
				System.out.println(str.charAt(i));
		}
		// a문자는 몇번째 자리에 있는가?
		// Java Programming
		int idx1 = str.indexOf('a');
		System.out.println("\n" + "idx1 : " + idx1);
		int idx2 = str.lastIndexOf('a');
		System.out.println("idx2 : " + idx2);
		// a를 q로 변환해서 출력하시오.
		String str4 = str.replace('a', 'q');
		System.out.println("str4");
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == 'a') {
				System.out.print('q');
			} else
				System.out.print(str.charAt(i));
		} // --for
			// str을 반대로 출력 charAt. ?
		for (int i = str.length() - 1; i >= 0; i--) {
			System.out.print(str.charAt(i));
		}

		StringBuffer sb = new StringBuffer(str);
		String reverse = sb.reverse().toString();
		System.out.println(reverse);

		String str5 = "Java&JSP&Android&Iot&Spring";
		String str6[] = str5.split("&");
		for (int i = 0; i < str6.length; i++) {
			System.out.println(str6[i]);
		}
		String str7 = "     JSPStudy      ";
		System.out.println("***" + str7.trim() + "***");
		int idx3 = 22;//정수를 문자로 변환
		String str8 = String.valueOf(idx3);
		String str9 = idx3+"";
		System.out.println(str + " : " + str9);
	}
}
package ch11;

public class StringEx3 {
	public static void main(String[] args) {
		String str = "전지현이가 백화점에서 팬사인회를 연다." + "전지현은 5일 오후 3시 서울 소공동 롯데 백화점" + " 8층 이벤트홀에서... 구두 브랜드 조이제화의"
				+ " 홍보를 위한 팬사인회에 참석한다.";

		/*
		 * 1번.순방향으로 공백문자의 index번호를 출력하시오. 5, 11,......87, : hint : indexOf을 사용한다.
		 */
		int idx = -1;
		int len=str.length();
		do {
			idx = str.indexOf(' ', idx + 1);
			if (idx >= 0)
				System.out.print(idx + ", ");
		} while (idx >= 0);
		System.out.println();
		
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == ' ')
				System.out.print(i + ", ");
		}
		System.out.println();
		
		/*
		 * 2번.역방향으로 공백문자의 index번호를 출력하시오. 87, 81, 78,....5, : hint : lastIndexOf을 사용한다.
		 */
		idx = len;//str.length;
		do {
			idx = str.lastIndexOf(' ',idx-1);
			if(idx>=0) System.out.print(idx+", ");
		}while(idx>=0);
		System.out.println();
		
		for (int i = len-1; i >=0; i--) {
			if (str.charAt(i) == ' ')
				System.out.print(i + ", ");
		}
		System.out.println();
		
		/* 3번.빈칸을 '_' 출력하시오. hint:charAt */
		for (int i = 0; i < len ; i++) {
			char c = str.charAt(i);
			if(c!= ' ') System.out.print(c);
			else System.out.print('_');
		}
		
		System.out.println();	
		System.out.print(str.replace(' ', '_'));		
		System.out.println();
		/* 4번 첫단어 출력하기 : substring, indexOf */
		int a= str.indexOf(' ');
		String str2 = str.substring(0,a);
		System.out.println("str2 : "+str2);

		/* 5번 마지막단어 출력하기 : substring, lastIndexOf */
		int b= str.lastIndexOf(' ')+1;
		String str3= str.substring(b,len);
		System.out.println("str3 : "+str3);
	}
}




Wrapper




package ch11;

import java.util.Vector;

public class WrapperEx1 {
	public static void main(String[] args) {
		// 자바의 기본형 Data Type(B가지 -> 객체화)
		int a = 10;
		// Auto Boxing
		Integer it1 = a;// 원래 변환 안됨.
		// Auto UnBoxing
		int b = it1;
		Vector v = new Vector();
		v.add(a);// Auto Boxing
		v.add(it1);
		int c = (Integer) v.get(0);// Auto unBoxing

		Integer it2 = new Integer(10);
		Integer it3 = new Integer("20");
		Integer it4 = Integer.valueOf(10);
		Integer it5 = Integer.valueOf("20");
		int a1 = it4.intValue();
		a1 = it4;
		int b1 = it5.intValue();
		int c1 = a1 + b1;
		System.out.println(c1);
		int a2 = Integer.parseInt("30");
		System.out.println(Integer.MIN_VALUE);
		System.out.println(Integer.toBinaryString(a2));
	}
}
package ch11;

public class WrapperEx2 {
	public static void main(String[] args) {
		char c1 = 'J';
		System.out.println(c1);
		if(Character.isLetter(c1))
			System.out.println(" : 문자입니다.");
		
		char c2 = 'a';//대문자인지 소문자인지 판단
		if(Character.isUpperCase(c2) )
			System.out.println(" : 대문자.");
	
		char c3 = '2';
		if(Character.isDigit(c3))
			System.out.println(" : 숫자입니다.");
		
		char c4 = ' ';
		if(Character.isWhitespace(c4))
			System.out.println(" : 공백입니다.");
		
		Long l;
		Boolean b;
		Short s;
		Double d;
		Float f;
		Byte bt;
		Integer i;
		Character c;
	}
}

0개의 댓글