2013-11-26 37 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; 
    } 

} 

文本行是 n個1

輸出應該是

變種數= 1;

但輸出是

變種數目= 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]); 

,如果你的條件滿足

+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); 
     } 

    } 

輸出執行此操作:

var number=1; 
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

OK。一次分割線,我認爲這就夠了。 – Ardy