I keep getting the wrong numbers from my program - java

I am trying to write a program where the user inputs 2 dates and the program calculates the days in between (without using any of the calendar classes) and prints the results, but I keep getting crazy numbers that make no sense. When I tried to make it calculate 5 days it printed the results of 333 and when I try to do something like 20 years it prints numbers in the millions.
import java.util.Scanner;
public class DaysCalc {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Please enter starting date (mm-dd-yyyy) ");
String startingdate = input.next();
System.out.print("Please enter ending date (mm-dd-yyyy) ");
String finishingdate = input.next();
String startingdayst = startingdate.substring(3, 5);
String startingmonthst = startingdate.substring(0, 2);
String startingyearst = startingdate.substring(6, 10);
String finishingdayst = finishingdate.substring(3, 5);
String finishingmonthst = finishingdate.substring(0, 2);
String finishingyearst = finishingdate.substring(6, 10);
int startingyear = Integer.parseInt(startingyearst);
int startingday = Integer.parseInt(startingdayst);
int startingmonth = Integer.parseInt(startingmonthst);
int finishingyear = Integer.parseInt(finishingyearst);
int finishingday = Integer.parseInt(finishingdayst);
int finishingmonth = Integer.parseInt(finishingmonthst);
System.out.println(startingyear + "," + startingday + "," + startingmonth );
System.out.println(finishingyear + "," + finishingday + "," + finishingmonth);
int daysLeft = 0;
int daysUntilEnd = 0;
int i = 0;
daysLeft = daysInAMonth(startingmonth, startingyear) - startingday;
daysUntilEnd = finishingday;
for (int monthCount = startingmonth + 1; monthCount <= 12; monthCount++)
{
i += daysInAMonth(monthCount, startingyear);
}
for (int yearCount = startingyear + 1; yearCount <= finishingyear; yearCount++)
{
if (isLeapYear(yearCount))
{
i = i * 366;
}
else
{
i = i * 365;
}
i += daysUntilEnd;
}
System.out.println(i);
}
private static boolean isLeapYear(int year) {
boolean isLeapYear = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));
return isLeapYear;
}
private static int daysInAMonth(int month, int year) {
if (month == 2)
{
if (isLeapYear(year) )
{
return 29;
}
else
{
return 28;
}
}
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 9 || month == 11) {
return 31;
}
return 30;
}
}

the problem was with this part
for (int monthCount = startingmonth + 1; monthCount < finishingmonth; monthCount++)
{
i += daysInAMonth(monthCount, startingyear);
}
for (int yearCount = startingyear + 1; yearCount <= finishingyear; yearCount++)
{
if (isLeapYear(yearCount))
{
i = i + 366;
}
else
{
i = i + 365;
}
}
i += daysUntilEnd;
i += daysInAMonth(startingmonth, startingyear)-startingday-1;
System.out.println(i);
it's not completely bug free but i hope it helps you to see where it went wrong. it was kind of an eye mistake

Related

Can't figure out the error Luhn check

