# Testing values in a nest if/else structure not working.

Page 1 of 1

## 11 Replies - 800 Views - Last Post: 07 October 2010 - 06:14 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=193809&amp;s=2f0e7dd50c8b98e587b129ac4138fc26&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 maindric

Reputation: 4
• Posts: 13
• Joined: 06-October 10

# Testing values in a nest if/else structure not working.

Posted 06 October 2010 - 05:19 PM

import java.util.Scanner;

public class deddy_Sudoku {
public static void main(String args[]) {
System.out
.printf("\t\t******Welcome to check some four by four sudoku!******\n\nThis version of sudoku is programmed by Donn Eddy in UNO CIST 1400, it is to,\nin my oppinion, allow some interest and applicability of what we are learning.\n\nAll you do is enter 1-4 in the row with spaces seperating the values,\nand we check to see whether or not it is valid.  It is that simple\nA sample would be:\n\t\tPlease enter row 1: 1 2 3 4\n\nIt is that easy!  Have fun!\n\n");
// Intro lines, tells the user what this is and how to use it.

// *****Variable declarations********
Scanner input = new Scanner(System.in);
int a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4, d1, d2, d3, d4, valid = 1;

// Get the first row.
while (valid == 1) {
a1 = input.nextInt();
if (a1 > 4 || a1 < 0) {
valid = 1;
System.out.printf("%d is not valid, please try again.\n", a1);
} else {
a2 = input.nextInt();
if (a2 > 4 || a2 < 0) {
valid = 1;
System.out.printf("%d is not valid, please try again.\n",
a2);
} else {
a3 = input.nextInt();
if (a3 > 4 || a3 < 0) {
valid = 1;
System.out.printf(
"%d is not valid, please try again.\n", a3);
} else {
a4 = input.nextInt();
if (a4 > 4 || a4 < 0) {
valid = 1;
System.out.printf(
"%d is not valid, please try again.\n", a4);
} else {
valid = 0;
}
}
}
}
}

valid = 1; // Lets the next while enter.

// Get the second row.
while (valid == 1) {
b1 = input.nextInt();
if (b1 > 4 || b1 < 0) {
valid = 1;
System.out.printf("%d is not valid, please try again.\n", b1);
} else {
b2 = input.nextInt();
if (b2 > 4 || b2 < 0) {
valid = 1;
System.out.printf("%d is not valid, please try again.\n",
b2);
} else {
b3 = input.nextInt();
if (b3 > 4 || b3 < 0) {
valid = 1;
System.out.printf(
"%d is not valid, please try again.\n", b3);
} else {
b4 = input.nextInt();
if (b4 > 4 || b4 < 0) {
valid = 1;
System.out.printf(
"%d is not valid, please try again.\n", b4);
} else {
valid = 0;
}
}
}
}
}

valid = 1; // Lets the next while enter.

// Get the third row.
while (valid == 1) {
c1 = input.nextInt();
if (c1 > 4 || c1 < 0) {
valid = 1;
System.out.printf("%d is not valid, please try again.\n", c1);
} else {
c2 = input.nextInt();
if (c2 > 4 || c2 < 0) {
valid = 1;
System.out.printf("%d is not valid, please try again.\n",
c2);
} else {
c3 = input.nextInt();
if (c3 > 4 || c3 < 0) {
valid = 1;
System.out.printf(
"%d is not valid, please try again.\n", c3);
} else {
c4 = input.nextInt();
if (c4 > 4 || c4 < 0) {
valid = 1;
System.out.printf(
"%d is not valid, please try again.\n", c4);
} else {
valid = 0;
}
}
}
}
}

valid = 1; // Lets the next while enter.

// Get the fourth row.
while (valid == 1) {
d1 = input.nextInt();
if (d1 > 4 || d1 < 0) {
valid = 1;
System.out.printf("%d is not valid, please try again.\n", d1);
} else {
d2 = input.nextInt();
if (d2 > 4 || d2 < 0) {
valid = 1;
System.out.printf("%d is not valid, please try again.\n",
d2);
} else {
d3 = input.nextInt();
if (d3 > 4 || d3 < 0) {
valid = 1;
System.out.printf(
"%d is not valid, please try again.\n", d3);
} else {
d4 = input.nextInt();
if (d4 > 4 || d4 < 0) {
valid = 1;
System.out.printf(
"%d is not valid, please try again.\n", d4);
} else {
valid = 0;
}
}
}
}
}
System.out.printf("\n"); // Makes the output look nicer.
}
}

Hi, I am new here, and I consider myself not as a complete beginner in programming, but not really skilled.

