# 문제 아래의 입력파일을 읽고 실행 예와 같이 동작하려고 하는데 에러가 납니다. 해결방법을 알려주세요. ## 입력 파일 weather2.txt ``` 4.5 2.7 3.1 20.8 29.0 30.0 30.0 -1.5 12.6 19.9 31.5 ``` ## 실행 예 ``` Input file? weather2.txt 4.5 to 2.7, change = -1.8 2.7 to 3.1, change = 0.4 3.1 to 20.8, change = 17.7 20.8 to 29.0, change = 8.2 29.0 to 30.0, change = 1.0 30.0 to 30.0, change = 0.0 30.0 to -1.5, change = -31.5 -1.5 to 12.6, change = 14.1 12.6 to 19.9, change = 7.3 19.9 to 31.5, change = 11.6 ``` ## 작성 코드 ``` public class Weather extends ConsoleProgram { public void run() { String file = promptUserForFile("Input file? "); try { Scanner input = new Scanner(new File(file)); double weather1= input.nextDouble(); while(input.hasNext()) { double weather2= input.nextDouble(); double difference = weather2- weather1; println(weather1 + " to " + weather2 +", " + "change = " + difference); weather1 = weather2; } } catch (FileNotFoundException e) { println("Error reading file"+ e); } } } ``` ## 작성 코드 실행 결과 ``` Input file? weather2.txt 4.5 to 2.7, change = -1.7999999999999998 2.7 to 3.1, change = 0.3999999999999999 3.1 to 20.8, change = 17.7 20.8 to 29.0, change = 8.2 29.0 to 30.0, change = 1.0 30.0 to 30.0, change = 0.0 30.0 to -1.5, change = -31.5 -1.5 to 12.6, change = 14.1 12.6 to 19.9, change = 7.299999999999999 19.9 to 31.5, change = 11.600000000000001 differences:2,3c2,3 < 4.5·to·2.7,·change·=·-1.8 < 2.7·to·3.1,·change·=·0.4 > 4.5·to·2.7,·change·=·-1.7999999999999998 > 2.7·to·3.1,·change·=·0.3999999999999999 10,11c10,11 < 12.6·to·19.9,·change·=·7.3 < 19.9·to·31.5,·change·=·11.6 > 12.6·to·19.9,·change·=·7.299999999999999 > 19.9·to·31.5,·change·=·11.600000000000001 ``` ***세번째 조건은*** test #3: bogus.txt file input: bogus.txt: 16.2 23.2 19.2 abc 7.7 hi there! 22.9 TODO: buy pants 18.4 -1.6 14.6 :-) expected output: Input file? bogus.txt 16.2 to 23.2, change = 7.0 23.2 to 19.2, change = -4.0 19.2 to 7.7, change = -11.5 7.7 to 22.9, change = 15.2 22.9 to 18.4, change = -4.5 18.4 to -1.6, change = -20.0 -1.6 to 14.6, change = 16.2 your output:Input file? bogus.txt 16.2 to 23.2, change = 7.0 23.2 to 19.2, change = -4.0 threw NoSuchElementException: Near input line 2token ' abc' cannot be interpreted as type double differences:4,8c4,6 < 19.2·to·7.7,·change·=·-11.5 < 7.7·to·22.9,·change·=·15.2 < 22.9·to·18.4,·change·=·-4.5 < 18.4·to·-1.6,·change·=·-20.0 < -1.6·to·14.6,·change·=·16.2 > > threw·NoSuchElementException: > Near·input·line·2token·'·abc'·cannot·be·interpreted·as·type·double exception:NoSuchElementException: Near input line 2token ' abc' cannot be interpreted as type double 두번째 조건은 결과값을 반올림만 해주면 될거같아 %.2f 를 통해 해결해보려했는데 오류가 발생합니다. 세번째 조건은 실수사이에 문자가 들어가 있어 오류가 발생하는것 까지는 이해했습니다. (If there are any non-numeric tokens of input in the file, your program should skip over them and ignore them. You may assume that the user types the name of a file that exists and is readable.) 위의 제시문을 읽어보면 숫자가 아닌 문자가 있을경우 무시해버리고 진행해야 한다고 하는데 그 방법을 잘 모르겠습니다 ..if문을 사용하면 될까요? 어쩌다 보니 원어 수업을 듣게되었는데 바로바로 이해하지 못하고 넘어가게 되버리는 경우가 많아지니 기본적인 질문이 많아지네요.. 조언 부탁드립니다!
## 소수점 반올림 출력 printf() 메소드와 형식을 적용해보세요. ``` System.out.printf("%.1f\n", -1.7999999999999998); // -1.8 ``` ## PS. 한번에 한 질문씩 부탁드려도 될까요? :)
## non-double 무시하기 `try-catch`와 `continue`를 사용하여 에러 발생시 다음반복으로 넘겨보세요 :) (약간의 코드구조를 변경) ``` public class Weather extends ConsoleProgram { public void run() { String file = promptUserForFile("Input file? "); Scanner input = new Scanner(new File(file)); double weather1= input.nextDouble(); while(input.hasNext()) { try { double weather2 = input.nextDouble(); } catch (FileNotFoundException e1) { println("Error reading file"+ e1); } catch (NoSuchElementException e2) { continue; // 다음 반복으로 이동 } double difference = weather2 - weather1; println(weather1 + " to " + weather2 +", " + "change = " + difference); weather1 = weather2; } } } ```
해결이 안됩니다.
## 추가 답변 아래 코드는 로컬 환경에서 동작하는 코드이니, 웹에 동작하는 코드와는 다를 수 있습니다. ## 작성 코드 ``` import java.util.InputMismatchException; import java.util.Scanner; public class WeatherTest { public static void main(String[] args) { Scanner input = new Scanner("16.2 23.2 19.2 abc 7.7 hi there! 22.9 TODO: buy pants 18.4 -1.6 14.6 :-)"); double weather1 = input.nextDouble(); double weather2; while (input.hasNext()) { try { weather2 = input.nextDouble(); } catch (InputMismatchException e) { System.out.println("Error: " + e); input.next(); continue; } double difference = weather2 - weather1; System.out.println(weather1 + " to " + weather2 + ", " + "change = " + difference); weather1 = weather2; } } } ``` ## 실행 결과 ``` 16.2 to 23.2, change = 7.0 23.2 to 19.2, change = -4.0 Error: java.util.InputMismatchException 19.2 to 7.7, change = -11.5 Error: java.util.InputMismatchException Error: java.util.InputMismatchException 7.7 to 22.9, change = 15.2 Error: java.util.InputMismatchException Error: java.util.InputMismatchException Error: java.util.InputMismatchException 22.9 to 18.4, change = -4.5 18.4 to -1.6, change = -20.0 -1.6 to 14.6, change = 16.2 Error: java.util.InputMismatchException ```
참고해서 해결해보겠습니다 감사합니다!