Its supose to tell me if a card is valid or invalid using luhn check
4388576018402626 invalid
4388576018410707 valid
but it keeps telling me that everything is invalid :/
Any tips on what to do, or where to look, would be amazing. I have been stuck for a few hours.
It would also help if people tell me any tips on how to find why a code is not working as intended.
im using eclipse and java
public class Task11 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a credit card number as a long integer: ");
long number = input.nextLong();
if (isValid(number)) {
System.out.println(number + " is valid");
} else {
System.out.println(number + " is invalid");
}
}
public static boolean isValid(long number) {
return (getSize(number) >= 13) && (getSize(number) <= 16)
&& (prefixMatched(number, 4) || prefixMatched(number, 5) || prefixMatched(number, 6) || prefixMatched(number, 37))
&& (sumOfDoubleEvenPlace(number) + sumOfOddPlace(number)) % 10 == 0;
}
public static int sumOfDoubleEvenPlace(long number) {
int result = 0;
long start = 0;
String digits = Long.toString(number);
if ((digits.length() % 2) == 0) {
start = digits.length() - 1;
} else {
start = digits.length() - 2;
}
while (start != 0) {
result += (int) ((((start % 10) * 2) % 10) + (((start % 10) * 2) / 2));
start = start / 100;
}
return result;
}
public static int getDigit(int number) {
return number % 10 + (number / 10);
}
public static int sumOfOddPlace(long number) {
int result = 0;
while (number != 0) {
result += (int) (number % 10);
number = number / 100;
}
return result;
}
public static boolean prefixMatched(long number, int d) {
return getPrefix(number, getSize(d)) == d;
}
public static int getSize(long d) {
int numberOfDigits = 0;
String sizeString = Long.toString(d);
numberOfDigits = sizeString.length();
return numberOfDigits;
}
public static long getPrefix(long number, int k) {
String size = Long.toString(number);
if (size.length() <= k) {
return number;
} else {
return Long.parseLong(size.substring(0, k));
}
}
}
You should modiffy your isValid() method to write down when it doesn't work, like this:
public static boolean isValid(long number) {
System.err.println();
if(getSize(number) < 13){
System.out.println("Err: Number "+number+" is too short");
return false;
} else if (getSize(number) > 16){
public static boolean isValid(long number) {
System.err.println();
if(getSize(number) < 13){
System.out.println("Err: Number "+number+" is too short");
return false;
} else if (getSize(number) > 16){
System.out.println("Err: Number "+number+" is too long");
return false;
} else if (! (prefixMatched(number, 4) || prefixMatched(number, 5) || prefixMatched(number, 6) || prefixMatched(number, 37)) ){
System.out.println("Err: Number "+number+" prefix doesn't match");
return false;
} else if( (sumOfDoubleEvenPlace(number) + sumOfOddPlace(number)) % 10 != 0){
System.out.println("Err: Number "+number+" doesn't have sum of odd and evens % 10. ");
return false;
}
return true;
}
My guess for your problem is on the getPrefix() method, you should add some logs here too.
EDIT: so, got more time to help you (don't know if it's still necessary but anyway). Also, I corrected the method I wrote, there were some errors (like, the opposite of getSize(number) >= 13 is getSize(number) < 13)...
First it will be faster to test with a set of data instead of entering the values each time yourself (add the values you want to check):
public static void main(String[] args) {
long[] luhnCheckSet = {
0, // too short
1111111111111111111L, // too long (19)
222222222222222l // prefix doesn't match
4388576018402626l, // should work ?
};
//System.out.print("Enter a credit card number as a long integer: ");
//long number = input.nextLong();
for(long number : luhnCheckSet){
System.out.println("Checking number: "+number);
if (isValid(number)) {
System.out.println(number + " is valid");
} else {
System.out.println(number + " is invalid");
}
System.out.println("-");
}
}
I don't know the details of this, but I think you should work with String all along, and parse to long only if needed (if number is more than 19 characters, it might not parse it long).
Still, going with longs.
I detailed your getPrefix() with more logs AND put the d in parameter in long (it's good habit to be carefull what primitive types you compare):
public static boolean prefixMatched(long number, long d) {
int prefixSize = getSize(d);
long numberPrefix = getPrefix(number, prefixSize);
System.out.println("Testing prefix of size "+prefixSize+" from number: "+number+". Prefix is: "+numberPrefix+", should be:"+d+", are they equals ? "+(numberPrefix == d));
return numberPrefix == d;
}
Still don't know what's wrong with this code, but it looks like it comes from the last test:
I didn't do it but you should make one method from sumOfDoubleEvenPlace(number) + sumOfOddPlace(number)) % 10 and log both numbers and the sum (like i did in prefixMatched() ). Add logs in both method to be sure it gets the result you want/ works like it should.
Have you used a debugger ? if you can, do it, it can be faster than adding a lot of logs !
Good luck
EDIT:
Here are the working functions and below I provided a shorter, more efficient solution too:
public class CreditCardValidation {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = 0;
long array[] = new long [16];
do
{
count = 0;
array = new long [16];
System.out.print("Enter your Credit Card Number : ");
long number = in.nextLong();
for (int i = 0; number != 0; i++) {
array[i] = number % 10;
number = number / 10;
count++;
}
}
while(count < 13);
if ((array[count - 1] == 4) || (array[count - 1] == 5) || (array[count- 1] == 3 && array[count - 2] == 7)){
if (isValid(array) == true) {
System.out.println("\n The Credit Card Number is Valid. ");
} else {
System.out.println("\n The Credit Card Number is Invalid. ");
}
} else{
System.out.println("\n The Credit Card Number is Invalid. ");
}
in.close();
}
public static boolean isValid(long[] array) {
int total = sumOfDoubleEvenPlace(array) + sumOfOddPlace(array);
if ((total % 10 == 0)) {
for (int i=0; i< array.length; i++){
System.out.println(array[i]);}
return true;
} else {
for (int i=0; i< array.length; i++){
System.out.println(array[i]);}
return false;
}
}
public static int getDigit(int number) {
if (number <= 9) {
return number;
} else {
int firstDigit = number % 10;
int secondDigit = (int) (number / 10);
return firstDigit + secondDigit;
}
}
public static int sumOfOddPlace(long[] array) {
int result = 0;
for (int i=0; i< array.length; i++)
{
while (array[i] > 0) {
result += (int) (array[i] % 10);
array[i] = array[i] / 100;
}
}
System.out.println("\n The sum of odd place is " + result);
return result;
}
public static int sumOfDoubleEvenPlace(long[] array) {
int result = 0;
long temp = 0;
for (int i=0; i< array.length; i++){
while (array[i] > 0) {
temp = array[i] % 100;
result += getDigit((int) (temp / 10) * 2);
array[i] = array[i] / 100;
}
}
System.out.println("\n The sum of double even place is " + result);
return result;
}
}
I also found a solution with less lines of logic. I know you're probably searching for an OO approach with functions, building from this could be of some help.
Similar question regarding error in Luhn algorithm logic:
Check Credit Card Validity using Luhn Algorithm
Link to shorter solution:
https://code.google.com/p/gnuc-credit-card-checker/source/browse/trunk/CCCheckerPro/src/com/gnuc/java/ccc/Luhn.java
And here I tested the solution with real CC numbers:
public class CreditCardValidation{
public static boolean Check(String ccNumber)
{
int sum = 0;
boolean alternate = false;
for (int i = ccNumber.length() - 1; i >= 0; i--)
{
int n = Integer.parseInt(ccNumber.substring(i, i + 1));
if (alternate)
{
n *= 2;
if (n > 9)
{
n = (n % 10) + 1;
}
}
sum += n;
alternate = !alternate;
}
return (sum % 10 == 0);
}
public static void main(String[] args){
//String num = "REPLACE WITH VALID NUMBER"; //Valid
String num = REPLACE WITH INVALID NUMBER; //Invalid
num = num.trim();
if(Check(num)){
System.out.println("Valid");
}
else
System.out.println("Invalid");
//Check();
}
}

Java Console Calendar in 2 Column

Hi guys i really need your help. I need to output a 3 year calendar in 3 columns. In the first column the year should be the year that the user input.
I have a code that will post a 3 year calendar but only in 1 column. I need to separate the other year in another column plss help me
package project;
import java.util.*;
public class les1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Calendar cal = Calendar.getInstance();
String ans;
// getting the system calendar's year, maximum days,
// weeks per month and system calendar's month.
int year = cal.get(Calendar.YEAR);
int day = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
int week = cal.get(Calendar.DAY_OF_WEEK);
int month = cal.get(Calendar.MONTH);
int col[] = new int[3];
do {
// validation for input year
do {
System.out.print("\nEnter year: ");
while (true)
try {
year = Integer.parseInt(in.next());
break;
} catch (Exception ex) {
System.out.println("Try again!");
System.out.print("\nEnter year: ");
}
if (year < 1900 || year > 2099) {
System.out.println("Try again!");
}
} while (year < 1900 || year > 2099);
/*
* System.out.println("enter number of elements");
*
* int n = in.nextInt();
*
* int arr[] = new int[n];
*
* for (int j = 0; j < n; j++) {// for reading array
*
* }
*
* for (int j : arr) {
*/
// loop for months
for (month = 1; month <= 1; month++) {
// calculating years, month, days and weeks.
int y = year - (14 - month) / 12;
int x = y + y / 4 - y / 100 + y / 400;
int m = month + 12 * ((14 - month) / 12) - 2;
int d = (1 + x + (31 * m) / 12) % 7;
// calculate leap year
boolean LeapYear = (year % 400 == 0);
// displaying month as String
cal.set(year, month, 0);
System.out
.print(cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH) + "\t\t\t\t\t\t");
System.out.print("Sun Mon Tue Wed Thu Fri Sat" + "\t\t\t");
int length = (int) (30 + ((month + (month / 8.0)) % 2));
if (month == 2) {
if (LeapYear) {
length -= 1;
} else {
length -= 2;
}
}
for (int a = 0; a < col.length; a++) {
int counter = 1;
// spacing for first day of the month
for (int i = 0; i < d; i++) {
System.out.print(" ");
counter++;
}
// spacing for days
for (day = 1; day <= length; day++) {
System.out.printf("%2d", day);
System.out.print((counter++ % 7 != 0) ? " " : "\n");
}
}
week = (week + length) % 6;
}
year++;
/* } */
System.out.print("\n\n" + "Enter another year (Y/N)? ");
ans = in.next();
} while (ans.equals("Y") || ans.equals("y"));
System.out.println("\nEnd!");
in.close();
}
}
Take a look at the following code. It will print three different years' calendars in three columns. I have used this formula to determine the day of the week in any month.
import java.util.*;
public class les1 {
public static void main(String[] args) {
String[] days = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
Scanner in = new Scanner(System. in );
Calendar cal = Calendar.getInstance();
String ans;
int year = cal.get(Calendar.YEAR);
int day = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
int week = cal.get(Calendar.DAY_OF_WEEK);
int month = cal.get(Calendar.MONTH);
int col[] = new int[3];
do {
// validation for input year
do {
System.out.print("\nEnter year: ");
while (true)
try {
year = Integer.parseInt( in .next());
break;
} catch (Exception ex) {
System.out.println("Try again!");
System.out.print("\nEnter year: ");
}
if (year < 1900 || year > 2099) {
System.out.println("Try again!");
}
} while (year < 1900 || year > 2099);
for (month = 1; month <= 1; month++) {
HashMap < Integer, Integer > map = new HashMap < Integer, Integer > ();
map.put(1, 0);
map.put(7, 6);
map.put(2, 3);
map.put(8, 2);
map.put(3, 3);
map.put(9, 5);
map.put(4, 6);
map.put(10, 0);
map.put(5, 1);
map.put(11, 3);
map.put(6, 4);
map.put(12, 5);
System.out.println();
for (int a = 0; a < col.length; a++) {
int counter = 1;
System.out.printf("%-6s%-6s%-6s%-6s%-6s%-6s%-6s", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
System.out.printf("\t\t\t"); //Formatting for days
}
// System.out.println();
// calculate leap year
boolean LeapYear = false;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) LeapYear = true;
// displaying month as String
ArrayList < Integer > dds = new ArrayList < Integer > ();
dds.add(0);
dds.add(0);
dds.add(0);
dds.add(0);
int indexofA = 0;
String yy = year + "";
int m;
if (LeapYear && month == 1) m = map.get(month) + 6;
else if (LeapYear && month == 2) m = map.get(month) - 1;
else m = map.get(month);
int y = Integer.parseInt(yy.substring(2, 4));
int c = (year / 100);
if (c % 4 == 0) c = 6;
else if (c % 4 == 1) c = 4;
else if (c % 4 == 2) c = 2;
else c = 0;
int x = y + y / 4 + c;
int d = (1 + x + m) % 7;
// cal.set(year, month, 0);
int length = (int)(30 + ((month + (month / 8.0)) % 2));
if (month == 2) {
if (LeapYear) {
length -= 1;
} else {
length -= 2;
}
}
System.out.print("len : " + length);
System.out.println();
boolean thirtyone = false;
int count = 0;
for (int r = 0; r < 6; r++) { //For each row of the calendars
String[] dayss = new String[7];
int index = 0;
for (int a = 0; a < col.length; a++) { //For each column of a calendar
for (int i = 0; i < d && dds.get(indexofA) == 0; i++) {
dayss[index] = " ";
index++;
}
for (day = dds.get(indexofA) + 1; day <= length; day++) {
if (day == 31) {
dayss[index] = day + "";
index++;
thirtyone = true;
} else if (day < 31) {
dayss[index] = day + "";
index++;
}
if (index == 7) {
dds.set(indexofA, day);
break;
}
}
indexofA++;
System.out.printf("%-6s%-6s%-6s%-6s%-6s%-6s%-6s", dayss[0], dayss[1], dayss[2], dayss[3],
dayss[4], dayss[5], dayss[6]);
System.out.printf("\t\t\t");
index = 0;
year++;
LeapYear = false;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) LeapYear = true;
yy = year + "";
if (LeapYear && month == 1) m = map.get(month) + 6;
else if (LeapYear && month == 2) m = map.get(month) - 1;
else m = map.get(month);
y = Integer.parseInt(yy.substring(2, 4));
c = (year / 100);
if (c % 4 == 0) c = 6;
else if (c % 4 == 1) c = 4;
else if (c % 4 == 2) c = 2;
else c = 0;
x = y + y / 4 + c;
d = (1 + x + m) % 7;
}
year -= 3;
yy = year + "";
LeapYear = false;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) LeapYear = true;
if (LeapYear && month == 1) m = map.get(month) + 6;
else if (LeapYear && month == 2) m = map.get(month) - 1;
else m = map.get(month);
y = Integer.parseInt(yy.substring(2, 4));
c = (year / 100);
if (c % 4 == 0) c = 6;
else if (c % 4 == 1) c = 4;
else if (c % 4 == 2) c = 2;
else c = 0;
x = y + y / 4 + c;
d = (1 + x + m) % 7;
indexofA = 0;
System.out.println();
}
}
System.out.print("\n\n" + "Enter another year (Y/N)? ");
ans = in .next();
} while (ans.equals("Y") || ans.equals("y"));
System.out.println("\nEnd!"); in .close();
}
}

