728x90



728x90
반응형
728x90

 

한바퀴 도는 메소드 먼저 하나 만들었다

한바퀴 돌기 좋은 코드 로직인 것 같다

이제 예외(Exception)를 강제 발생시켜 예외처리 연습을 해보려한다

 

IOException 임포트 후에 예외 강제 발생 코드인 throw를 넣고 런시키니 뜬 에러메세지

Unhandled exception type IOException 

Unreachable code

 

Unhandled exception type IOException는  IOException을 예외처리 못했다는 의미이고

Unreachable code는 예외가 발생되면 발생된 곳 이후 코드들은 전부 작동하지 않기에 닿지않는 코드라고 에러가 뜨는 것이다. println 결력도 call_3메소드는 출력되지 않고 2까지만 되고 있다

이제 발생한 예외를 try catch문을 써서 직접 처리 해본다

 

 

코드 call_1 off까지 끝까지 잘 작동한다.

아래 빨간줄은 에러처럼 보이지만 catch문 안에 e.printStackTrace();에서 .printStackTrace()이 에러 정보를 보여주기 때문에 나오는 것이다. 에러메세지가 아니니 걱정하지 않아도 된다.

 

 

 .printStackTrace()지워보면 빨간줄이 전부 사라진다.

 

기능클래스의 메소드 call_3에서 직접 예외처리 했지만,

런파일에서 pe4.call_3를 try문 안에 넣고 해도 된다

728x90
반응형
728x90



728x90
반응형

'small steps > 운동 - 체력이 국력' 카테고리의 다른 글

22.02.24 운동기록  (0) 2022.02.25
22.02.22 운동기록 -2시간 운동 오버트레이닝일지도…  (0) 2022.02.23
22.02.20 운동기록  (0) 2022.02.20
22.02.19 운동기록  (0) 2022.02.20
22.02.17 운동기록  (0) 2022.02.17
728x90

 

String 안에 쓸 수 있는 많이 쓰는 메소드

1)charAt ()

2)concat()

3)equals()

4)substring()

5)replace()

 

 

public class practice_everyday01 {
	public static void main(String[] args) {	
		
		// String methods
		String str = "힘을 내라";
		
		// 1)charAt ()
		// charAt(int index):char
		char cheer = str.charAt(0);
		System.out.println(cheer); // 힘
		
		// 2) concat()
		// concat(String str):String
		// 원래 문자열 끝에 매개변수로 들어온 값을 이어붙인 문자열 반환
		String cstr = str.concat(" save it");
		System.out.println(cstr);
		
		
		// 3) equals()
		// equals(Object anObject):boolean
		System.out.println(str.equals(cstr));
		
		// 4)substring()
		// substring(int beginIndex):String
		// 해당 인덱스 넘버부터 문자열 시작. 
		// substring(int beginIndex, int endIndex):String
		// A to B 사이의 문자열 일부 반환
		
		System.out.println(str.substring(2));
		System.out.println(str.substring(0,3));
		
		
		// 5)5)replace()
		// replace(char oldChar, char newChar):String
		
		System.out.println(str.replace('힘','손'));
	}	
}
728x90
반응형
728x90
728x90
반응형
728x90



728x90
반응형
728x90

 

StringBuffer 안에 쓸 수 있는 많이 쓰는 메소드

append(),  insert(), delete(), reverse()

 

 

public class practice_everyday01 {
	public static void main(String[] args) {	
		
		// StringBuffer
		
		// 1) append()
		// 받아온 값을 뒤에 추가로 이어주는 메소드
		StringBuffer sb1 = new StringBuffer();
		sb1.append("a"); // str
		sb1.append('1'); // char
		sb1.append(2);   // int
		System.out.println(sb1); // 문자열 취급 더하기 a+1+2=a12
 		// a12
		
		// 2) insert()
		// 특정 인덱스번호에 문자열 삽입기능
		sb1.insert(1, "b");  	// 인덱스1 위치에 넣으면 원래 1에 있던 문자는 뒤로 밀려난다 
		sb1.insert(2, 'c');  	// 인덱스1 위치에 넣으면 원래 1에 있던 문자는 뒤로 밀려난다 
		System.out.println(sb1);// ""나 ''나 둘 다 작동
		//abc12
		
		// 3)delete 
		// delete(start, end-1)
		sb1.delete(3, 5);  		// end 숫자의 -1까지 삭제한다 1부터5라면 1부터4까지만 지운다
		System.out.println(sb1);
		// abc
		
		// 4)reverse
		sb1.reverse();  // 문자열을 거꾸로 돌려서 출력
		System.out.println(sb1);
		// cba
	}	
}
728x90
반응형
728x90

 

 

 

public class practice_everyday01 {
	public static void main(String[] args) {	
		
		int[] arr = new int[4];
		for(int i = 0; i<arr.length;i++) {
			System.out.println(arr[i] += (10+i));
		}
			
	}
}
728x90
반응형
728x90

 

오늘 코딩으로 

다량의 주문이나 DB관리 때 런파일에서 간단하게 수치만 바꿔서 관리할 수 있는 아주 편리한 기능을 배웠다!!

 

실행 클래스

public class Run {
	public static void main(String[] args) {	
		Book b1 = new Book();
//		b1.inform();
				
		Book b2 = new Book("책1","출판사1","저자1");
//		b2.inform();
				
		Book b3 = new Book("책2","출판사2","저자2",10000,0.3);
		b3.inform();
				
	}

}

 

 

기능 클래스

public class Book {
	
	private String title;
	private String publisher;
	private String author;
	private int price;
	private double discountRate;
	
	// 기본 생성자
	public Book() {
				
	}
	
	// 매개변수 3개인 생성자
	public Book(String title, String publisher, String author) {
		this.title = title;			// 값넣기까지 초기화 // 값을 대입시키는게 초기화
		this.publisher = publisher;
		this.author = author;
	}
	
	// 매개변수 5개인 생성자
	public Book(String title, String publisher, String author, 
				int price, double discountRate) {
		this.title = title;
		this.publisher = publisher;
		this.author = author;
//		this(title,publisher,author); 	// this() 생성자
		this.price = price;
		this.discountRate = discountRate;
		
	}
	
	public void inform() {
		System.out.printf(" title : %s%n publisher : %s%n author : %s%n price : %d%n 할인율 : %f",title,publisher,author,price,discountRate);
	}
	
}

 

728x90
반응형
728x90

 

println()

printf()

두가지로 추가 연습

 

728x90
반응형

+ Recent posts