2014-03-04 3 views
0

문제가 있습니다. 텍스트를 분리하려고합니다. (예 : city # country # capitalcityname : 예 : Krakow # Poland # Warsaw) # 뒤에 어떻게 텍스트를 나눌 수 있습니까? 텍스트를 테이블에 넣고 싶습니다. 좋은 생각입니까? 텍스트를 뱉어 내고 테이블에 추가하기

public class WordSearch { 
ArrayList<String> list= new ArrayList<>(); 
    public WordSearch(){ 

     try { 

      File file = new File ("songs.txt"); 
      BufferedReader br = new BufferedReader(new FileReader(file)); 
      String line = null; 
      while((line = br.readLine()) != null) { 
      list.add(line); 
//   System.out.println(line); 
      } 
     } catch (IOException e) { 
      System.out.println(e.getLocalizedMessage()); 
     } 
    } 
     public ArrayList<String> getList() { 
     return list; 
    } 
} 

는 더 명확 나는 테이블 enter image description here

+0

StringTokenizers에 모습 – Coderchu

+2

@d_ominic 유무 : 왜 너는 심지어 오라클은 당신이 사용하지 않을 것을 권장합니까? 거의 항상'String # split (String regex)'를 대신 사용합니다. –

+1

원래의 포스터에는 "테이블"이 무엇을 의미합니까? –

답변

2

당신은 split() 방법을 사용할 수 있습니다 시각적 예를 추가 할 수 있도록합니다.

String[] list = text.split("#"); 
4

당신은 split() 방법을 사용할 수 있습니다 :

String s="Krakow#Poland#Warsaw"; 
    String[] parts = s.split("#"); 

출력 :

for (String i: parts) 
    System.out.println(i); 

==>

run: 

Krakow 
Poland 
Warsaw 

BUILD SUCCESSFUL (total time: 0 seconds) 
관련 문제