How to get 10 per line -leap years

Please help with formatting my output.
I have been asked to "Write a program that displays all the leap years, ten per line, in the twenty-first century (from 2001 to 2100), separated by exactly one space".
Although I get the right results, it's not in the required format.
Thanks in advance
public class Leapyear {
public static void main(String[] args) {
//declare variabls;
int year;
int count=1;
int yearsperline = 10;
//loop
for(year=2001;2001<=2100;year++){
if((year%4==0 && year%100!=0) || (year%400==0))
System.out.print(year+",");
if ( year ==2100)
break;
while (count%10==0)
System.out.println();
}
}
}
You can write something like this:
//declare variables;
final int YEARS_PER_LINE = 10;
final int START_YEAR = 2001;
//loop
for(int year = START_YEAR; year <= 2100; year++){
System.out.print(year);
if((year - START_YEAR) % YEARS_PER_LINE == (YEARS_PER_LINE - 1)) {
System.out.println();
} else {
System.out.print(",");
}
}
Try this one:
public class LeapYear
{
public static void main(String[] args)
{
// declare variables
final int startYear = 2001;
final int endYear = 2100;
final int yearsPerLine = 10;
// loop
for (int year = startYear, count = 0; year <= endYear; year++)
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
if (count % yearsPerLine != 0)
System.out.print(" ");
else if (count > 0 && count % yearsPerLine == 0)
System.out.println();
System.out.print(year);
++count;
}
}
}
}
int startFromYear = 2001;
int stopAtYear = 2100;
int count = 0;
for(int i = startFromYear; i <= stopAtYear; i++){
if((i % 4 == 0 && i % 100 != 0)||(i % 400 == 0)){
System.out.print(i + " ");
count++;
if(count % 10 ==0)
System.out.println();
}
}
100% correct! and easy to understand.
public class LeapYears
{
public static void main(String[] args)
{
int count = 0;
for(int i = 2001; i <= 2100; i++)
{
if ((i % 4 == 0 && i % 100 != 0)||(i % 400 == 0))
{
count++;
//if count is 10 then start a new line.
if(count % 10 == 0)
{
System.out.println(i);
}
//if count is smaller 10 then in the same line
else
{
System.out.print(i + " ");
}
}
}
}
}

