## Java Sudoku Solver

I have just uploaded a program I wrote a couple of weeks ago to GitHub. The program is a sudoku solver which I wrote in Java. I am also posting the source code below.

Usage
The program reads the sudoku from a text file called “Sudoku.txt”. Which should be formatted like so:

```860020000
000700059
000000000
000060800
040000000
005300007
000000000
020000600
007509000
```

Where the 0s are the blanks in the given puzzle. The program will then print the solution to the command line.
When input the puzzle above:

Here is the source code:

```import java.util.Scanner;
import java.io.File;

public class SudokuSolver
{
public static void main(String[] args) throws Exception
{
Scanner fileScanner = new Scanner(new File(&quot;Sudoku.txt&quot;));

int[][] sudoku = new int[9][9]; //Will hold the puzzle in a 2D array.
//We will use this variable to hold the next line of the puzzle, then parse
//each digit on that line.
String line = fileScanner.nextLine();

//Loops round placing the digits into the correct place of the array.
for (int y = 0; y &lt; 9; y++)
{
for (int x = 0; x &lt; 9; x++)
{
//Gets the digit converts it from a char to an in and places it in the array.
sudoku[y][x] = Character.getNumericValue(line.charAt(x));
//At the end of each line we load the next line, but check there is a next
//line before trying to load it so the program does not crash.
if (x == 8 &amp;&amp; fileScanner.hasNextLine())
{
line = fileScanner.nextLine();
}
} //for x
} //for y

//The recursive function that actually solves the sudoku (starting at 0,0)
solve(sudoku, 0, 0);

} //main

//This is a recursive function used to go through each cell and place a valid number.
//Until they are filled in.
private static void solve(int[][] sudoku, int cellX, int cellY)
{
//If the y value is 9 then the sudoku has been solved.
if(cellY &gt; 8)
{
printSudoku(sudoku);
System.out.println();
//System.exit(1); // = This will end the program quicker as it does not have to
//&quot;go back up&quot; through the levels of recursion, but means the main
//routine will not continue running.
//Also if there is more than one solution to the sudoku using this will only print
//the first solution found.
}
else
{
//Here we calculate the next digit for the solve routine to try.
int nextX = cellX;
int nextY = cellY;
if(cellX == 8)
{
//When at the end of a row add 1 to the row and reset the &quot;column&quot; to 0.
nextX = 0;
nextY++;
}
else
{
nextX++;
}

//If the digit was already given to us, we can move onto the next one.
if(sudoku[cellY][cellX] != 0)
{
solve(sudoku, nextX, nextY);
}
else
{
//Otherwise, starting at 1 through 9 we check if the number is &quot;legal&quot;
//and if so place that number, and move on to the next cell.
for(int checkNum = 1; checkNum &lt; 10; checkNum++)
{
if(checkSquare(sudoku, cellX, cellY, checkNum)
&amp;&amp; checkRow(sudoku, cellY, checkNum)
&amp;&amp; checkCol(sudoku, cellX, checkNum))
{
sudoku[cellY][cellX] = checkNum;
solve(sudoku, nextX, nextY);
}
}
//If we get to here it means in it's current state the sudoku is impossible
//which means that one of the numbers we &quot;placed&quot; earlier is incorrect.
sudoku[cellY][cellX] = 0;
}
}
}

//This method is given a cell location and a number to check, it then checks
//whether that number is already in the 3x3 square and returns false if so.
private static boolean checkSquare(int[][] sudoku, int reqX, int reqY, int toCheck)
{
int rowY;
int colX;

//First we work out which column the &quot;square&quot; belongs to.
//We take the given x value and if it is below 3 then that means
//the square we need is in the first column (out of 3). etc.
if(reqX &lt; 3)
{
colX = 0;
}
else if (reqX &lt; 6)
{
colX = 3;
}
else
{
colX = 6;
}

//We do the same but for the rows. For example if the y value is 5 then
//the related square would be on the second row.
if(reqY &lt; 3)
{
rowY = 0;
}
else if (reqY &lt; 6)
{
rowY = 3;
}
else
{
rowY = 6;
}

//We have now defined the square we need to check and have the top left
//co-ordinate stored in the variables rowY and colX.
//We now loop round and check each digit in the square, and if a digit matches
//we return false.
for(int y = rowY; y &lt; rowY + 3; y++)
{
for(int x = colX; x &lt; colX + 3; x++)
{
if(sudoku[y][x] == toCheck)
{
return false;
}
}
}

return true; //number not in the square.

}

//Checks if a given number is in a given row and returns false if it is.
private static boolean checkRow(int[][] sudoku, int rowY, int toCheck)
{
//loops round each digit in a row.
for(int x = 0; x &lt; 9; x++)
{
//Checks if the given number is the same as the current digit
//and returns false if so.
if (toCheck == sudoku[rowY][x])
{
return false;
}
}
return true; //the number is not in the row.
}

//Checks if a given number is in a given column and returns false if it is.
private static boolean checkCol(int[][] sudoku, int colX, int toCheck)
{
//Loops round each digit in a column.
for(int y = 0; y &lt; 9; y++)
{
//Checks if the current digit is the given digit and returns false if so.
if (toCheck == sudoku[y][colX])
{
return false;
}
}
return true; //the number is not in the column.
}

//Prints the sudoku to the screen.
private static void printSudoku(int sudoku[][])
{
//Loops round each digit and prints it.
for(int y = 0; y &lt; 9; y++)
{
for(int x = 0; x &lt; 9; x++)
{
System.out.print(sudoku[y][x]);
//Starts a new line when at the end of a row.
if(x == 8)
{
System.out.println();
}
} //for x
} //for y
} //printSudoku
} //SudokuSolver
```

