Results 1 to 11 of 11
Thread: Converting a char to an int
- 12-29-2009, 06:55 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 31
- Rep Power
- 0
Converting a char to an int
Hi again.
I have a method to convert a char to an int [for a chess game] but it prints out this when I do the check:
Enter current position:
a2
The position is:
Pos@bf32c (which changes each time I compile)
Some of the braces I left out deliberately, the input are the coordinates
My code is:
chess class
Pos classJava Code:Pos curPos; do { System.out.println("Enter current position "); String curSquare = sc.nextLine(); curPos = Pieces.squareToPos(curSquare); } while (curPos == null); System.out.println(" "+curPos);
the method in the pieces classJava Code:public class Pos public int x; public int y; public Pos(int x, int y) { this.x = x; this.y = y; }
Java Code:public static Pos squareToPos(String square) { int x = -1; int y = -1; char col; char row; row = square.charAt(0); if (row == 'a') x = 0; if (row == 'b') x = 1; if (row == 'c') x = 2; if (row == 'd') x = 3; if (row == 'e') x = 4; if (row == 'f') x = 5; if (row == 'g') x = 6; if (row == 'h') x = 7; col = square.charAt(1); if (col == '1') y = 0; if (col == '2') y = 1; if (col == '3') y = 2; if (col == '4') y = 3; if (col == '5') y = 4; if (col == '6') y = 5; if (col == '7') y = 6; if (col == '8') y = 7; if (x == -1 || y == -1) { return null; } else return new Pos(x, y); }
Thanks for answering
- 12-29-2009, 07:55 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
System.out.println(" "+curPos);
The string concatenation (+) causes the toString() of the Pos class method to be called for curPos. This method returns a string and the default behaviour (which it inherits from the Object class) is to return a string like "Pos@bf32c".
If you want to see something else you should override toString() to do something else:
Java Code:public String toString() { return String.format("(%d,%d)", x, y); }
- 12-30-2009, 09:44 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 31
- Rep Power
- 0
I didn't understand how to override the toString()
- 12-30-2009, 10:43 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Just put the method I posted into the Pos class.
We call this "overriding" because the method will take the place of the toString() method that the Pos class already has because it is a sort of Object.
- 12-31-2009, 12:48 AM #5gcampton Guest
the 'Pos@bf32c' is just the address of the memory location of the object, when we override the toString() and make our own, telling it we want to see output for something such as an int value, or such. Then it prints out the value we want instead of the memory location. As you start to learn about inheritence this will make more sense.
- 12-31-2009, 02:49 PM #6
Member
- Join Date
- Dec 2009
- Posts
- 31
- Rep Power
- 0
It's confusing. The Pos class doesn't have a toString()
At the moment I have a curPos which is the coordinate 'a3' typed in by the player.
I found the
but I don't know how to use it. I think that if I get that I will move fwd.public static String format(String format,
Object... args)
-
- 12-31-2009, 03:25 PM #8
Member
- Join Date
- Dec 2009
- Posts
- 31
- Rep Power
- 0
Brilliant!!
For input: b1
Result: (1,0)
However in the 8x8 array shouldn't the 1 from "b1" be 1? Maybe not
Thanks to all. I will be posting more though!!
chess class
Pos class (I added this)Java Code:do { System.out.println("Enter current position "); String curSquare = sc.nextLine(); curPos = Pieces.squareToPos(curSquare); } while (curPos == null); System.out.println(curPos.toString()); //changed this
Java Code:public String toString() { return String.format("(%d,%d)", x, y); }
- 12-31-2009, 08:27 PM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
For input: b1
Result: (1,0)
However...
b1 --> (1,0) looks OK to me. The squareToPos() method is "zero based" - starts from zero. So for example a column of '1' results in the first y value: a value of zero.
Java Code:if (col == '1') y = 0;
- 01-06-2010, 08:04 PM #10
Member
- Join Date
- Dec 2009
- Posts
- 31
- Rep Power
- 0
I am completely stuck now. As I go on it gets complicated. I need to do sth with the coordinates. I have x and y, how do I send it to a method so I can move a piece.
- 01-06-2010, 09:09 PM #11
Member
- Join Date
- Dec 2009
- Posts
- 31
- Rep Power
- 0
I have tried this but the error says I cannot access a non static method from a static context.
[the pos class is above]
Trying to send this
to hereJava Code:System.out.println("Enter name of piece "); String piece = sc.nextLine(); //ask for moves // maybe look for a particular type of input from the user, sc.nextInt(); //looks for integer inputs though unless the input is integer you will get errors do { System.out.println("Enter current position "); String curSquare = sc.nextLine(); curPos = Pieces.squareToPos(curSquare); //is this right? [B]Pos.x = Move.getSourceRow();[/B] //ask for moves System.out.println("Enter destination "); String desSquare = sc.nextLine(); desPos = Pieces.squareToPos(desSquare); //need two input methods? } while (curPos == null);
MOVE CLASS
Java Code:public class Move { public int sourceRow, sourceCol; public int destRow, destCol; public Move(int fromRow, int fromCol, int toRow, int toCol) { sourceRow = fromRow; sourceCol = fromCol; destRow = toRow; destCol = toCol; } public void setFromRow(int fromx) { sourceRow = fromx; } /** Returns the source row. */ public int getSourceRow() { return sourceRow; } /** Returns the source column. */ public int getSourceCol() { return sourceCol; } /** Returns the destination row. */ public int getDestRow() { return destRow; } /** Returns the destination column. */ public int getDestCol() { return destCol; } /** Sets the source square. */ public void setSource(int sourceRow, int sourceCol) { } /** Sets the destination square. */ public void setDestination(int destRow, int destCol) { } /* public void makeMove(int fromRow, int fromCol, int toRow, int toCol) { Baseboard[toRow][toCol] = board[fromRow][fromCol]; // Move the piece. Baseboard[fromRow][fromCol] = null; }*/ }
Similar Threads
-
Compare a char
By Torgero in forum New To JavaReplies: 3Last Post: 02-01-2009, 03:24 PM -
drawing char by char with Graphics
By diggitydoggz in forum New To JavaReplies: 5Last Post: 12-27-2008, 12:49 PM -
char to string
By kian_hong2000 in forum New To JavaReplies: 2Last Post: 08-25-2008, 01:51 PM -
Casting an int value into a char
By kurtulas in forum New To JavaReplies: 2Last Post: 02-16-2008, 08:03 PM -
Help with, String, Char
By lenny in forum New To JavaReplies: 1Last Post: 07-25-2007, 02:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks