0
107054600
003008529
000000070
004905100
800700900
012003000
700000006
200609004
036010000

The files store a Sudoku puzzle in this way.

I have to be able to read these puzzles into both a 2d int array and a 2d Jlabel array.

It works when reading into the Int array alone. However, when I try assign the icon value of the j Label with the current number being processed by the for loop I get this compiler error:

The constructor javax.swing.JLabel(int) is undefined

Why is this?

The relevant code is below:

JLabel[][] gridLabels = new JLabel[9][9];
int [][] grid = new int [9][9];  // Game Array

try {
 String fileName1 = new Scanner(new File("./temp.txt")).useDelimiter("\\Z").next();
 Scanner fin;
 fin = new Scanner( new File(fileName1));
 //Reads in the puzzle from a temporary text file

 for(int row=0; row<9; row++)
{
 String line = fin.next();
 for(int col=0; col<9; col++)
  { grid[row][col] = line.charAt(col)-'0'; 
   gridLabels[row][col] = new JLabel(line.charAt(col)-'0');    //PROBLEM HERE//
  }
 }
  } 
  catch (FileNotFoundException exception)
  {System.out.println("Incorrect Selection"); // Catches incorrect selection
  }
  catch (NoSuchElementException exception)
  {System.out.println("No Such Element");
  }
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142

2 Answers2

1

In constructor for javax.swing.JLabel you can pass a String but not an int. You should convert your int to String before passing it to Label constructor. You can do conversion from int to String as String.valueOf(i).

Look here for conversion

Community
  • 1
  • 1
Abubakkar
  • 15,488
  • 8
  • 55
  • 83
0

Result of line.charAt(col)-'0' is an int and the error says that JLabel does not have any constructor that takes an int argument.

Try new JLabel(String.valueOf(line.charAt(col)-'0')).

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142