2016-03-14 32 views
0

我想替換「。」與「X」。這最終是爲了計算機將找出的基於文本的迷宮。我只想讓第一個改變,並希望我可以從那裏找出其餘的部分。這是我迄今爲止所擁有的。如何更改字符串,因爲我掃描文本Java

Scanner sc = new Scanner(maze2); 

public void traverseMaze(int row, int col, int handLocationX, int handLocationY) { 

    for(int rowCount = 0; rowCount <=11 ; rowCount ++){ 
     row = rowCount; 

     for(int colCount = 0; colCount <= 11; colCount++){ 
      col = colCount; 

      if(row ==2 && col ==0){ 
       System.out.println(col); 
       System.out.println(row); 
       sc.next().replace(".", "X"); //stuck here HELP! :) 
      } 
     } 
     System.out.println(maze2); 
    } 
+0

什麼是SC?請解釋你做了什麼,是否有錯誤。 – AchmadJP

+0

對不起,sc是掃描儀。沒有錯誤。 – pewpew

+5

如果sc.next()返回一個字符串,請注意,String.replace()不會修改字符串本身,而是返回一個新的字符串,其中的發生已被替換。 –

回答

0

正如評論所說,String是不變的,所以

sc.next().replace(".", "X"); //stuck here HELP! :) 

應該是這樣的

String orgStr = sc.next(); 
String newStr = orgStr.replace(".", "X");