Show lines in java with loops

Viewed 93

i have a problem with a little java program.

I have to type a number between 1 and 10. This number represents the number of lines. For example: If you type 4, the following image is displayed.

o
oo
ooo
oooo

    import java.util.Scanner;
   public class Main 
   {
     public static void main(String[] args) 
   {
    // TODO Auto-generated method stub

    int number, count;

   System.out.println("Write the number from 1 to 10");
    
    Scanner teklatua = new Scanner(System.in); 
    String datu = teklatua.nextLine(); 
    number = Integer.parseInt(datu); 
    
    if(number> 10 || number< 1) 
    {
    System.out.println(number + " not between 1 and 10, choose again");
    }

   else
   {
    for (count=1; count<=number ; count++)
    {
     System.out.println("o");
  
      for (count=1; count<=number; count++)
      {
      System.out.print("o");
      }
   }           
  }  
 }  
}

Thanks!!

Edit: Sorry, my problem was that the execution of the code gives me:
o
oooo and I did not know how to solve it

5 Answers

I haven't seen the question. But I assume the problem is using of "count" variable as an iteration variable in two loops. To fix it just use another variable like:

public static void main(String args[]) 
{
   Scanner teklatua = new Scanner(System.in); 
   int number = -1;
   while (number> 10 || number< 1)
   {
      System.out.println("Choose a number between 1 and 10");
      String datu = teklatua.nextLine(); 
      number = Integer.parseInt(datu); 
   }

   for (int i=0;i<number;i++)
   {
      for (int a =0;a<i+1;a++)
        System.out.print("o");
      System.out.println();
   }    
}

Consider using a helper method for the validation of the user's input:

import java.util.Scanner;

class Main {
    private static int getIntegerInput(Scanner scanner, String prompt, int minValue, int maxValue) {
        System.out.print(prompt);
        int validInteger = -1;
        while (scanner.hasNext()) {
            if (scanner.hasNextInt()) {
                validInteger = scanner.nextInt();
                if (validInteger >= minValue && validInteger <= maxValue) {
                    break;
                } else {
                    System.out.printf(
                            "Error: Please enter an integer between %d and %d inclusive%n", minValue, maxValue);
                    System.out.print(prompt);
                }
            } else {
                System.out.printf(
                        "Error: Please enter an integer between %d and %d inclusive%n", minValue, maxValue);
                System.out.print(prompt);
                scanner.next();
            }
        }
        return validInteger;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int minLines = 1, maxLines = 10;
        String numLinesPrompt = "Please enter the number of lines: ";
        int numLines = getIntegerInput(scanner, numLinesPrompt, minLines, maxLines);
        for (int i = 0; i < numLines; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print('o');
            }
            System.out.println();
        }
    }
}

Example Usage:

Please enter the number of lines: 12
Error: Please enter an integer between 1 and 10 inclusive
Please enter the number of lines: -2
Error: Please enter an integer between 1 and 10 inclusive
Please enter the number of lines: a
Error: Please enter an integer between 1 and 10 inclusive
Please enter the number of lines: 5
o
oo
ooo
oooo
ooooo

You might want add another variable for the inner loop. Somthing like this:

....
for (int row = 1; row <= number; ++row) {
  System.out.println("o");
  for (int column = 1; column <= row; ++column) {
    System.out.print("o");
  }
}
....

The nested loops need to be updated:

  • the outer loop has a separate counter of lines i which also defines how many "o" are printed
  • the inner loop prints not more than i characters
  • upon ending the inner loop, new line is printed
for (int i = 1; i <= number; i++) {
    for (count=1; count <= i; count++) {
        System.out.print("o");
    }
    System.out.println();
}

Another approach could be to use String::repeat instead of inner loop:

for (count = 1; count <= number; count++) {
    System.out.println("o".repeat(count));
}

your second forLoop is wrong it should be something like

for(int j=1;j<=n;j++) {
    for(int i=1; i<=j; i++) {
        System.out.print("0");
       }
        System.out.println();
 }
Related