[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

how to terminate printing an array of ints

Posted on 2004-09-29
10
Medium Priority
?
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 92

Accepted Solution

by:
objects earned 100 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 100 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

650 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