Java subtract months without Library

I want to subtract months from integer without any library.
The problem is that when I reduce 1 month from first month : 0 (January) it should be 12 (Dec) but it will be -1..
This is my code for adding
int currentMonthInt = Integer.parseInt(currentMonth) - 1;
int currentYearInt = Integer.parseInt(currentYear);
// show today month
if (dateposition == 0){
showListView(currentMonth, currentYear, db);
}
// show next month
for (int i = 1; i <=200; i++){
if (dateposition == i){
int month = currentMonthInt + i;
int year = currentYearInt + (month / 12);
month = (month % 12)+1;
String monthString = String.format("%02d", month);
String yearString = String.valueOf(year);
showListView(monthString, yearString, db);
}
}
and this is my code for subtracting : (but it dosen't work)
for (int i = -200; i < 0; i++){
//This is not correct!
//int month = currentMonthInt + i;
//int year = currentYearInt + (month / 12);
//month = (month % 12)+1;
String monthString = String.format("%02d", month);
String yearString = String.valueOf(year);
showListView(monthString, yearString, db);
}
P.S. dateposition is for position of month if its 0 its today months and year and if its +1 its next month and so on and if its -1 its prev month
OK I wrote it and I test it its worked!
for (int i = -200; i < 0; i++){
if (dateposition == i){
int month = currentMonthInt + i;
int year = currentYearInt + (month / 12);
if (month >= 0){
month = (month % 12)+1;
} else {
int c1 = Math.abs(month / 12) + 1;
month += (12 * c1);
}
String monthString = String.format("%02d", month);
String yearString = String.valueOf(year);
showListView(monthString, yearString, db);
}
}
Using Joda library
public void test() {
LocalDate fromDate = LocalDate.now();
System.out.println(fromDate);
LocalDate newYear = fromDate;
for (int i = 0; i < 10; i++) {
newYear = newYear.minusMonths(1);
System.out.print(newYear + ", ");
}
System.out.println("\n-------");
newYear = fromDate;
for (int i = 0; i < 10; i++) {
newYear = newYear.plusMonths(1);
System.out.print(newYear + ", ");
}
}
Output:
2014-10-20
2014-09-20, 2014-08-20, 2014-07-20, 2014-06-20, 2014-05-20, 2014-04-20, 2014-03-20, 2014-02-20, 2014-01-20, 2013-12-20,
-------
2014-11-20, 2014-12-20, 2015-01-20, 2015-02-20, 2015-03-20, 2015-04-20, 2015-05-20, 2015-06-20, 2015-07-20, 2015-08-20,

Compiling Issues, saying that it is illegal start and expecting ; on both the constructors

error: ';' expected
illegal start of expression
These are the two errors im getting within the program on both getGrid and checkGrid
Cannot seem to figure out what the problem is and what is causing it.
help would be appreciated
import java.util.Scanner;
public class Aleko_SudokuChecker
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("\nWelcome to the Sudoku Checker v1.0!\n");
System.out.print("\nThis program checks simple, small, 4x4 Sudoku grids for\n");
System.out.print("correctness. Each column, row and 2x2 region contains the numbers\n");
System.out.print("1 through 4 only once.\n\n");
System.out.print("To check your sudoku, enter your board one row at a time, with\neach digit separated by a space. Hit ENTER at the end of a row.\n");
SudokuChecker foo = new SudokuChecker();
foo.getGrid();
foo.checkGrid();
public void getGrid()
{
System.out.print("\nEnter Row 1: ");
int i = in.nextInt();
int j = in.nextInt();
int k = in.nextInt();
int m = in.nextInt();
System.out.print("Enter Row 2: ");
int n = in.nextInt();
int i1 = in.nextInt();
int i2 = in.nextInt();
int i3 = in.nextInt();
System.out.print("Enter Row 3: ");
int i4 = in.nextInt();
int i5 = in.nextInt();
int i6 = in.nextInt();
int i7 = in.nextInt();
System.out.print("Enter Row 4: ");
int i8 = in.nextInt();
int i9 = in.nextInt();
int i10 = in.nextInt();
int i11 = in.nextInt();
}
public void checkGrid()
{
System.out.print("\nThank you. Now checking ... \n");
int x = 0;
int i12 = i + j + n + i1;
int i13 = i4 + i5 + i8 + i9;
int i14 = k + m + i2 + i3;
int i15 = i6 + i7 + i10 + i11;
int i16 = i + j + k + m;
int i17 = n + i1 + i2 + i3;
int i18 = i4 + i5 + i6 + i7;
int i19 = i8 + i9 + i10 + i11;
int i20 = i + n + i4 + i8;
int i21 = j + i1 + i5 + i9;
int i22 = k + i2 + i6 + i10;
int i23 = m + i3 + i7 + i11;
if (i12 == 10)
{
System.out.print("\nREG-1:PASS");
}
else
{
System.out.print("\nREG-1:FAIL");
x = x+1;
}
if (i13 == 10)
{
System.out.print("\nREG-2:PASS");
}
else
{
System.out.print("\nReg-2:FAIL");
x = x +1;
}
if (i14 == 10)
{
System.out.print("\nREG-3:PASS");
}
else
{
System.out.print("\nREG-3:FAIL");
x = x+1;
}
if (i15 == 10)
{
System.out.print("\nREG-4:PASS\n");
}
else
{
System.out.print("\nREG-4:FAIL\n");
x = x+1;
}
if (i16 == 10)
{
System.out.print("\nROW-1:PASS");
}
else
{
System.out.print("\nROW-1:FAIL");
x = x+1;
}
if (i17 == 10)
{
System.out.print("\nROW-2:PASS");
}
else
{
System.out.print("\nROW-2:FAIL");
x = x+1;
}
if(i18 == 10)
{
System.out.print("\nROW-3:PASS");
}
else
{
System.out.print("\nROW-3:FAIL");
x = x+1;
}
if (i19 == 10)
{
System.out.print("\nROW-4:PASS\n");
}
else
{
System.out.print("\nROW-4:FAIL\n");
x = x+1;
}
if (i20 == 10)
{
System.out.print("\nCOL-1:PASS");
}
else
{
System.out.print("\nCOL-1:FAIL");
x = x+1;
}
if (i21 == 10)
{
System.out.print("\nCOL-2:PASS");
}
else
{
System.out.print("\nCOL-2:FAIL");
x = x+1;
}
if (i22 == 10)
{
System.out.print("\nCOL-3:PASS");
}
else
{
System.out.print("\nCOL-3:FAIL");
x = x+1;
}
if (i23 == 10)
{
System.out.print("\nCOL-4:PASS\n");
}
else
{
System.out.print("\nCOL-4:FAIL\n");
x = x+1;
}
if ((i12 == 10) && (i13 == 10) && (i14 == 10) && (i15 == 10) && (i16 == 10) && (i17 == 10) && (i18 == 10) && (i19 == 10) && (i20 == 10) && (i21 == 10) && (i22 == 10) && (i23 == 10)) {
System.out.print("\nCongratulations!\nThis Sudoku is valid.\n\n");
}
else
{
System.out.print("\nSorry.\nThis Sudoku is invalid.\n\n");
}
}
}
}
I think you need to close the main() method with a brace "}" after foo.checkGrid(); and before you start the new method public void getGrid().

Categories