Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java simple method question?
New Posts  All Forums:Forum Nav:

Java simple method question?

post #1 of 6
Thread Starter 
Why am I getting duplicate output? At the end of step 2, i put a condition that if a list = 9, then it should invoke method print which prints a 9x9 2D array. For whatever reason, if the program has to repeat a method, then when it finally does meet the condition to inovke the 'print' method in step 2, it always prints at least one 9x9 array and it even sometimes prints 3 or 4 9x9 arrays of the same information.

The CODE....

public void step1()
{
Collections.shuffle(list19);

int x = 0;

for(int row = 0; row < 3; row++)
for(int column = 0; column < 3; column++)
{
array[row][column] = list19.get(x);
x++;
}

step2();
}


public void step2()
{

bank.clear();

for(int i = 0; i < 3; i++)
{
for(int x = 0; x < 3; x++)
{
pass((i % 3), (i + 1), 0, 3);

if(temp.isEmpty() == true)
{
redux( 2 );
break;
}
else
assignment((i % 3), (x + 3));
}
}

if(bank.size() == 9)
print();
} // end method step2


public void redux(int r)
{
switch( r )
{
case 2:
{
System.out.println("recalled to step 1");
step1();
break;
}
}
}

public void print()
{
for(int row = 0; row < array.length; row++)
for(int column = 0; column < array[row].length; column++)
{
System.out.print(array[row][column] + " ");

if(column == 8)
System.out.println();
}

}


The OUTPUT...

recalled to step 1
8 3 9 4 1 7 0 0 0
1 2 7 6 5 8 0 0 0
5 4 6 3 9 2 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0

8 3 9 4 1 7 0 0 0
1 2 7 6 5 8 0 0 0
5 4 6 3 9 2 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
post #2 of 6
Still in college, so not a pro but I think.

when you do:
Code:
if(temp.isEmpty() == true)
{
redux( 2 );
break;
}
else
assignment((i % 3), (x + 3));
}
}

if(bank.size() == 9)
print();

As far as I know break only works for do and while loops, you don't stop anything there.
You call redux(2) and then you print, after the print, it starts all over again and will keep printing aslong as temp is empty.
so while temp is empty you do something like this:

step1
step2
temp is empty
call redux
print
step1
step2
temp is empty
call redux
print
step1
step2
temp is not empty
print

understand? tongue.gif
My System
(28 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 950 X58 Extreme NVIDIA GeForce GTX 760 NVIDIA GeForce GTX 760 
GraphicsRAMRAMRAM
NVIDIA GeForce GTX 460 Kingston  Kingston  Kingston  
RAMRAMRAMHard Drive
Kingston  Kingston  Kingston  2 TB Samsung HDD 
Hard DriveCoolingCoolingOS
OCZ 120 GB Agility 3 SSD Corsair Cooling Hydro Series H80 Scythe Kaze Fan Controller Windows 7 Ultimate 64bit 
MonitorMonitorMonitorKeyboard
3x BenQ XL2420  Philips HDTV 2x Fujitsu Siemens 24" Razer Lycosa Mirror Edition 
PowerCaseMouseMouse Pad
Antec Truepower TP-750 Cooler Master HAF 912 Plus Razer Deathadder Razer Vespula 
AudioAudioAudio
Muse Pocket LT3  Razer Megalodon Logitech Dolby 5.1 
  hide details  
Reply
My System
(28 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 950 X58 Extreme NVIDIA GeForce GTX 760 NVIDIA GeForce GTX 760 
GraphicsRAMRAMRAM
NVIDIA GeForce GTX 460 Kingston  Kingston  Kingston  
RAMRAMRAMHard Drive
Kingston  Kingston  Kingston  2 TB Samsung HDD 
Hard DriveCoolingCoolingOS
OCZ 120 GB Agility 3 SSD Corsair Cooling Hydro Series H80 Scythe Kaze Fan Controller Windows 7 Ultimate 64bit 
MonitorMonitorMonitorKeyboard
3x BenQ XL2420  Philips HDTV 2x Fujitsu Siemens 24" Razer Lycosa Mirror Edition 
PowerCaseMouseMouse Pad
Antec Truepower TP-750 Cooler Master HAF 912 Plus Razer Deathadder Razer Vespula 
AudioAudioAudio
Muse Pocket LT3  Razer Megalodon Logitech Dolby 5.1 
  hide details  
Reply
post #3 of 6
Thread Starter 
thanks for your reply. i'm going to try and review the code tonight or tomorrow and will post the solution.
post #4 of 6
not sure what you're trying to accomplish as this is only a fragment of your app.
But if the only reference to your print() method is in step 2, then thats your issue.
Look at the logic and enure you always want to check for bank.size == 9 for all your cases.
Code:
public void step2() {
   bank.clear();
   for(int i = 0; i < 3; i++) {
      for(int x = 0; x < 3; x++) {
         pass((i % 3), (i + 1), 0, 3);

         if(temp.isEmpty()) {
           redux( 2 );
         } else {
           assignment((i % 3), (x + 3));
         }
      }
   }
   
   System.out.println(">Bank size = " + bank.size() + " : should it be?");
   
   if(bank.size() == 9)
     print();
   }
}

Edited by mjoc13 - 1/11/12 at 6:11pm
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k Asus P8Z68-V eVga 8800 gts sc 320 Kingston HyperX 8GB 1600 
Hard DriveOptical DriveOSMonitor
wd 320 sataII, WD 250 PATA, WD 2TB green LG HT w7 x64 Sammy 22 ws LCD 
PowerCase
Corsair HX620W Coolmaster Elite 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k Asus P8Z68-V eVga 8800 gts sc 320 Kingston HyperX 8GB 1600 
Hard DriveOptical DriveOSMonitor
wd 320 sataII, WD 250 PATA, WD 2TB green LG HT w7 x64 Sammy 22 ws LCD 
PowerCase
Corsair HX620W Coolmaster Elite 
  hide details  
Reply
post #5 of 6
Thread Starter 
Quote:
Originally Posted by mjoc13 View Post

not sure what you're trying to accomplish as this is only a fragment of your app.
But if the only reference to your print() method is in step 2, then thats your issue.
Look at the logic and enure you always want to check for bank.size == 9 for all your cases.


i am creating a game Sudoku. i've only copied a fragment of my code to illustrate what my problem is, which is duplicate output if the code has to invoke previous steps. Instead of trying to explain it all, here's a brief illustration of how I have my code setup...

(grid 1)__(grid 2)

step 1__step 2

1 2 3__4 5 6 row 1
4 5 6__1 2 3 row 2
7 8 9__X X X row 3

step 1 shuffles a list numbers and then assigns those numbers to array row 0 (all the numbers in grid 1)

step 2 takes the numbers in grid 1: row 1, and then removes those numbers from a list of "temporary" numbers, 1 - 9. after a "new" list of available numbers from grid 1: row 1, has been determined, the program picks one of those numbers at random and assigns the first element in array row 1 (1,0)

so the list of temporary numbers to pick from for the first iteration of the loops in step 2 are
-before 1, 2, 3, 4, 5, 6, 7, 8, 9
-after 4, 5, 6, 7, 8, 9

after each iteration of the loops, the number assigned to the array is added to "the bank", so for the second iteration of the loop, the list of temporary numbers is determined once again, and then whatever numbers are in the bank are also removed before any number is pick and assigned

-number(s) in bank = 4
-before 1, 2, 3, 4, 5, 6, 7, 8, 9
-after 5, 6, 7, 8, 9

so looking at the grid above, you can see that after a few iterations, there are NO available numbers to pick from since the numbers 1 - 9 are already used which means temp.IsEmpty() is true because list 'temp' has no numbers stored


row 3 = 7, 8, 9
numbers in bank = 4, 5, 6, 1, 2, 3
.....after numbers in row 3 and the numbers in the bank are removed, there are no numbers to fill the "temporary" list of numbers

at this point, the numbers in the bank are 4, 5, 6, 1, 2, 3 which equals a size of 6 and NOT 9, so when the program has to cycle through the steps until it can assign all of the numbers as according to the rules of Sudoku, the program should only print once when the bank's size equals 9 in step 2. Anyhow, i'm still at a loss why it prints duplicate output, and sometimes even up to 9 grids.
Code:
public void step1()
    {
        Collections.shuffle(list19);
        
        int x = 0;
        
        for(int row = 0; row < 3; row++)
            for(int column = 0; column < 3; column++)
            {
                array[row][column] = list19.get(x);
                x++;
            }
        
        step2();
    } // end method step1
    

    // create quadrant #2
    public void step2()
    {
        
        bank.clear();

        // fill quadrant #2
        for(int i = 0; i < 3; i++)
        {
            for(int x = 0; x < 3; x++)
            {
                pass((i % 3), (i + 1), 0, 3);
                
                if(temp.isEmpty() == true)
                {
                    redux( 2);
                    break;
                }
                else
                    assignment((i % 3), (x + 3));
            }
        }
        
        if(bank.size() == 9)
            print();
    } // end method step2
  

    public void assignment(int a, int b)
    {
        array[a][b] = temp.get(0);
        bank.add(temp.get(0));
    } // end method assignment
    
    
    public void redux(int r)
    {
        switch( r )
        {
            case 2:
            {
                System.out.println("recalled to step 1");
                step1();
                break;
            }
            
            case 3:
            {
                System.out.println("recalled to step 2");
                step2();
                break;
            }
        }
    }
    
    
    // print Sudoku array
    public void print()
    {
        for(int row = 0; row < array.length; row++)
            for(int column = 0; column < array[row].length; column++)
            {
                System.out.print(array[row][column] + " ");
                
                if(column == 8)
                    System.out.println();
            }
        
    } // end method print

Edited by starcrafted - 1/11/12 at 8:32pm
post #6 of 6
Thread Starter 
I figured it out by running a simpler version of the code, but still have absolutely no reason why I was getting duplicate output

here is the code that was giving me duplicate output
Code:
public void step2()
    {
        
        bank.clear();

        // fill quadrant #2
        for(int i = 0; i < 3; i++)
        {
            for(int x = 0; x < 3; x++)
            {
                pass((i % 3), (i + 1), 0, 3);
                
                if(temp.isEmpty() == true)
                {
                    redux( 2);
                    break;
                }
                else
                    assignment((i % 3), (x + 3));
            }
        }
        
        if(bank.size() == 9)
            print();
    } // end method step2

here is a revision of the code that prevents duplicate output
Code:
public void step2()
    {
        
        bank.clear();

        // fill quadrant #2
        for(int i = 0; i < 3; i++)
        {
            for(int x = 0; x < 3; x++)
            {
                pass((i % 3), (i + 1), 0, 3);
                assignment((i % 3), (x + 3));
            }
            
        }

        System.out.println(bank.size());
        
        if(bank.size() == 9)
            print();
        else
            step1();
       
    } // end method step2
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java simple method question?