2014-11-13 3 views
-2

처리 할 파일을 입력 한 후 명령 줄은 다음 줄로 점프하지만 원하는 배열을 인쇄하는 것과는 달리 공백으로 남습니다. getInputScanner() 메서드를 호출하여 파일에 액세스하는 스캐너를 생성하고, 사용자가 파일을 입력 한 후 명령 줄이 텍스트를 처리하는 것과 반대로 다음 줄로 건너 뛰고 싶습니다. 어떤 아이디어? 당신이 input.nextLine()를 호출 스캐너 발전하지 않을 이후공백을 반환하는 명령 줄

import java.util.*; 
import java.awt.*; 
import java.io.*; 


public class Test { 
    public static void main(String [] args) { 

     Scanner console = new Scanner(System.in); 

     Scanner input = getInputScanner(console); 

     System.out.println("{" + nameArr(input) + "}"); 

    } 

    public static Scanner getInputScanner(Scanner console) { 

     System.out.print("Please enter a file to process: "); 
     File file = new File(console.next()); 
     while (!file.exists()) { 
     System.out.print("\nFile does not exists.\nPlease enter a new file name: "); 
     file = new File(console.next()); 
     } 
     try { 
     Scanner fileScanner = new Scanner(file); 
     return fileScanner; 
     } catch (FileNotFoundException e) { 
     System.out.println("File not found"); 
     } 
     return null; 
    } 

    public static String [] nameArr(Scanner input) { 

     int count = 0; 
     while (input.hasNextLine()) { 
     count++; 
     } 

     String [] nameArray = new String[count]; 

     while (input.hasNextLine()) { 

     String line = input.nextLine(); 

     for (int i = 0; i < nameArray.length; i++) { 
      nameArray[i] = lineProcess(input.nextLine()); 
     } 
     } 
     return nameArray; 
    } 

    public static String lineProcess(String line) { 
     Scanner lineScan = new Scanner(line); 

     String line2 = lineScan.nextLine(); 

     String lineString[] = line2.split(" "); 

     String name = lineString[0]; 

     return name; 

    } 


} 

답변

0

당신은했습니다 무한 loop :

while (input.hasNextLine()) { 
    count++; 
} 

내가 잘못된 일을 해요 코드의 다른 부분을 참조, java.util.List를 사용해보십시오

import java.util.*; 
import java.io.*; 

public class Test { 

    public static void main(String [] args) { 

     Scanner console = new Scanner(System.in); 

     Scanner input = getInputScanner(console); 

     List<String> nameList = nameArr(input); 
     for(String name : nameList){ 
      System.out.println("{ "+ name + " } "); 
     } 

    } 

    public static Scanner getInputScanner(Scanner console) { 

     System.out.print("Please enter a file to process: "); 
     File file = new File(console.next()); 
     while (!file.exists()) { 
      System.out.print("\nFile does not exists.\nPlease enter a new file name: "); 
      file = new File(console.next()); 
     } 
     try { 
      Scanner fileScanner = new Scanner(file); 
      return fileScanner; 
     } catch (FileNotFoundException e) { 
      System.out.println("File not found"); 
     } 
     return null; 
    } 

    public static List<String> nameArr(Scanner input) { 

     List<String> nameList = new ArrayList<String>(); 

     while (input.hasNextLine()) { 
      nameList.add(lineProcess(input.nextLine())); 
     } 
     return nameList; 
    } 

    public static String lineProcess(String line) { 

     return line.split(" ")[0]; 

    } 
} 
+0

어떻게 그렇게 : 대신 array의 당신의 처리 된 파일의 라인에서 names를 수집? input.hasNextLine()은 getInputScanner()에 입력 된 전체 파일을 계산하지 않고 count는 파일의 행 수와 같습니까? – LeSexyNinja

+0

'nextLine()'을 호출하는 스캐너를 진행시키지 않으면,'hasNextLine()'메소드는 항상'true'를 리턴합니다. – albciff

+0

@LeSexyNinja 내 대답을 편집하여 가능한 대체 방법을 제안합니다. – albciff

관련 문제