I have a class assignment, no, I'm not asking for code. I am encountering a bug I cannot seem to debug.

This snippet takes 4 integers into in 4 rows, and the values must be between 1-4 separated with spaces. I then check each value to make sure they are between 1 and 4.

Sometimes, when I run the code, it works fine, but sometimes, it just takes an incorrect value and continues anyways, sometimes it takes a couple times trying to force it to fail, and sometimes just doesn't work right.

valid==1 then it loops over again, and is invalid.

valid==0 then it goes on, and is valid.

I wanted to use arrays, but the instructor says no, so that is why I use a1, a2...d3, d4.

Example of how output should be:
Please enter row 1: 1 2 3 4
Please enter row 2: 1 5 3 4
5 is not valid, please try again
Please enter row 2: 1 2 3 4

and etc.

What I get sometimes:
Please enter row 1: 1 2 34

It does not make sense to me... Maybe you guys can make sense of it...

One more quick question, not on topic with the thread's point, but my newbness. Why, with some of my printf statements, why does it sometimes appear in preview?
System.out.printf(
"Blah", foo, bar);

Is This A Good Question/Topic? 0

## Replies To: Testing values in a nest if/else structure not working.

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Testing values in a nest if/else structure not working.

Posted 06 October 2010 - 05:34 PM

You are far away from OO programming and you haven't finish
Look at all the code you have to check 4 rows...

You first have to learn out to use arrays then you can do

int[][] grid = new int[4][4];

// loop for 4 rows
for(int row = 0; row < 4; row++) {
// then loop for each element column in the row
for(int col = 0; col < 4; col++) {
// single input code here
}
}

### #3 maindric

Reputation: 4
• Posts: 13
• Joined: 06-October 10

## Re: Testing values in a nest if/else structure not working.

Posted 06 October 2010 - 05:37 PM

As I have stated in the OP, I would have loved to use Arrays but the teacher will not allow it. We have to use what she taught up to now.

The rest of the code, I can work out. I just posted what the bug I am experiencing lies. I can't seem to see why it is leaving the while loop. I tried to go line by line, but I am not finding what is wrong with this snippet causing it to leave the loop.

I took a peak at what you wrote, and that is how I would have done it honestly, but with my instructor's limits, she will mark off points if I do that.

This post has been edited by maindric: 06 October 2010 - 05:40 PM

### #4 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: Testing values in a nest if/else structure not working.

Posted 06 October 2010 - 06:12 PM

How 'bout writing a function that returns one valid digit. Then you could do

...
a1 = getDigit();
a2 = getDigit();
a3 = getDigit();
a4 = getDigit();

b1 = getDigit();
...

This post has been edited by n8wxs: 06 October 2010 - 06:13 PM

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Testing values in a nest if/else structure not working.

Posted 06 October 2010 - 06:18 PM

Forget your else, if the value is not valid you pass to the following number
If an eror occur you will start all over again
better to do it that way

// Get the first row.
boolean valid = false;
while(!valid) {
a1 = input.nextInt();
valid = a1 >= 0 && a1 <= 4;
if (!valid)
System.out.printf("%d is not valid, please try again.\n", a1);
}

boolean valid = false;
while(!valid) {
a2 = input.nextInt();
valid = a2 >= 0 && a2 <= 4;
if (!valid)
System.out.printf("%d is not valid, please try again.\n", a2);
}

boolean valid = false;
while(!valid) {
a3 = input.nextInt();
valid = a3 >= 0 && a3 <= 4;
if (!valid)
System.out.printf("%d is not valid, please try again.\n", a3);
}

boolean valid = false;
while(!valid) {
a4 = input.nextInt();
valid = a4 >= 0 && a4 <= 4;
if (!valid)
System.out.printf("%d is not valid, please try again.\n", a4);
}

n8wxs, on 06 October 2010 - 07:12 PM, said:

How 'bout writing a function that returns one valid digit. Then you could do

...
a1 = getDigit();
a2 = getDigit();
a3 = getDigit();
a4 = getDigit();

b1 = getDigit();
...

Actually this is a good idea and if the number can be only once you can write 4 getDigit() with a different signature
a1 = getDigit();
a2 = getDigit(a1);
a3 = getDigit(a1, a2);
a4 = getDigit(a1, a2, a3);

### #6 maindric

Reputation: 4
• Posts: 13
• Joined: 06-October 10

## Re: Testing values in a nest if/else structure not working.

Posted 06 October 2010 - 06:57 PM

@nbl

