473,804 Members | 3,570 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help on counting

20 New Member
Well im writing a program in java and my program had to do with Sudoku but its only 4 by 4 not 9 by 9. My program checks to see weather the rows, columns, and regions add up to 10. The output would look like this...

Enter Row 1: 3 2 4 1
Enter Row 2: 4 1 3 2
Enter Row 3: 1 4 2 3
Enter Row 4: 2 3 1 4

REG-1:GOOD
REG-2:GOOD
REG-3:GOOD
REG-4:GOOD

ROW-1:GOOD
ROW-2:GOOD
ROW-3:GOOD
ROW-4:GOOD

COL-1:GOOD
COL-2:GOOD
COL-3:GOOD
COL-4:GOOD

SUDO:VALID

My problem is the last part where it says sudo:valid. I know i need to make a variable that counts the number of things that are invalid and if that variable's value is 0 at the end of the program then i have a valid sudoku. But im not sure how to do this. Anyone know how? Heres my code

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class test
  4.  {
  5. public static void main( String args[] )
  6.  {
  7. Scanner input = new Scanner( System.in );
  8.   int a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4, d1, d2, d3, d4;
  9.  
  10. System.out.print ( "Enter First Row:" ); 
  11.  a1 = input.nextInt();  
  12.  a2 = input.nextInt(); 
  13.  a3 = input.nextInt();
  14.  a4 = input.nextInt(); 
  15.  
  16. System.out.print ( "Enter Second Row:" ); 
  17.  b1 = input.nextInt();  
  18.  b2 = input.nextInt(); 
  19.  b3 = input.nextInt();
  20.  b4 = input.nextInt(); 
  21.  
  22. System.out.print ( "Enter Third Row:" ); 
  23.  c1 = input.nextInt();  
  24.  c2 = input.nextInt(); 
  25.  c3 = input.nextInt();
  26.  c4 = input.nextInt(); 
  27.  
  28. System.out.print ( "Enter Fourth Row:" ); 
  29.  d1 = input.nextInt();  
  30.  d2 = input.nextInt(); 
  31.  d3 = input.nextInt();
  32.  d4 = input.nextInt(); 
  33.  
  34. // Regions
  35.  
  36. if (a1 + a2 + b1 + b2 == 10)
  37.  {
  38. System.out.println ( "\nREG-1:GOOD" );
  39.  }
  40. else
  41.  {
  42. System.out.println ( "\nREG-1:BAD" );
  43.  }
  44.  
  45. if (c1 + c2 + d1 + d2 == 10)
  46.  {
  47. System.out.println ( "REG-2:GOOD" );
  48.  }
  49. else
  50.  {
  51. System.out.println ( "REG-2:BAD" );
  52.  }
  53.  
  54. if (a3 + a4 + b3 + b4 == 10)
  55.  {
  56. System.out.println ( "REG-3:GOOD" );
  57.  }
  58. else
  59.  {
  60. System.out.println ( "REG-3:BAD" );
  61.  }
  62.  
  63. if (c3 + c4 + d3 + d4 == 10)
  64.  {
  65. System.out.println ( "REG-4:GOOD" );
  66.  }
  67. else
  68.  {
  69. System.out.println ( "REG-4:BAD" );
  70.  }
  71.  
  72. // Rows
  73.  
  74. if (a1 + a2 + a3 + a4 == 10)
  75.  {
  76. System.out.println ( "\nROW-1:GOOD" );
  77.  }
  78. else
  79.  {
  80. System.out.println ( "\nROW-1:BAD" );
  81.  }
  82.  
  83. if (b1 + b2 + b3 + b4 == 10)
  84.  {
  85. System.out.println ( "ROW-2:GOOD" );
  86.  }
  87. else
  88.  {
  89. System.out.println ( "ROW-2:BAD" );
  90.  }
  91.  
  92. if (c1 + c2 + c3 + c4 == 10)
  93.  {
  94. System.out.println ( "ROW-3:GOOD" );
  95.  }
  96. else
  97.  {
  98. System.out.println ( "ROW-3:BAD" );
  99.  }
  100.  
  101. if (d1 + d2 + d3 + d4 == 10)
  102.  {
  103. System.out.println ( "ROW-4:GOOD" );
  104.  }
  105. else
  106.  {
  107. System.out.println ( "ROW-4:BAD" );
  108.  }
  109.  
  110.  
  111. // comlumns
  112.  
  113. if (a1 + b1 + c1 + d1 == 10)
  114.  {
  115. System.out.println ( "\nCOL-1:GOOD" );
  116.  }
  117. else
  118.  {
  119. System.out.println ( "\nCOL-1:BAD" );
  120.  }
  121.  
  122. if (a2 + b2 + c2 + d2 == 10)
  123.  {
  124. System.out.println ( "COL-2:GOOD" );
  125.  }
  126. else
  127.  {
  128. System.out.println ( "COL-2:BAD" );
  129.  }
  130.  
  131. if (a3 + b3 + c3 + d3 == 10)
  132.  {
  133. System.out.println ( "COL-3:GOOD" );
  134.  }
  135. else
  136.  {
  137. System.out.println ( "COL-3:BAD" );
  138.  }
  139.  
  140. if (a4 + b4 + c4 + d4 == 10)
  141.  {
  142. System.out.println ( "COL-4:GOOD" );
  143.  }
  144. else
  145.  {
  146. System.out.println ( "COL-4:BAD" );
  147.  }
  148.  
  149.  
  150. }
  151.  
  152. }
  153.  
  154.  
