txt 파일에서 Strings와 ints의 조합을 ArrayList로 읽으려고합니다. 문제가 ArrayList String 개체를 Integer로 변환하는 것과 관련이 있다는 것을 알고 있지만 해결 방법을 알지 못합니다. arraylist의 처음 두 인덱스를 인스턴스 변수 "name"및 "creationDate"로 초기화합니다. 프로그램의 목적은 재생 목록 이름, 생성 날짜 및 노래 제목, 아티스트 및 길이가있는 mp3 플레이어입니다. 도와주세요!!
public void readPlaylist(String path) {
String token = "";
ArrayList<String> temps = new ArrayList<String>();
try {
Scanner inputFile = new Scanner(new File(path)).useDelimiter("\n");
while (inputFile.hasNext()) {
token = inputFile.next();
temps.add(token);
}
inputFile.close();
} catch (java.io.FileNotFoundException ex) {
System.out.println("Could not open the input file");
}
this.name = (String) temps.get(0);
this.creationDate = (String) temps.get(1);
for (int i = 2; i < temps.size() -2; i+=3) {
String title = (String) temps.get(i);
String artist = (String) temps.get(i + 1);
int duration;
duration = parseInt((String) temps.get(i+2));
System.out.println(temps.get(i+2));
Song song = new Song(title,artist,duration);
list.add(song);
}
}
다음은 내가 읽는 txt 파일입니다.
KB Jams
09/01/2013
Repect
Aritha Franklin
182
Since I Fell for You
Jimmy Smith
256
Green Onions
Booker T. & the MG's
174
One Nation Under the Groove (Instrumental)
Parliament Funkadelic
347
I'd Rather Be With You
Bootsy Collins
294
Alpha and Omega
Israel & New Breed
351
Nobody Greater
VaShawn Mitchell
397
문자열에 공백과 같은 숫자 값 이외의 것이 포함 된 경우 NumberFormatException 오류가 발생합니다.
이 오류는 구문 분석하려는 내용을 알려줍니다.
print 문을 사용하여 공백이나 줄 바꿈을 볼 수없는 이유는 단지 공백이기 때문입니다.
숫자를 둘러싼 과도한 공백을 제거하기 위해 문자열을 다듬을 수 있습니다
parseInt(temps.get(i+2).trim())
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다