# Thread: Converting a char to an int

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
Java 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);```
Pos class
Java Code:
```public class Pos
public int x;
public int y;
public Pos(int x, int y)
{
this.x = x;
this.y = y;
}```
the method in the pieces class
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

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
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);
}```

3. Member
Join Date
Dec 2009
Posts
31
Rep Power
0
I didn't understand how to override the toString()

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
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.

5. gcampton 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.

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
public static String format(String format,
Object... args)
but I don't know how to use it. I think that if I get that I will move fwd.

7. Originally Posted by michail
It's confusing. The Pos class doesn't have a toString()
Actually, yes, it does. All classes have a default toString() that returns a String representation of something similar to an object's memory position.

To make toString more functional, override it yourself and have it output a String that makes sense.

Much luck!

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
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```
Pos class (I added this)

Java Code:
```
public String toString()
{
return String.format("(%d,%d)", x, y);
}```

9. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
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;`

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.

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

Java 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);```
to here

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;
}*/
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•