강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của hana78786
hana78786

câu hỏi đã được viết

Sân chơi JAVA mà bạn học được khi tạo

데이터를 불러오는걸 계속 실패합니다

Viết

·

233

0

마지막 강의를 해봤는데 저장은 정상적으로 되는데 데이터 불러오기는 실패하네요
혼자서는 어떤부분에서 문제가 있는지 모르겠습니다
 
 
 
package calendar;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

public class cla_pro_1 {
	private final static int[] arr = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	private final static int[] larr = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
private static final String savefile="cal_p.dat";
	
	public boolean isLeepYear(int year) {
		if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
			return true;
		else
			return false;
	}
	public int arr(int year, int mon) {
		if (isLeepYear(year)) {
			return larr[mon];
		} else {
			return arr[mon];
		}
	}


	
	

	public void cal_print(int year, int mon) {
	

		System.out.printf("    %3d년  %3d월\n",year,mon);
		System.out.println(" su mo tu we th fr sa");
		System.out.println("----------------------");
		
		if(mon<=2) {mon=mon+12;year=year-1;}
		int y=year%100;
		int c=(year-y)/100;
		int z=((21*c/4)+(5*y/4)+(26*(mon+1)/10))%7;
		int maxday=arr(year,mon);
		for(int b=0;b<z;b++) {
			System.out.print("   ");
		}
		for(int a=1;a<=maxday;a++) {
			System.out.printf("%3d",a);
			if ((a+z)%7==0) {
				System.out.println();
			
		}}System.out.println();
		
	}
	
		
		private static HashMap<Date,String>hm;
		public cla_pro_1() {
			hm=new HashMap<Date,String>();
			File f=new File(savefile);
			if(!f.exists())
				return;
try {
	Scanner s=new Scanner(f);
	while(s.hasNext()){
		String line=s.nextLine();
		String[] words=line.split(",");
		String date=words[0];
		String plan=words[1];
		PlanItem p=new PlanItem(date, plan);
	}
} catch (FileNotFoundException e) {

	e.printStackTrace();
}
		}
	
	public void plan(String strdate, String plan) throws ParseException {
		PlanItem p=new PlanItem(strdate,plan);
		SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");
		Date date=formatter.parse(strdate);
			hm.put(date, plan);
				System.out.println("날짜:"+strdate+" 일정:"+plan);
			File f=new File(savefile);
			
			String item=p.savestring();
			try {
				FileWriter fw=new FileWriter(f, true);
				fw.write(item);
				fw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	public String searchPlan(String strdate) throws ParseException {
		Date date=new SimpleDateFormat("yyyy-MM-dd").parse(strdate);
		String plan=hm.get(date);
		return plan;
		
	}
	
		
		
	}


 
 
package calendar;

import java.text.ParseException;
import java.util.Date;
import java.util.Scanner;

public class cal_pro_2 {

	
	public static void printmenu() {
		System.out.println("+-------------+");
		System.out.println("| 1. 일정등록");
		System.out.println("| 2. 일정검색");
		System.out.println("| 3. 달력보기");
		System.out.println("| h. 도움말  q.종료");
		System.out.println("+-------------+");
	}
			
		
		
	
	public void runprompt() throws ParseException {
		cal_pro_2 cp2=new cal_pro_2();
		cla_pro_1 cp1=new cla_pro_1();
		printmenu();
		boolean loop=true;
		while(loop) {
			System.out.println("명령(1,2,3,h,q)");
			System.out.print(">");
			Scanner sc=new Scanner(System.in);
			String cmd=sc.next();
			switch(cmd) {
			case "1": cp2.cal_1(sc, cp1);break;
			case "2": cp2.cal_2(sc, cp1);break;
			case "3": cp2.cal_print();break;
			case "h": cp2.help(sc);break;
			case "q": System.out.println("종료합니다");loop=false;
			}}}
	
	private void cal_2(Scanner sc,cla_pro_1 cp1) throws ParseException {
		System.out.println("[일정검색]");
		System.out.println("날짜를 입력하세요 yyyy-MM-dd");
		System.out.print(">");
		String strdate=sc.next();
		String plan=cp1.searchPlan(strdate);
		if(plan==null) {System.out.println("일정이 없습니다");
			}
		else {System.out.println(plan);}

		cp1.searchPlan(strdate);
	

		
	}




	private void help(Scanner sc) {
		System.out.println("도움말");
		System.out.println("번호를 입력하고 날짜를 바르게 입력해주세요");
		return;
		
		
	}




	private void cal_1(Scanner sc,cla_pro_1 cp1) throws ParseException {
		System.out.println("[일정입력]");
		System.out.println("날짜를 입력하세요 yyyy-MM-dd");
		System.out.print(">");
		String strdate=sc.next();
		String plan="";
		sc.nextLine();
		System.out.println("일정을 입력해주세요");
		System.out.print(">");
		plan=sc.nextLine();
		cp1.plan(strdate, plan);
		
		
		
		
	}




	public void cal_print() {
		while(true) {
			System.out.println("년도를 입력하세요");
			System.out.print(">");
			Scanner s=new Scanner(System.in);
			int year=s.nextInt();
			if(year==-1) {System.out.println("종료합니다");break;}
			System.out.println("월을 입력하세요");
			System.out.print(">");
			int mon=s.nextInt();
			if(mon==-1) {System.out.println("종료합니다");break;}
			else if(mon<1||mon>12) {System.out.println("잘못된입력입니다");continue;}
			
			cla_pro_1 cp=new cla_pro_1();
			cp.cal_print(year, mon);
			return;
	
		}
	}
public static void main(String[] args) throws ParseException {
	cal_pro_2 cp2=new cal_pro_2();
	cp2.runprompt();
}
}
 
package calendar;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class PlanItem {
		public Date planDate;
		public String detail;
		
		public static Date getDatefromString(String strdate) {
			Date date = null;
			try {
				date=new SimpleDateFormat("yyyy-MM-dd").parse(strdate);
			} catch (ParseException e) {
				// 이부분은 위에 simpleDateformat이 바르지 않게 입력됏을때의 오류값 출력이다
				e.printStackTrace();
			}
			return date;
		}
		
		public PlanItem(String date, String detail) {
			this.planDate=getDatefromString(date);
			
			this.detail=detail;
		}

		public Date getdate() {
			return planDate;
		}

		public String savestring() {
			SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");
			String sdate=formatter.format(planDate);		
			return sdate.toString()+","+detail+"\n";
		}
}
 
 
 
 
 
java

Câu trả lời

Câu hỏi này đang chờ câu trả lời
Hãy là người đầu tiên trả lời!
Hình ảnh hồ sơ của hana78786
hana78786

câu hỏi đã được viết

Đặt câu hỏi