How can i make multiple loops?

I am facing a problem with java array loops well, this is the problem

Use the space below to write main method that reads 5 integer values into an array named list. Then, use the array to output the positive values followed by the negative values and ignore zeros. Sample Output Enter 5 numbers: 10 -3 0 -4 9 List: 10 9 -4

and this is the answer:

int [] list = new int[5];
// 1pt
Scanner read = new Scanner (System.in);

// 3pts
System.out.println("Enter 5 numbers:");
for(int i=0; i<list.length;i++)
 list[i] = read.nextInt();

// 3 pts
for(int i=0; i<list.length; i++)
 if(list[i] > 0)
 System.out.println(list[i]);

// 3 pts
for(int i=0; i<list.length; i++)
 if(list[i] < 0)
 System.out.println(list[i]);

but i do not know how to make them work together if you get me

I guess You should use continue to exclude 0. And than You don’t need 3 loops. To make a loop in loop You should use brackets.

1 Like

I think you can use a single for-loop and maintain the list of numbers in a string.

String positiveNumbers = "";
String negativeNumbers = "";
for (int i = 0; i < 5; i++) {
  // here you will spend numbers to the relevant
  // variables
} 
// here you will print the values of the strings.

One of my teachers used to say that if you can solve a certain problem yourself, you can make a computer do it too; you just need to break down your approach into smaller steps.

1 Like

Here what i was thinking yesterday, also solution

for (int i = 0; i < list.length; i++) {
                if (list[i] == 0) {
                    continue;
                }
                for (int j = i + 1; j < list.length; j++){
                    int temp;

                    if (list[i] < list[j]){
                        temp = list[i];
                        list[i] = list[j];
                        list[j] = temp;
                    }
                }
                System.out.println(list[i]);
1 Like

As a professional software developer with over 25 years of experience let me give you a little bit of advice:

Do NOT overly complicate your code… (Google the KISS principal if you’re not familiar)

@Sarah’s initial answer is by far the best way to go with this simple case. It’s WAY more readable and understandable and therefore WAY easier to maintain. Too many people write code that’s slick and/or complicated at the expense of maintainability.

Write code with efficiency in mind absolutely. But eeking out a single millisecond of performance in this kind of an app is unnecessary and ultimately a waste of time initially and a maintenance nightmare afterwards. Address performance bottlenecks when you KNOW they exist.

Not only that but it introduces errors as well. @Gintsm’s answer is both inefficient and incorrect for example.

The problem is that the inner loop iterates 24 times AND introduces a temp variable with assignment. Furthermore, this is a basic sort (which wasn’t required) which is entirely dependent on the initial input order (so it only coincidentally sorts).

By comparison, @Sarah’s original answer only iterates 10 times (5 per for loop) - which is more efficient then?

AND sorted output was never even required…

Finally to answer @Sarah’s original question which (academically) asks how to combine for loops (if I understand correctly), then @Gintsm’s first answer is correct - you can nest loops by using brackets. eg:

for (int i = 0; i < list.length; i++)
{
   for (int j = 0; j < list.length; j++)
   {
      ...
   }
}

In fact I would recommend using {}'s for ALL control blocks because it’s another common source of bugs.

Too many times I’ve seen someone write

if (a > b) 
   do something

Then later they realize that they need to add a statement:

if (a > b)
   do something
   do something else

and BOOM because now they need brackets:

if (a > b)
{
   do something
   do something else
}

So if you always start with:

if (a > b)
{
   do something
}

you’ll never make that mistake

1 Like
class Main {
  public static void main(String[] args) {

    int weeks = 3;
    int days = 7;

    // outer loop prints weeks
    for (int i = 1; i <= weeks; ++i) {
      System.out.println("Week: " + i);

      // inner loop prints days
      for (int j = 1; j <= days; ++j) {
        System.out.println("  Day: " + j);
      }
    }
  }
}

Sales Training Programs in India