Sep 23 '08 #1
3 1237
r035198x
13,262 MVP
Check an article in the Java howTos area by Jos which talks about sudoku solvers.
Sep 23 '08 #2
ManidipSengupta
4 New Member
Have you thought about using arrays? Here you go -

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner; 
  2.  
  3. public class Sudoku4 { 
  4.     protected    int    n=4, checkSum=10, failCount=0;
  5.     protected    Scanner    input = new Scanner(System.in);
  6.  
  7.     public Sudoku4 () {
  8.         int[][] a = new int[n][n];
  9.         for (int iRow = 0 ; iRow < n ; iRow++) {
  10.             System.out.print ( "Enter Row #"+(iRow+1)+ ": ");
  11.             for (int jCol = 0 ; jCol < n ; jCol++)
  12.                 a[iRow][jCol] = input.nextInt();
  13.         }
  14.  
  15.         // Regions
  16.         System.out.println();
  17.         int regionIndex = 1, xCount = n/2, yCount = n/2;
  18.         for (int ix = 0 ; ix < n ; ix += xCount) {
  19.             for (int jy = 0 ; jy < n ; jy += yCount) {
  20.                 failCount += checkRegion(a,
  21.                     ix, jy, xCount, yCount,
  22.                     "REG-" + regionIndex + ":");
  23.                 regionIndex++;
  24.         }}
  25.  
  26.         // Rows
  27.         System.out.println();
  28.         regionIndex = 1; xCount = 1; yCount = n;
  29.         for (int ix = 0 ; ix < n ; ix += xCount) {
  30.             for (int jy = 0 ; jy < n ; jy += yCount) {
  31.                 failCount += checkRegion(a,
  32.                     ix, jy, xCount, yCount,
  33.                     "ROW-" + regionIndex + ":");
  34.                 regionIndex++;
  35.         }}
  36.  
  37.         // Columns
  38.         System.out.println();
  39.         regionIndex = 1; xCount = n; yCount = 1;
  40.         for (int ix = 0 ; ix < n ; ix += xCount) {
  41.             for (int jy = 0 ; jy < n ; jy += yCount) {
  42.                 failCount += checkRegion(a,
  43.                     ix, jy, xCount, yCount,
  44.                     "COL-" + regionIndex + ":");
  45.                 regionIndex++;
  46.         }}
  47.                 }
  48.  
  49.     private int checkRegion (int[][] a, int rowStart, int colStart,
  50.             int rowCount, int colCount, String identifier) {
  51.         int sum = 0, errCountInRegion = 1;
  52.         for (int i = 0; i < rowCount ; i++)
  53.             for (int j = 0 ; j < colCount ; j++)
  54.                 sum += a[i+rowStart][j+colStart];
  55.         if (sum == checkSum)
  56.             errCountInRegion = 0;
  57.         System.out.println (identifier + 
  58.             ((errCountInRegion == 0) ? "GOOD" : "BAD"));
  59.         return errCountInRegion;
  60.     }
  61.  
  62.     public void printFailCount () {
  63.         System.out.println();
  64.         if (failCount == 0)
  65.             System.out.println ("Sudoku: VALID");
  66.         else
  67.             System.out.println("Fail Count = " + failCount);
  68.     }
  69.  
  70.     public static void main(String args[] ) { 
  71.         new Sudoku4().printFailCount();
  72.     }
  73. }
  74.  
