Solved

how to terminate printing an array of ints

Posted on 2004-09-29
10
201 Views
Last Modified: 2010-03-31
Hello,

I have this array of 0s and 1s. Originally the array is initialized to all 1s, and then using this method:

public static int bitSwap(int[] swapArray)
{
      
for (k=0; k < swapArray.length; k++)
{

if (swapArray[k] == 0)
{
swapArray[k] = 1;
}
else
{

swapArray[k] = 0;
j=k;
break;
}

}

It switches 1s to 0s and 0s to 1s, but once a 1 has been flipped to 0, the code breaks. This method returns the index at which the 0 was flipped, which is what i need it to do. The user passes an integer value, which determines the size of the array that is passed to this method. My question is this:

I need to print the return statement of this method in a loop of some kind. I know that when the array passed contains ALL 0s in it's indices, then it needs to stop, but I can't figure out how to make it do that.
Help would be appreciated.

-NeedlessKane
0
Comment
Question by:NeedlessKane
  • 5
  • 4
10 Comments
 
LVL 92

Accepted Solution

by:
objects earned 25 total points
ID: 12184916
heres a method to print the array contents.:

public static void print(int[] a)
{
   for (int i=0; i<a.length; i++) System.out.print(a[i]+" ");
   System.out.println();
}
0
 
LVL 92

Expert Comment

by:objects
ID: 12184933
use something like the following in your loop to display the processing:

print(a);
int result = bitSwap(a);
System.out.println(result);
0
 

Author Comment

by:NeedlessKane
ID: 12185003
Yeah, but I need to keep printing the values of bitSwap until the array that is passed is full of 0s, how can I make it check to see when the array is all 0s?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 92

Expert Comment

by:objects
ID: 12185047
you'll need to check the array contents after each call.

boolean allzero = false;
for (int i=0; allzeroes && i<a.length; i++) if (a[i]==1) allzeroes = false;
0
 

Author Comment

by:NeedlessKane
ID: 12185192
Hrmm, still working w/ it. How exactly does that && statement work in the for loop? Like what's the logic behind that.
0
 
LVL 92

Expert Comment

by:objects
ID: 12185213
&& is an and, so the expression is only true if both the expressions are true
0
 

Author Comment

by:NeedlessKane
ID: 12185400
This is what i'm trying and it's not working:

int d;
boolean allzero = true;
for (d=0; allzero && d<diskMove.length; d++)
bitSwap(diskMove);
if (diskMove[d]==1)
allzero = false;
else{
System.out.println(diskMove[d]);
     }


0
 

Author Comment

by:NeedlessKane
ID: 12185846
Still not working, i've tried alot of things, agh.

All i need to do is run that bitSwap() in a loop and stop it each time....then after it stops, check the current array to see if all the values are == 0, if they aren't == to 0, run bitSwap again, and keep doing that over and over, but i'm at a loss.
0
 
LVL 3

Assisted Solution

by:CI-Ia0s
CI-Ia0s earned 25 total points
ID: 12186209
I'd add "allzero = true;" to the top of the else statement:

int d;
boolean allzero = true;
for (d=0; allzero && d<diskMove.length; d++)
bitSwap(diskMove);
if (diskMove[d]==1)
allzero = false;
else{
allzero = true;
System.out.println(diskMove[d]);
}
0
 
LVL 92

Expert Comment

by:objects
ID: 12186854
>  if they aren't == to 0, run bitSwap again, and keep doing that over and over, but i'm at a loss

your call to bitSwap needs to be outside your loop.

> I'd add "allzero = true;" to the top of the else statement

Not really necessary, as allzero is already true.

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unhandled exception type Exception 18 42
Glassfish admin console not working 1 34
Is there a simpler dropbox system? 10 34
passing enum to a method 4 23
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question