如何更改字符串,因为我扫描文本Java

问题描述:

我想替换“。”与“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()不会修改字符串本身,而是返回一个新的字符串,其中的发生已被替换。 –

正如评论所说,String是不变的,所以

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

应该是这样的

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