如何在java中将字符串转换为int并分离字符串的值?

时间:2013-10-10 19:40:15

标签: java

我正在研究java中的棋盘游戏。目前,该板已在二维阵列中初始化。玩家可以通过输入他的筹码的颜色加上他的移动来进行移动,例如通过输入:“W c 3”W =筹码/玩家c的颜色是对应于该列的字母,6是该行。我需要能够从字符串中获取值并更新电路板的行和列。所以“a 1”应该是row = 1 col = 1.“b 1”应该是row = 1 col = 2“e 5”将是row = 5 col = 5作为示例。

我该怎么做呢?

这是我的move.java类中的代码,如果这有帮助:我正在处理的方法是Move(String str)方法。

public class Move implements Comparable<Move>{
    static final int PASS_VALUE = 0; 
    int row, col; 
    boolean movement;
    int pass;
    int pos;
    Board board; 
    /**
     * 
     * Default constructor that initializes a pass move
     */
    Move(){
        row = PASS_VALUE; 
        col = PASS_VALUE;
    }//Move default contructor
   /**
    * 
    * @param rowValue
    * @param colValue 
    */
    Move(int rowValue, int colValue){
        row = rowValue; 
        col = colValue; 
    }//Move constructor

    /**
     * 
     * @param oldMove -- Move to be copied 
     */
    Move(Move oldMove){
        row = oldMove.row;
        col = oldMove.col; 

    }//Move clone constructor

    Move(String str) {
        //int i = Integer.parseInt( str );


    } //Move String constructor

    Move (int positions) {

    }//Move Positions constructor

    /**
     * 
     * @return string value of Move 
     */
    @Override
    public String toString(){
        String result ="";
        String headers = " abcdefgh";
        char colLetter = headers.charAt(col);
        result = colLetter + " " + row; 

        return result;
    }//toString
    /**
     * 
     * @param otherMove -- move to be compared 
     * @return 
     *      -1 if this move precedes otherMove
     *       0 if this move equals otherMove
     *       1 if this move succeeds otherMove
     */
    @Override
    public int compareTo(Move otherMove){
        return 0;
    }//compareTo

   boolean isAMove() {
        return movement; 
    }
   boolean isAPass(){
       return row == PASS_VALUE;
   }
}//Move

***请记住,此代码正在填充字符串变量(str):

Move getOpponentMove(BufferedReader keyboard) throws IOException {
        OthelloOut.printComment("Please enter your move");
        InputStreamReader reader = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(reader);
        String initializeStr = keyboard.readLine();
        Move opponentMove = new Move(initializeStr);

        return opponentMove;
    }

4 个答案:

答案 0 :(得分:1)

如果您的字符串严格形式为“a b”,其中a在a - z的范围内,而b是0到9范围内的数字,则执行类似

的操作
/* s is your string */
int row = s.charAt(0) - 'a' + 1;
int col = s.charAt(2) - '0' + 1;

我利用了ASCII字符数值以及'a'是字节数据类型的事实。

当然,在制作中,您应该预先验证s(检查其长度,第二个字符是否是空格等。您甚至可以通过Java {{1}使用正则表达式检查} 方法)。您所要做的就是检查是否

String.matches

是真的。我的方法是对古老的C日的回归。

答案 1 :(得分:0)

我会这样做:

  • 使用空格作为分隔符进行拆分
  • “abcdefg .... xyz”中的indexOf(字母)
  • 的Integer.parseInt(数字)

答案 2 :(得分:0)

按以下顺序执行:

假设您输入了以下表单:

String input = new String("W c 3");

1.使用split():

分析输入
String color = input.split(" ")[0];
String column = input.split(" ")[1];

2.对于int变量,调用方法parseInt()

int row = Integer.parseInt(input.split(" ")[2]);

答案 3 :(得分:0)

你可以这样做(评论解释):

// create an array of the alphabet from a - z
public String[] alpha = {"a", "b", "c", "d", "e"}; //...etc.

// here is your value
String input = "e 5";

// split it at the space
String[] split = input.split(" ");

// find it in the array and add 1 to get your row (because arrays start at 0)
int row = Arrays.asList(alpha).indexOf(split[0]) + 1;

// get the column as well
int column = Integer.parseInt(split[1]);
相关问题