Thanks for that. I am checking with the instructor whether or not I am able to use booleans (I know how to do a lot of this stuff, but her limits saying we cannot go beyond what she taught in class is making simple things difficult..). For now, I will leave it as an integer and make a 0 / 1 flag.

@n8wxs

Thanks for the idea, but as I told nbl, I am not allowed to go away from what my instructor taught so far, and she has yet to get to methods outside of the main method.

If anyone wants to see the assignment document, it is attached.

#### Attached File(s)

This post has been edited by maindric: 06 October 2010 - 07:00 PM

### #7 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: Testing values in a nest if/else structure not working.

Posted 06 October 2010 - 08:09 PM

Quote

Allow your user to enter their Sudoku grids row-by-row, separating each of the four values by a
space and hitting ENTER at the end of the row.

I would recommend using a String to hold the output of Scanner.nextLine()

Using pbl's example of a while() loop for each line, parse the string contents to see there are 4 legal digits. If there are, then you can use a second scanner in the while() loop, initialized from the string, to output the digits into your integer variables.

See the scanner constructor: Scanner

EDIT:

I just read the rest of the requirements. Sigh.

Quote

Write a straightforward (yet lengthy) program that utilizes the if/else selection
structure quite heavily.

You'll use a lot of if/else doing the validation stuff.

This post has been edited by n8wxs: 06 October 2010 - 08:34 PM

### #8 maindric

Reputation: 4
• Posts: 13
• Joined: 06-October 10

## Re: Testing values in a nest if/else structure not working.

Posted 06 October 2010 - 08:47 PM

Update, I found the bug.

In some of the if statements, I forgot to add a space between the if and the (). IE. if() vs if ().

Now, not a bug, but something I want to fix.

When I enter an invalid number, and you put 5 6 7 8, it is correct and bring up error in all of it, and cycles through. It would show:
5 is invalid
Please enter values: 6 is invalid
Please enter values: 7 is invalid
Please enter values: 8 is invalid

How do I clear out the rest of the entries right after it detects it's first error? (I fixed the bug in my initial code.)

@n8wxs
I would try that, but the instructor has not taught that yet to the class. I know you don't know what she has and hasn't, please don't get frustrated with me. **EDIT** I just saw your edit, and yeah, her limitations do get frustrating. Thanks for understanding.

**EDIT**
My recent code:
import java.util.Scanner;

public class deddy_Sudoku
{
public static void main ( String args[] )
{
System.out.printf("\t\t******Welcome to check some four by four sudoku!******\n\nThis version of sudoku is programmed by Donn Eddy in UNO CIST 1400, it is to,\nin my oppinion, allow some interest and applicability of what we are learning.\n\nAll you do is enter 1-4 in the row with spaces seperating the values,\nand we check to see whether or not it is valid.  It is that simple\nA sample would be:\n\t\tPlease enter row 1: 1 2 3 4\n\nIt is that easy!  Have fun!\n\n");
//Intro lines, tells the user what this is and how to use it.

//*****Variable declarations********
Scanner input = new Scanner(System.in);
int a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4, d1, d2, d3, d4, valid=1;

//Get the first row.
while(valid==1)
{
a1=input.nextInt();
if (a1>4 || a1<1)
{
valid=1;
System.out.printf("%d is not valid, please try again.\n", a1);
}
else
{
a2=input.nextInt();
if (a2>4 || a2<1)
{
valid=1;
System.out.printf("%d is not valid, please try again.\n", a2);
}
else
{
a3=input.nextInt();
if (a3>4 || a3<1)
{
valid=1;
System.out.printf("%d is not valid, please try again.\n", a3);
}
else
{
a4=input.nextInt();
if (a4>4 || a4<1)
{
valid=1;
System.out.printf("%d is not valid, please try again.\n", a4);
}
else
{
valid=0;
}
}
}
}
}

valid=1; //Lets the next while enter.

//Get the second row.
while(valid==1)
{
b1=input.nextInt();
if (b1>4 || b1<1)
{
valid=1;
System.out.printf("%d is not valid, please try again.\n", b1);
}
else
{
b2=input.nextInt();
if (b2>4 || b2<1)
{
valid=1;
System.out.printf("%d is not valid, please try again.\n", b2);
}
else
{
b3=input.nextInt();
if (b3>4 || b3<1)
{
valid=1;
System.out.printf("%d is not valid, please try again.\n", b3);
}
else
{
b4=input.nextInt();
if (b4>4 || b4<1)
{
valid=1;
System.out.printf("%d is not valid, please try again.\n", b4);
}
else
{
valid=0;
}
}
}
}
}

valid=1; //Lets the next while enter.

//Get the third row.
while(valid==1)
{
c1=input.nextInt();
if (c1>4 || c1<1)
{
valid=1;
System.out.printf("%d is not valid, please try again.\n", c1);
}
else
{
c2=input.nextInt();
if (c2>4 || c2<1)
{
valid=1;
System.out.printf("%d is not valid, please try again.\n", c2);
}
else
{
c3=input.nextInt();
if (c3>4 || c3<1)
{
valid=1;
System.out.printf("%d is not valid, please try again.\n", c3);
}
else
{
c4=input.nextInt();
if (c4>4 || c4<1)
{
valid=1;
System.out.printf("%d is not valid, please try again.\n", c4);
}
else
{
valid=0;
}
}
}
}
}

valid=1; //Lets the next while enter.

//Get the fourth row.
while(valid==1)
{
d1=input.nextInt();
if (d1>4 || d1<0)
{
valid=1;
System.out.printf("%d is not valid, please try again.\n", d1);
}
else
{
d2=input.nextInt();
if (d2>4 || d2<1)
{
valid=1;
System.out.printf("%d is not valid, please try again.\n", d2);
}
else
{
d3=input.nextInt();
if (d3>4 || d3<1)
{
valid=1;
System.out.printf("%d is not valid, please try again.\n", d3);
}
else
{
d4=input.nextInt();
if (d4>4 || d4<1)
{
valid=1;
System.out.printf("%d is not valid, please try again.\n", d4);
}
else
{
valid=0;
}
}
}
}
}
System.out.printf("\n"); //Makes the output look nicer.
}
}

