how to use Array with if else statements?

ein

I have this code:

 String[] sweetFlevors = { "Caramel", "Cinnamon", "Wetermelon", "Backed Beans" };

 String[] savoryFlavors = { "Sea Salt", "Potato Chip", "Carrot", "Barbque" };

 int[] numberRows = { 1, 2, 3, 4 };
 int[] numberCol = { 1, 2 };

 for (int col = 0; col < numberCol.length; col++) {
     System.out.println("       " + numberCol[0] + "         " + numberCol[1]);

     for (String sweet: sweetFlevors) {
         for (String savory: savoryFlavors) {

             for (int row = 0; row < numberRows.length; row++) {
                 System.out.print(numberRows[row] + ". ");

                 System.out.println(sweet + " and " + savory);
             }
         }
     }
 }

and my Output is something like this:

    1         2
1. Caramel and Sea Salt
2. Caramel and Sea Salt
3. Caramel and Sea Salt
2. Wetermelon and Carrot
3. Backed Beans and Barbque
4. Backed Beans and Barbque
       1         2     //  How to delete this line?
1. Caramel and Sea Salt
2. Caramel and Sea Salt

I dont know how I can delete the second line column? and why I get it?

thanks you for help me :)

Faiq Irfan

You're printing the column header twice because you have put it inside the for loop for (int col = 0; col < numberCol.length; col++). You have to do it like this:

String[] sweetFlevors = { "Caramel", "Cinnamon", "Wetermelon", "Backed Beans" };

String[] savoryFlavors = { "Sea Salt", "Potato Chip", "Carrot", "Barbque" };

int[] numberRows = { 1, 2, 3, 4 };
int[] numberCol = { 1, 2 };

System.out.println("       " + numberCol[0] + "         " + numberCol[1]);
for (int col = 0; col < numberCol.length; col++) {
    for (String sweet: sweetFlevors) {
        for (String savory: savoryFlavors) {
            for (int row = 0; row < numberRows.length; row++) {
                System.out.print(numberRows[row] + ". ");

                System.out.println(sweet + " and " + savory);
            }
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use if else statements in select form?

From Dev

How to use the if-else statements in batch programming?

From Dev

How to use values from if else statements

From Dev

In Corona SDK, how do I use the 'and' keyword in if, elseif, else statements?

From Dev

How to use multiple execvp calls with if-else statements?

From Dev

In Corona SDK, how do I use the 'and' keyword in if, elseif, else statements?

From Dev

How to use if and else statements to achieve Age Classifier program

From Dev

Multiple if else statements using array

From Dev

Rails – How to use if else statement in array?

From Dev

Rails – How to use if else statement in array?

From Dev

How to refactor these if else statements to a function

From Dev

Is it faster to use else/if statements or a SQL query?

From Dev

Why use preprocessor #if statements instead of if() else?

From Dev

A Beginner Trying to Use Else Statements C#?

From Dev

Use comma separated string to create or statements in if else?

From Dev

list comprehension that makes use of a dictionary and if else statements

From Dev

How to use IF or ELSE statements in Python? BTW, How is my username and profile picture?

From Dev

How can I use if-else statements (or a better way) to assign absolute values to days in a year (using R)?

From Dev

spacebars: how to use and / or in if statements

From Dev

How to use If statements in a stream?

From Dev

How to use multiple if statements

From Dev

How to properly use if else and else if

From Dev

How to improve nested if-else statements?

From Dev

How to include if-else statements into reactive flow

From Dev

How to include if-else statements into reactive flow

From Dev

How to bypass scope convention in ruby if else statements

From Dev

How to execute block of statements in then and else in Oracle sql

From Dev

How to use "if statements" in "let" statements in xquery

From Dev

How to use loop statements and condition statements in TOSCA?

Related Related

  1. 1

    How to use if else statements in select form?

  2. 2

    How to use the if-else statements in batch programming?

  3. 3

    How to use values from if else statements

  4. 4

    In Corona SDK, how do I use the 'and' keyword in if, elseif, else statements?

  5. 5

    How to use multiple execvp calls with if-else statements?

  6. 6

    In Corona SDK, how do I use the 'and' keyword in if, elseif, else statements?

  7. 7

    How to use if and else statements to achieve Age Classifier program

  8. 8

    Multiple if else statements using array

  9. 9

    Rails – How to use if else statement in array?

  10. 10

    Rails – How to use if else statement in array?

  11. 11

    How to refactor these if else statements to a function

  12. 12

    Is it faster to use else/if statements or a SQL query?

  13. 13

    Why use preprocessor #if statements instead of if() else?

  14. 14

    A Beginner Trying to Use Else Statements C#?

  15. 15

    Use comma separated string to create or statements in if else?

  16. 16

    list comprehension that makes use of a dictionary and if else statements

  17. 17

    How to use IF or ELSE statements in Python? BTW, How is my username and profile picture?

  18. 18

    How can I use if-else statements (or a better way) to assign absolute values to days in a year (using R)?

  19. 19

    spacebars: how to use and / or in if statements

  20. 20

    How to use If statements in a stream?

  21. 21

    How to use multiple if statements

  22. 22

    How to properly use if else and else if

  23. 23

    How to improve nested if-else statements?

  24. 24

    How to include if-else statements into reactive flow

  25. 25

    How to include if-else statements into reactive flow

  26. 26

    How to bypass scope convention in ruby if else statements

  27. 27

    How to execute block of statements in then and else in Oracle sql

  28. 28

    How to use "if statements" in "let" statements in xquery

  29. 29

    How to use loop statements and condition statements in TOSCA?

HotTag

Archive