2017-02-28 271 views
-4

我有一個字符串數組,它在第一個單元格中有"lastname,"。我想刪除" , "從字符串數組中的字符串末尾刪除「,」

我不想要其他的幫助,只是如何從最後的名字拉出昏迷。

這是送給

1 T T T T T F T T F F 
Bobb, Bill 123456789 T T T T T F T T F F 
Lou, Mary 974387643 F T T T T F T T F F 
Bobb, Sam 213458679 F T F T f t 6 T F f 
Bobb, Joe 315274986 t t t t t f t t f f 
ZZZZ 

輸入這是輸出通緝測驗1 結果:

123-45-6789 Bill Bobb 10 
974-38-7643 Mary Lou 9 
213-45-8679 Sam Bobb 5 
315-27-4986 Joe Bobb 10 

The average score is 8.5 

這是我

while(!input.equalsIgnoreCase("zzzz")) //while the input is not "zzzz" loop will run 
    { 
    String [] key = input.split ("\\s+"); //takes String input and converts it to an array seperated by " " 
    input = br.readLine(); 
     while(!input.equalsIgnoreCase("zzzz")) 
     { 
     String [] student = input.split ("\\s+");//takes in student information seperated by " " 
     String lname = student[0].substring(0); 
     String id = student[2].substring(0,3) + "-" + student[2].substring(3,5)+"-"+ student[2].substring(5);//inserts "-" into id# 
     System.out.println (id); 
     System.out.println (lname.trim("\,")); 
     input = br.readLine(); 
     } 
    } 
+2

'String.replaceAll(」, 「」「)'或'簡單地String.substring(0,string.length減() - 1)' – kaetzacoatl

+0

@kaetzacoatl使用'與string.replace ()',除非你需要正則表達式。 – shmosel

回答

0

而(輸入! .equalsIgnoreCase(「zzzz」)) { String [] student = input.split(「\ s +」); //獲取由「」分隔的學生信息 String lname = student [0] .substring(0,student [0] .length() - 1); //從姓氏中刪除「,」。 String fname = student [1]; String id = student [2] .substring(0,3)+「 - 」+ student [2] .substring(3,5)+「 - 」+ student [2] .substring(5); //插入「 - 「到ID#

 int score =0; 
     int i =1; 
     while(i<=key.length-1) 
      { 
      if(key[i].equalsIgnoreCase(student[i+2])) 
      { 
       score++; 
       i++; 
      } 
      else 
       i++; 

      tscore += score; 
      scount++; 
      } 
     if(scount == 0) 
      System.out.println ("Empty class data"); 

     System.out.println(scount); 
     System.out.println ("Results for quiz " + key[0] + ":" +'\n');  
     System.out.println (id + " " +fname+ " "+lname+" "+score); 
     System.out.println(key[1] +":"+student[3]); 
     input = br.readLine(); 
     }