I have written a program to create a 4x4 grid Sudoku style.
The numbers for the puzzle are read from a file and placed in the puzzle.
As you can see, i have assigned the first numbers 'a' and the second 'b' etc
However, i can not access the a,b,c,d String in the main method. How can i do this?

Puzzle

3 2 1 4
1 4 3 2
2 3 4 1
4 1 2 3

Solver

import java.util.Scanner;
import java.io.*;
import jp.ac.kobe_u.cs.cream.*;

public class FSolver {
  private static Scanner x;
  public static void openFile(){
    try{
      x = new Scanner(new File("Puzzle.txt"));
    }
    catch(Exception e){
      System.out.println("no file");
    }
  }
  
  public static void readFile(){
      while(x.hasNext()){
       String a = x.next();
       String b = x.next();
       String c = x.next();
       String d = x.next();
       
       System.out.printf("%s %s %s %s\n", a, b, c, d);
       
      }
  }
  
  public static void closeFile(){
  x.close();
  }

  public static void main(String[] args){
     openFile();
     readFile();
     
     closeFile();
    Network net = new Network();
    int n = 4;
    IntVariable[][] v = new IntVariable[n][n];
    IntVariable[] vs = new IntVariable[n];
  
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
    v[i][j] = new IntVariable(net, 1, n);
    }
  }
  
    v[0][1].equals(2);
    v[0][3].equals(4);
    v[1][2].equals(3);
    v[2][1].equals(3);
    v[3][0].equals(4);
      
    v[3][3].ge(v[2][3]);
  
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      vs[j] = v[i][j];
    }
    new NotEquals(net, vs);
  }
  
  for (int j = 0; j < n; j++) {
    for (int i = 0; i < n; i++) {
      vs[i] = v[i][j];
    }
    new NotEquals(net, vs);
  }

  for (int i0 = 0; i0 < n; i0 += 2) {
    for (int j0 = 0; j0 < n; j0 += 2) {
      int k = 0;
      for (int i = i0; i < i0+2; i++) {
        for (int j = j0; j < j0+2; j++) {
          vs[k++] = v[i][j];
        }
      }
      new NotEquals(net, vs);
    }
  }
  
  Solver solver = new DefaultSolver(net);
  
  for (solver.start(); solver.waitNext(); solver.resume()) {
    Solution solution = solver.getSolution();
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++)
        System.out.print(solution.getIntValue(v[i][j]) + " ");
      System.out.println();
    }
    System.out.println();
  }
  solver.stop();
}
  
}

If this is what I think, then it is a problem of scope. the read file method just prints out a,b,c,d, and e, but does not assign to a variable that main could access. try declaring a,b,c,d, and e where you declare the scanner variable. That way every piece of code in the class will be able to access it.

thanks, that helped :)

now i have one more problem, it solves the program if you already have the numbers for the Sudoku, however, i need it to solve it if the user has not got all the numbers, i have tried to substitute the numbers for 0, but have come to a blank.
Where the ??????? i think there needs to be code to tell it so, but dont know how to go about it.
Say if i commented out v[j] ... (z); and v .... (x) - the program would solve them, however, i need a value to replace these in the Puzzle.txt as (z) would just read the next value and not the first value
Hopefully you understand the above, if not just ask and ill try and clarify :)


import java.util.Scanner;
import java.io.*;
import jp.ac.kobe_u.cs.cream.*;

public class FSolver {
  private static Scanner x;
  public static void openFile(){
    try{
      x = new Scanner(new File("Puzzle.txt"));
    }
    catch(Exception e){
      System.out.println("no file");
    }
  }
  private static String a;
  private static String b;
  private static String c;
  private static String d;
  private static String e;
  private static String f;
  private static String g;
  private static String h;
  private static String i;
  private static String j;
  private static String k;
  private static String l;
  private static String m;
  private static String nn;
  private static String o;
  private static String p;
  
  public static void readFile(){
      
       a = x.next();
       b = x.next();
       c = x.next();
       d = x.next();
       
       e = x.next();
       f = x.next();
       g = x.next();
       h = x.next();
       
       i = x.next();
       j = x.next();
       k = x.next();
       l = x.next();
       
       m = x.next();
       nn = x.next();
       o = x.next();
       p = x.next();
       
       //System.out.printf("%s %s %s %s \n %s %s %s %s \n %s %s %s %s \n %s %s %s %s \n", a, b, c, d, e, f, g, h,i,j,k,l,m,nn,o,p);
       
      
  }
  
  public static void closeFile(){
  x.close();
  }