## Whose number is it anyway?

Recently, I got a new mobile contract with Orange through Dial-a-phone since my old contract (also with Orange) was up (at last!). Since the new contract had been ‘purchased’ through Dial-a-phone instead of Oranges’ upgrade process I was given a new number. Not a problem, I thought, I’ll just transfer the old number to the new contract…

Turns out it’s not as simple process as I thought it would be!

My first thought was since it was already a number on Orange I would be able to transfer it easily and I would only have to make the one phone call. It was during this phone call I learnt there is going to be more to it than I had expected. Here’s a summary of how it went:

```Me: Could I transfer this number to a new contract which is with Orange.
Orange: That isn't possible.
Me: Oh... If I get the PAC code could I ring up from the new sim and use it to
transfer the number?
Orange: You can't transfer a number from Orange to Orange unfortunately.
Me: Okay, in that case can I make sure the contract will end in 30 days
looks like I will just have to keep the new number.
*Asking to end the old contract - you have to give 30 days otherwise it will turn
*into a monthly rolling contract, despite the actual contract length being up *
Orange: Yes, I will put that through now.
Me: Hmmmm... Actually if I get the PAC code for this number, then transfer it to a
different network then back to Orange on the new contract would that work?
Orange: ... Yes that would be the long way round I guess but should work.
Me: Okay. could I get the PAC code then.```

Obviously, I was then given the PAC code. It seems very odd to me that a network can not transfer a number from one sim to another, even through a PAC code? It seems even more odd that I am able to transfer it to a different network then back, why don’t they have the ability to just transfer it internally?

Now armed with the PAC code I went to GiffGaff and T-Mobile and ordered a free sim, with plans of transferring the number to one of them then back to Orange. To do this you first have to top-up the sim, seeing as T-Mobiles minimum top-up was £5 and GiffGaffs was £10 I used T-Mobiles sim. So I topped up, rang them and and gave the PAC code Orange had given me, not a problem, I was told the number would be transferred the next day. Great, I thought!

I waited 2 days to allow the transfer to go through could then use the T-Mobile sim with the number I am trying to keep. All I thought I would have to do now was get the PAC code from T-Mobile and give it to Orange on the new contract and that’d be it. NOPE.

I got the PAC code from T-Mobile without a problem, then rang Orange to give them the PAC code. That phone call went like this:

```*after giving Orange the PAC code*
Orange: Okay and what's the number?
Me: *gives the number*
Orange: Okay... Erm... Right...
Orange: Are you sure that's the correct number *reads it back*
Me: Yes
Orange: It's already on a different Orange account, so I can't transfer it.
Me: Right, I have the T-Mobile sim in a mobile right now which is accepting calls
to that number, so how can it still be on an Orange account?```

Unfortunately, the person at Orange I was speaking to couldn’t help me. So now I am left with my old number currently on T-Mobile and Orange! Surely, that should not be possible, when I gave the PAC to T-Mobile and the number was transferred it should have been disconnected from Orange, but clearly it hasn’t been.

I’m going to try again in a few days and hope that the situation at the moment is just due to the transfer being recent and Orange hasn’t been updated that the number is no longer on their network!

Update (28/03/2013):
Turns out after transferring the number over to T-Mobile in the first place, although it had transferred, it takes a while for it to be processed on Oranges’ end that the number is no longer “with them”. So I have now been able to transfer it back to Orange on the contract I want!