This post has been edited by maindric: 06 October 2010 - 08:56 PM

### #9 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Testing values in a nest if/else structure not working.

Posted 06 October 2010 - 09:07 PM

maindric, on 06 October 2010 - 09:47 PM, said:

Update, I found the bug.

In some of the if statements, I forgot to add a space between the if and the (). IE. if() vs if ().

That was surely the NOT the problem
this statements are all legal and equivalent Java ignores the spaces

if(a == B)/>
if    (a        == B)/>

if

{

a

==

B

)

This post has been edited by pbl: 06 October 2010 - 09:09 PM
Reason for edit:: oups last b should be B

### #10 maindric

Reputation: 4
• Posts: 13
• Joined: 06-October 10

## Re: Testing values in a nest if/else structure not working.

Posted 06 October 2010 - 09:29 PM

@pbl

Your right, I still experience it.

As I said before, it happens kind of randomly. I just didn't experience it in about 10 attempts, but it came back...

### #11 maindric

Reputation: 4
• Posts: 13
• Joined: 06-October 10

## Re: Testing values in a nest if/else structure not working.

Posted 07 October 2010 - 01:21 PM

For some reason, it will not let me edit my previous post, so I have to give this update in a reply with the dreaded double post, I apologize for that.

I found a work around. I un-nested the if/else statements, and made them standalone.

//Get the first row.
while (valid<4)
{
valid=0; //Resets valid to prevent errors.

a1=input.nextInt();
if (a1 >=1 && a1 <=4)
{
valid++; //If vaild, increment.
}
else
{
System.out.printf("%d is invalid, please enter good data.\n", a1); //If invalid, tell data is invalid, and they need to reenter. Will not exit loop enless valid==4.
}

a2=input.nextInt();
if (a2 >=1 && a2 <=4)
{
valid++;
}
else
{
System.out.printf("%d is invalid, please enter good data.\n", a2);
}

a3=input.nextInt();
if (a3 >=1 && a3 <=4)
{
valid++;
}
else
{
System.out.printf("%d is invalid, please enter good data.\n", a3);
}

a4=input.nextInt();
if (a4 >=1 && a4 <=4)
{
valid++;
}
else
{
System.out.printf("%d is invalid, please enter good data.\n", a4);
}
}

valid=0;

### #12 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Testing values in a nest if/else structure not working.

Posted 07 October 2010 - 06:14 PM

If the user enter a wrong value it is kind of useless to ask him for the other ones as he will have to reenter them anyway better to start all over again at that moment

//Get the first row.
while (valid<4)
{
valid=0; //Resets valid to prevent errors.

a2=input.nextInt();
if (a2 >=1 && a2 <=4)
{
valid++;
}
else
{
System.out.printf("%d is invalid, please enter good data.\n", a2);
continue;  // <-------
}

}

Another solution would be to initalize a1, a2, a3, and a4 to -1
and reset them to -1 if the number entered is not correct
so in your while you can check if(a2 != -1).. do not prompt again for the value