2012-02-28 7 views
0

UPDATE :분할 문자열 []

출력 될 sould :

chapter: Chapter 01 
title: one 
code: 111111 

chapter: Chapter 02 
title: two 
code: 222222 

chapter: Chapter 03 
title: three 
code: 333333 

chapter: Chapter 04 
title: four 
code: 444444 

chapter: Chapter 05 
title: five 
code: 555555 

는 I 문자열 []를 분할하는 코드를 갖는다. 문제가 무엇인지 잘 모르겠습니다. 아무도 나 좀 도와 줄래? 단지 내가 결과에서보고 싶은 그래서 더미 String[]

for (int j = 0; j < myArray.length; j++) 
{ 
String[] songs = myArray[j].split("\\<<<"); 

System.out.println("chapter: " + songs[0]); 
System.out.println("title: " + songs[1]); 
System.out.println("code: " + songs[2]); 
} 

을 가지고 단순화를 위해

String[] myArray = "Chapter 01<<<one<<<111111:::Chapter 02<<<two<<<222222:::Chapter 03<<<three<<<33333:::Chapter 04<<<four<<<4444:::Chapter 05<<<five<<<5555:::" 

은 다음과 같습니다

chapter: Chapter 01 
title: one 
code: 111111 
............so on and so forth........ 
+3

출력을 원하는 * 것으로 지정하지만 실제로 출력되는 내용은 무엇입니까? –

+0

출력을 게시하십시오. – joshua

+0

왜 split 대신 StringTokenizer를 사용할 수 없습니까? 그것은 당신을 도울 세 가지 생성자가 있습니다. – developer

답변

3
public void splitString(){ 
    String[] myArray = "Chapter 01<<<one<<<111111:::Chapter 02<<<two<<<222222:::Chapter 03<<<three<<<33333:::Chapter 04<<<four<<<4444:::Chapter 05<<<five<<<5555:::".split(":::"); 
    for (int j = 0; j < myArray.length; j++) 
    { 
     String[] songs = myArray[j].split("<<<"); 

     System.out.println("chapter: " + songs[0]); 
     System.out.println("title: " + songs[1]); 
     System.out.println("code: " + songs[2]); 
    } 
} 
+0

@ paislee. 나는 이클립스에서 그 방법을 썼다, 그것을 컴파일하고 그것을 실행합니다. 나는 결과물을 보았고 그 질문에서 기대했던 것과 그 이후에 여기에 게시 된 것입니다. 당신은주의 깊게 봐야합니다. – JProgrammer

2

이 도움을합니까 :

public class Tmp { 

    public static void main (String[] args) { 

    String myArray = 
     "Chapter 01<<<one<<<111111:::Chapter 02<<<two<<<222222:::Chapter 03<<<three<<<33333"; 

    String[] chapters = myArray.split (":::"); 
    for (int i=0; i < chapters.length; i++) { 
     System.out.println ("chapters[" + i + "]: " + chapters[i] + "..."); 

     String[] sections = chapters[i].split ("<<<"); 
     for (int j=0; j < sections.length; j++) 
     System.out.println (" sections[" + j + "]: " + sections[j] + "..."); 
    } 
    } 

} 

java Tmp 
chapters[0]: Chapter 01<<<one<<<111111... 
    sections[0]: Chapter 01... 
    sections[1]: one... 
    sections[2]: 111111... 
chapter[s1]: Chapter 02<<<two<<<222222... 
    sections[0]: Chapter 02... 
    sections[1]: two... 
    sections[2]: 222222... 
chapters[2]: Chapter 03<<<three<<<33333... 
    sections[0]: Chapter 03... 
    sections[1]: three... 
    sections[2]: 33333... 
+0

+1 출력을 살펴보면, 정확하게 질문하지 않은 질문에 대한 해결책처럼 보입니다. :) – Jayan

+0

출력에서'[] '가 누락되었다고 말하는 겁니까? 왜 내가 정확히 묻지 않은거야? – issoa

1
import java.util.StringTokenizer; 

public class Stringss { 

public static void main(String[] args){ 
    String myString = "Chapter 01<<<one<<<111111:::" + 
      "Chapter 02<<<two<<<222222:::" + 
      "Chapter 03<<<three<<<33333:::" + 
      "Chapter 04<<<four<<<4444:::" + 
      "Chapter 05<<<five<<<5555:::"; 

    StringTokenizer st = new StringTokenizer(myString,":::"); 
    String[] songs; 
    while (st.hasMoreElements()) { 
     String token = st.nextToken(); 
     songs = token.split("\\<<<"); 

     System.out.println("chapter: " + songs[0]); 
     System.out.println("title: " + songs[1]); 
     System.out.println("code: " + songs[2]); 

     } 

} 
} 
0

@Nirmal et all의 의견을 참조하십시오.

여러 번 사용하십시오. 먼저 라인/섹션 마커로 :::를 사용합니다. "< < <"노래 항목 구분 기호를 사용하십시오. 이것이 @Paul의 대답입니다.이 대답을 쓰면 그 단계가 명확 해집니다.