【问题标题】:how to convert a string to an int in java and and separate the values of the string?如何在java中将字符串转换为int并分隔字符串的值?
【发布时间】:2013-10-10 19:40:14
【问题描述】:

我正在用 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;
    }

【问题讨论】:

  • Integer.parseInt 可能是一个开始的地方..
  • W c 3 ... 6 is the row ?
  • 做什么?在游戏上工作?解析用户输入?重新绘制游戏板?
  • @Fareed Warrad;您可能想要改写您的标题,因为您所做的不仅仅是将字符串转换为整数
  • 您可以使用空格作为分隔符进行拆分,有一个字符串“abcdef...yz”并从中获取索引以获取与您的字母对应的数字,并使用 parseInt 转换字符串到数字的整数。

标签: java


【解决方案1】:

如果您的字符串严格是“a b”形式,其中 a 在 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的String.matches方法使用正则表达式检查)。您所要做的就是检查是否

s.matches("[a-z] [0-9]")

是真的。我的方法是回到过去的 C 时代。

【讨论】:

    【解决方案2】:

    我会这样做:

    • 使用空格作为分隔符进行分割
    • indexOf(letter) in "abcdefg..xyz"
    • Integer.parseInt(digit)

    【讨论】:

      【解决方案3】:

      按以下顺序进行:

      假设你有一个表单的输入:

      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]);
      

      【讨论】:

        【解决方案4】:

        你可以这样做(cmets解释它):

        // 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]);
        

        【讨论】:

        • 尽管 shall we call it the augmented ellipsis operator .... 在语法上是无效的。
        • @Bathsheba 假设在这种情况下它会被理解为“等”或相当于一个范围的东西。我很抱歉并编辑我的答案。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-16
        • 2021-08-01
        • 2015-01-29
        • 2013-11-03
        • 2010-10-19
        • 1970-01-01
        相关资源
        最近更新 更多