I learnt about the Scanner class today, thanks to you. Best wishes, - M.
Sep 23 '08 #3
moorcroft
57 New Member
you could always just add a count variable at the top ie:

Expand|Select|Wrap|Line Numbers
  1. int failCount = 0;
Then each time it hits the code where it says

Expand|Select|Wrap|Line Numbers
  1. {
  2. //if rows do not add to 10 output fail;
  3. failCount++;
  4. }
Then at the end, say

Expand|Select|Wrap|Line Numbers
  1. if failCount>0 output "Fail"
Sep 23 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
2186
by: Elbert Lev | last post by:
Please correct me if I'm wrong. Python (as I understand) uses reference counting to determine when to delete the object. As soon as the object goes out of the scope it is deleted. Python does not use garbage collection (as Java does). So if the script runs a loop: for i in range(100): f = Obj(i)
5
2217
by: Carl Bevil | last post by:
I'm creating a library for internal use that relies on third-party code. I don't want clients of this library to know anything about the third party code, when compiling or running. Generally I've been achieving this by having an abstract base class which defines an interface, and inheriting a concrete class which defines the implementation. Clients of the library deal only with the base class and request objects of that type from a...
1
3257
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage collector, such as the one used in Java, maintains a record of whether or not an object is currentlys being used. An unused object is tagged as garbage,
18
2948
by: ChadDiesel | last post by:
I appreciate the help on this group. I know I've posted a lot here the last couple of weeks, but I was thrown into a database project at my work with very little Access experience. No other employee knows anything about Access. I've searched Google Groups, and that has been a lot of help, but there are some questions that I just can't find the answer to. I'll try to take it easy on the group after this question. I have one more...
9
2325
by: terry | last post by:
I am a programmer (cobol, peoplesoft, sqr, etc.) so I am familiar with programming logic, etc. but not very familiar with C. I need a C program in a study I'm doing. The program is fairly simple, but not familiar with C code it would take me some time to get it to work. A good C programmer can probably give me the code in a few minutes. Here's the program specs: I'm doing a study on the italicized words in the King James Bible. The...
5
1772
by: Jintty | last post by:
Hi, I'm trying to write a program that will read a txt file, copy it into another text file and display the number of words, lines and paragraphs. I was able to get the copying portion done, but i'm struggling with the counters. processBlank is supposed to increment the number of words whenever it hits a nonblank, so i figured using if its not equal to blank then update word count but no luck please help me, i'd appreciate any help
4
4199
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { } double &operator()(int i) { return myX; }
4
2212
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will implement all three techniques as programs. In these programs, as well as solving the problem, you will also measure how long the program takes to run. The programs are worth 80% of the total mark. The final 20% of the marks are awarded for a...
6
2133
by: naknak | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will implement all three techniques as programs. In these programs, as well as solving the problem, you will also measure how long the program takes to run. The programs are worth 80% of the total mark. The final 20% of the marks are awarded for a...
1
2279
by: oec.deepak | last post by:
Hi Cn any one telll me what is Reference counting in C++.
0
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10567
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5515
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.