2013-11-26 1 views
0

작동하지 않음이 텍스트의 라인이 내 코드자바 분할 선이

//Numbers (Need errors on sort and numbers) 
if(line.contains("n")) 
{ 
    //split the line with space 
    String[] LineSplit = line.split(" "); 

    if(LineSplit[0].contains("n")) 
    { 
     //split the already split line with "n thename " 
     String[] LineSplit2 = line.split("n " + LineSplit[0] + " "); 

     String text = "var " + LineSplit[1] + "=" + LineSplit2[0] + ";"; 
     text = text.replace("\n", "").replace("\r", ""); 

     JAVASCRIPTTextToWrite += text; 
    } 

} 

입니다 출력이 있어야한다 번호 1

N

VAR 수 = 1;

하지만 출력

이다 VAR 참조 번호 = N = 1;

일부 문제는 해결 방법을 알려주십시오. 코드는 잘 보이지만 작동하지 않습니다 :(당신의 상태가

답변

0
  String line = "n number 1"; 
      String JAVASCRIPTTextToWrite=""; 
      if(line.contains("n")) 
      { 
       //split the line with space 
       String[] LineSplit = line.split(" "); 

       if(LineSplit[0].contains("n")) 
       { 
        //split the already split line with "n thename " 
        String[] LineSplit2 = line.split("n " + LineSplit[1] + " "); 
        System.out.println(LineSplit[1]); 
        System.out.println(LineSplit2[0]); 
        String text = "var " + LineSplit[1] + "=" + LineSplit2[1] + ";"; 
        text = text.replace("\n", "").replace("\r", ""); 

        JAVASCRIPTTextToWrite += text; 
        System.out.println(JAVASCRIPTTextToWrite); 
       } 

      } 
0
String number = "n number 1"; 
Sting[] temp = number.split(" "); 
StringBuilder sb = new StringBuilder("var "); 
sb.append(temp[1]); 
sb.append(temp[2]); 

이 작업을 수행

var number=1; 
+0

변경 행 3 : StringBuilder sb = new StrignBuilder ("var"); . "var"뒤에 필요한 여분의 공간 – TheLostMind

+0

관측 덕분에 –

0
String line = "n number 1"; 

    if(line.contains("n")) 
    { 
    //split the line with space 
    String[] LineSplit = line.split(" "); 
     if(LineSplit[0].contains("n")) { 
     //split the already split line with "n thename " 
     String LineSplit2 = line.substring(line.lastIndexOf(" ") + 1 , line.length()); 

     String text = "var " + LineSplit[1] + "=" + LineSplit2 + ";"; 
     //text = text.replace("\n", "").replace("\r", ""); 
     System.out.println(text); 
     } 

    } 

출력을 만족하는 경우

0

모르겠어요은 무엇인가 당신의 목적은 문자열을 두 번 분할하는 것입니다. 원하는대로 넣으십시오. 아래 해결책은 충분하다고 생각합니다. 아래 코드를 살펴보십시오. 원하는 항목 :

String line = "n number 1"; 
    String JAVASCRIPTTextToWrite = ""; 
    //Numbers (Need errors on sort and numbers) 
    if(line.contains("n")) { 
     //split the line with space 
     String[] LineSplit = line.split(" "); 

     if(LineSplit.length == 3) { 
      StringBuilder text = new StringBuilder(); 
      text.append("var "); 
      text.append(LineSplit[1]); 
      text.append("="); 
      text.append(LineSplit[2]); 
      text.append(";"); 

      JAVASCRIPTTextToWrite += text.toString().replace("\n", "").replace("\r", ""); 
      System.out.println(JAVASCRIPTTextToWrite); 
     } 
    } 
+0

줄이 나뉘어져 있어야하므로 사용자는 확인 가능한 이름을 – user1892884

+0

으로 지정할 수 있습니다. 한 번에 한 줄씩 나누면 충분하다고 생각합니다. – Ardy