CLASS/JAVA
#12 / remind2
hingu
2024. 5. 17. 09:06
/* 1. {1,3,6,9,10}의 원시배열 데이터 중 홀수 값을 다음과 같이 모두 변경합니다. (LinkedList 사용) 결과 : {2,4,6,10,10} 으로 변경 LinkedList는 set이 더 빠름 */ public class exam12 { public static void main(String[] args) { Integer arr[] = {1,3,6,9,10}; List<Integer> arr_l = new LinkedList<Integer>(Arrays.asList(arr)); int idx=0; for(Integer a : arr_l) { //forEeach if(a%2 == 1) { arr_l.set(idx,a+1); }else { arr_l.set(idx,a); } idx++; } System.out.println(arr_l); } }
/*2. 해당 데이터 이름 입력 -> 번호변경 프로세서 제작 고객명 불일치시 "해당 고객은 검색되지 않습니다" 고객명 일치시 번호 입력 후 "정상적으로 변경 완료되었습니다." */ class ex13_box { String member[][] = { {"홍길동","SKT","45","hong@nate.com","01012234556"}, {"이순신","LGT","55","lee@gmail.com","01022309544"}, {"강감찬","KT","25","kang@gmail.com","01036601688"} }; Scanner sc = new Scanner(System.in); LinkedList<LinkedList<String>> data2 = new LinkedList<LinkedList<String>>(); public ex13_box() { System.out.println("검색할 고객명을 입력하세요 : "); String name = this.sc.nextLine(); int count = 0; int f,ff; for(f=0; f<member.length; f++) { LinkedList<String> data = new LinkedList<String>(); int node = Arrays.asList(member[f][0]).indexOf(name); for(ff=0; ff<member[0].length; ff++) { data.add(member[f][ff]); } this.data2.add(data); if(node != -1) { System.out.println("변경할 연락처를 입력하세요 : "); String number = this.sc.nextLine(); data.set(4, number); count++; } } if(count == 0) { System.out.println("해당 고객은 검색되지 않습니다"); }else { System.out.println("정상적으로 변경 완료되었습니다."); System.out.println(data2); } this.sc.close(); } }
/*4. getter,setter로 입력받은 3개항목값 2차배열로 던지기 [[132, 456, 789], [456, 78, 54], [85454, 654654, 7878]] */
/*5. Scanner로 댓글입력하면 class배열에 담기 (단, 현재 시간,분 함께 출력) "내용을 입력해 주세요 : " 결과 : [너무 어려워요ㅠ 24-05-17 04:37:03] */
/*6. {"1000","2000","","400","","600"} 를 예외처리 적용시켜 정상적으로 출력되는 코드 작성 해당 배열을 문자배열->숫자배열로 출력하되 ""는 0으로 처리 결과 : [1000, 2000, 0, 400, 0, 600] */ public class exam17 { public static void main(String[] args) { String arr[] = {"1000","2000","","400","","600"}; ArrayList<Integer> result = new ArrayList<Integer>(); int f; for(f=0; f<arr.length; f++) { try { Integer aa = Integer.parseInt(arr[f]); result.add(aa); }catch(java.lang.NumberFormatException e2) { result.add(0); } } System.out.println(result); } }
https://dev-eunse.tistory.com/69 예외처리 참고.. 너무어렵따..
/*8. 다음 아이디 생성시 예외처리를 이용하여 올바른 값이 배열(Object)에 저장 되도록 합니다. 아이디를 입력하세요 : (이건 문자) 휴대폰 뒷자리를 입력하세요(4자리) : (이건 숫자) 첫째 숫자가 0인지 아닌지 잘 구분할것 [결과] 1. 한글을 사용시 "아이디는 영문자 + 숫자만 사용 가능합니다" //정규식 코드로 확인 2. 아무것도 입력을 하지 않았을 경우 "아이디를 입력하셔야 합니다" -> "아이디를 입력하세요" 3. 휴대폰 뒷자리가 숫자 외에 문자가 입력될 경우 배열에 0으로 입력되도록 합니다. */ package exam; import java.util.ArrayList; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class exam18 { public static void main(String[] args) { new ex18_box(); } } class ex18_box { Scanner sc = new Scanner(System.in); ArrayList<Object> result_info = new ArrayList<Object>(); public ex18_box() { getInfo(); } public void getInfo() { System.out.println("아이디를 입력하세요 : "); String userid = sc.nextLine().intern(); //12가나다 입력 String s_id = userid.replaceAll("[a-zA-Z0-9]", "0"); //00가나다(영어,숫자만 0으로 바뀜) try { //에러확인 Integer i_id = Integer.parseInt(s_id); //가나다때문에 에러가 발생 //제대로 입력한 경우 this.result_info.add(userid); getNumber(); }catch(java.lang.NumberFormatException e) { //나머진다 0 예외 System.out.println(e); if(s_id == "") { System.out.println("아이디를 입력하셔야 합니다"); }else { System.out.println("아이디는 영문자 + 숫자만 사용 가능합니다"); } getInfo(); }catch(java.util.InputMismatchException e2) { this.result_info.add(0); }finally { System.out.println(this.result_info); sc.close(); } } public void getNumber() { System.out.println("휴대폰 번호 뒷자리를 입력하세요 : "); Integer pnumber = sc.nextInt(); String s_pnumber = String.valueOf(pnumber); if(String.valueOf(pnumber).length() != 4) { if(String.valueOf(pnumber).length() < 4) { int f; String lt0 = ""; for(f=0; f< 4-String.valueOf(pnumber).length(); f++) { lt0 += "0"; } this.result_info.add(lt0 + s_pnumber); }else { System.out.println("4자리로 입력해주세요"); getNumber(); } }else { this.result_info.add(s_pnumber); } } }
⚡ T
2.
❗ 2차배열에선 set을 바로 못때림(1차배열 로드 후 적용)
❗ 1차배열 값을 변경함 -> 자동으로 2차배열로 값도 변경됨(어차피 그룹화 되어있음)
/*2-t. 해당 데이터 이름 입력 -> 번호변경 프로세서 제작 고객명 불일치시 "해당 고객은 검색되지 않습니다" 고객명 일치시 번호 입력 후 "정상적으로 변경 완료되었습니다." */ class exam13_t_box { String member[][] = null; //원시배열 전역변수 List<List<String>> all = new LinkedList<List<String>>(); //전체 고객 데이터 담길 곳 Scanner sc = new Scanner(System.in); public exam13_t_box() { this.member = new String[][]{ {"홍길동","SKT","45","hong@nate.com","01012234556"}, {"이순신","LGT","55","lee@gmail.com","01022309544"}, {"강감찬","KT","25","kang@gmail.com","01036601688"} }; this.search(); //은닉화로 해당 메소드를 호출 }; //고객 검색 및 데이터 변경 public void search() { int w = 0; do { List<String> result = this.data(this.member[w]); this.all.add(result); w++; }while(w<this.member.length); //검색 System.out.println("고객명을 입력하세요 : "); String search_name = this.sc.nextLine(); boolean check = false; //검색 대상이 있을 경우 : true,없으면 false 유지 int idx=0; //배열 노드번호 for(List<String> info : this.all ) { //foreach로 2차 -> 1차 형태로 분리해서 출력 if(info.get(0).equals(search_name)) { check = true; //검색 결과가 있을 경우 true로 변환 break; } idx++; } if(check == false) { System.out.println("해당 고객은 검색되지 않습니다."); }else { //검색된 사용자가 있을경우 System.out.println("변경할 연락처를 입력하세요 : "); String tel = this.sc.nextLine(); //2차배열에선 set을 바로 못때림(1차배열 로드 후 적용) //1차배열 값을 변경함 -> 자동으로 2차배열로 값도 변경됨(어차피 그룹화 되어있음) List<String> redata = this.all.get(idx); //해당배열 전부가져옴(언박싱) redata.set(4, tel); } System.out.println(this.all); //최종 2차배열 변경 완료 this.sc.close(); }; //1차배열을 제작하는 메소드 public List<String> data(String m[]){ List<String> db = new ArrayList<String>(Arrays.asList(m)); return db; } }
4./*4-t. getter,setter로 입력받은 3개항목값 2차배열로 던지기 [[132, 456, 789], [456, 78, 54], [85454, 654654, 7878]] */ public class exam15_2 { public static void main(String[] args) { new exam15_2_box().abc(); } } class exam15_2_box { Scanner sc = new Scanner(System.in); List<LinkedList<String>> arr2 = new LinkedList<LinkedList<String>>(); public void abc() { exdto dto15 = new exdto(); System.out.println("상품명을 입력하세요 : "); String sname = this.sc.nextLine(); dto15.setName(sname); System.out.println("상품 가격을 입력하세요 : "); String sprice = this.sc.nextLine(); dto15.setPrice(sprice); System.out.println("상품 수량을 입력하세요 : "); String scount = this.sc.nextLine(); dto15.setCount(scount); arr2.add(dto15.made1Array()); System.out.println(arr2); this.abc(); } }
🔽package exam; import java.util.LinkedList; //dto - exam15.java 와 연계 public class exdto { String name,price,count; LinkedList<String> arr = new LinkedList<String>(); public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getCount() { return count; } public void setCount(String count) { this.count = count; } public LinkedList<String> made1Array() { this.arr.add(this.getName()); this.arr.add(this.getPrice()); this.arr.add(this.getCount()); return arr; } }
5./*5-t. Scanner로 댓글입력하면 class배열에 담기 (단, 현재 시간,분 함께 출력) "내용을 입력해 주세요 : " 결과 : [너무 어려워요ㅠ 24-05-17 04:37:03] */ public class exam16_t { public static void main(String[] args) { new exam16t_box(); } } class exam16t_box { Scanner sc = null; private ArrayList<String> all = new ArrayList<String>(); private String msg = ""; SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd hh:mm:ss"); Date today = null; //비속어 private LinkedList<String> munja = new LinkedList<String>(); public exam16t_box() { this.abc(); munja.add("신발"); } private void abc() { //은닉화 this.sc = new Scanner(System.in); System.out.println("댓글을 입력해주세요 : "); this.msg = this.sc.nextLine(); //날짜를 저정하는 현태 데이터일 경우 최종 마지막 입력사항 밑에 처리가 되어야 한다 this.today = new Date(); //엔터 쳤을때의 시간을 가져와야함!!!! this.all.add(this.msg); this.all.add(this.sdf.format(today)); System.out.println(this.all); //비속어는 어딘가 따로 클래스를 만들어서.. } }
6./*6-t. {"1000","2000","","400","","600"} 를 예외처리 적용시켜 정상적으로 출력되는 코드 작성 해당 배열을 문자배열->숫자배열로 출력하되 ""는 0으로 처리 결과 : [1000, 2000, 0, 400, 0, 600] */ package exam; import java.util.ArrayList; public class exam17_t { public static void main(String[] args) { new exam17t_box(); } } class exam17t_box{ private String[] data = null; //숫자처리 배열 private ArrayList<Integer> data2 = new ArrayList<Integer>(); public exam17t_box() { this.data = new String[]{"1000","2000","","400","","600"}; this.check(); } private void check() { int w=0; do { try { this.data2.add(Integer.valueOf(this.data[w])); }catch(Exception e) { System.out.println(e); try { //catch 안에 try작성해주는게 사씔은 정통.. this.data2.add(0); }catch(Exception e2) { System.out.println(e2); } } w++; }while(w<this.data.length); System.out.println(data2); } }