너무 자주 scan.nextLine()을 호출하는 것처럼 보입니다. scan.nextLine()을 호출 할 때마다 스캐너는 현재 행을지나갑니다. 파일에 100 개의 행이 있다고 가정하고 각 행마다 한 번 호출되도록 test = scan.nextLine().split("-");
을 while 루프의 끝 (그러나 여전히 루프 내부)으로 이동합니다.
편집 ...
솔루션 제안
: abcxyz
abcxyz 형태의 파일을 감안할 때 ,
(총 100 회)
사용이 코드 :
try{
File file = new File("testwrite.txt");
Scanner scan = new Scanner(file);
String[] test = scan.nextLine().split("-");
while(r<100){
while(c<6){
data[r][c] = test[c];
c++;
}
r++;
c = 0 ;
test = scan.nextLine().split("-");
}
System.out.println(data[1][5]);
}catch(Exception e){
System.out.println("Error: " + e.getMessage());
}
그런 다음 데이터 [행] [색인]을 사용하여 데이터에 액세스하십시오. 제외하고, 또는 다른 문자 - 나는 탭 (\의 t)에 분할하고 내 인스턴스에서, 다시
BufferedReader reader = new BufferedReader(new FileReader(path));
int lineNum = 0; //Use this to skip past a column header, remove if you don't have one
String readLine;
while ((readLine = reader.readLine()) != null) { //read until end of stream
if (lineNum == 0) {
lineNum++; //increment our line number so we start with our content at line 1.
continue;
}
String[] nextLine = readLine.split("\t");
for (int x = 0; x < nextLine.length; x++) {
nextLine[x] = nextLine[x].replace("\'", ""); //an example of using the line to do processing.
...additional file processing logic here...
}
}
하지만 당신은 그냥 간단하게에 분할 할 수 있습니다 : I 탭 분할
System.out.println (data [1] [5]) - 테스트 용입니다. –
'for'-loop처럼 보이는 것이 더 적합 할 것입니다. – arshajii
우선 첫째 줄 8, test = scan.nextLine(). split ("-"); Test는 문자열 배열이므로 인덱스를 지정해야합니다. – Waleed