  public static void main(String[] args){
     openFile();
     readFile();
     
    Network net = new Network();
    int n = 4;
    IntVariable[][] v = new IntVariable[n][n];
    IntVariable[] vs = new IntVariable[n];
  
    int z = Integer.parseInt(a);
    int y = Integer.parseInt(b);
    int x = Integer.parseInt(c);
    int w = Integer.parseInt(d);
    int vv = Integer.parseInt(e);
    int u = Integer.parseInt(f);
    int t = Integer.parseInt(g);
    int s = Integer.parseInt(h);
    int r = Integer.parseInt(i);
    int q = Integer.parseInt(j);
    int aa = Integer.parseInt(k);
    int bb = Integer.parseInt(l);
    int cc = Integer.parseInt(m);
    int dd = Integer.parseInt(nn);
    int ee = Integer.parseInt(o);
    int ff = Integer.parseInt(p);
    
    //boolean comp = (aa == 0 || bb == 0 || cc == 0 || dd == 0 || ee == 0 || ff == 0 || q == 0 
      //  || r == 0 || s == 0 || t == 0 || u == 0 || vv == 0 || w == 0 || x == 0 || y == 0 || z == 0);
    
 // Create constraint-variables
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
          v[i][j] = new IntVariable(net, 1, n);
      //}else
        //v[i][j] = new IntVariable(?????);
  }
  }
  
//Fixed-value constraints
    v[0][0].equals(z);
    v[0][1].equals(y);
    v[0][2].equals(x);
    v[0][3].equals(w);
    //System.out.printf("\n\n %s %s %s %s\n", z, y, x, w);
    
    v[1][0].equals(vv);
    v[1][1].equals(u);
    v[1][2].equals(t);
    v[1][3].equals(s);
    //System.out.printf("%s %s %s %s\n", vv, u, t, s);
    
    v[2][0].equals(r);
    v[2][1].equals(q);
    v[2][2].equals(aa);
    v[2][3].equals(bb);
    //System.out.printf("%s %s %s %s\n", r, q, aa, bb);
    
    v[3][0].equals(cc);
    v[3][1].equals(dd);
    v[3][2].equals(ee);
    v[3][3].equals(ff);
    //System.out.printf("%s %s %s %s\n", cc, dd, ee, ff);  
    closeFile();
    
    
    /*v[0][1].equals(2);
    v[0][3].equals(4);
    v[1][2].equals(3);
    v[2][1].equals(3);
    v[3][0].equals(4);  
    v[3][3].ge(v[2][3]);*/
    
    
 // Each row must contain distinct values
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      vs[j] = v[i][j];
    }
    new NotEquals(net, vs);
  }
  
//Each column must contain distinct values
  for (int j = 0; j < n; j++) {
    for (int i = 0; i < n; i++) {
      vs[i] = v[i][j];
    }
    new NotEquals(net, vs);
  }

//Each box must contain distinct values
  for (int i0 = 0; i0 < n; i0 += 2) {
    for (int j0 = 0; j0 < n; j0 += 2) {
      int k = 0;
      for (int i = i0; i < i0+2; i++) {
        for (int j = j0; j < j0+2; j++) {
          vs[k++] = v[i][j];
        }
      }
      new NotEquals(net, vs);
    }
  }
  
  Solver solver = new DefaultSolver(net);
  
  for (solver.start(); solver.waitNext(); solver.resume()) {
    Solution solution = solver.getSolution();
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++)
        System.out.print(solution.getIntValue(v[i][j]) + " ");
      System.out.println();
    }
    System.out.println();
  }
  solver.stop();
}
  
}

oop this part is meant to be

//boolean comp = (aa == 0 || bb == 0 || cc == 0 || dd == 0 || ee == 0 || ff == 0 || q == 0 
      //  || r == 0 || s == 0 || t == 0 || u == 0 || vv == 0 || w == 0 || x == 0 || y == 0 || z == 0);
    
 // Create constraint-variables
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      //if (comp == true){
        // A zero in the matrix represents and empty cell*/
          v[i][j] = new IntVariable(net, 1, n);
      //}else
        //v[i][j] = new IntVariable(net, c);
  }
  }

Ok all done, a person on other site replied faster. SOLVED

Sorry, I don't quite understand the problem. Can you give an example of input and output for what your program is supposed to do. Is it a question of how to solve a sudoku in java, or is it that you don't know how to represent a blank, a cell that the user has not entered anything into?

well, what was the solution?

I had the problem put into an array and any 0 where ignored and solved in the way they were supposed to

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.