Shangkari Sha
Shangkari Sha

Reputation: 21

Java Bar Chart Printing Program

here is the question:

Write an application that reads five numbers between 1 and 30. For each number that’s read, your program should display the same number of adjacent asterisks. For example, if your program reads the number 7, it should display *******. Display the bars of asterisks after you read all five numbers.

here is my code:

package Assignment.Q034;
import java.util.Scanner;

public class Q034_trial 
{
    public static void main (String[] args) 
    {          
        Scanner input = new Scanner (System.in);
        int num; 
        num = 1-30;
    
        for (int i = 0; i < 5; i++)// system asks for no more than 5 numbers
        { 
            System.out.printf("Enter a number: ");
            num = input.nextInt();
        }
    
        for (int j = 0; j < num; j++)
        {
            System.out.printf("*"); 
        }
                    
        System.out.println();
        
    }            
}

program IDe used: Apache Netbeans IDE 12.4

the code does not sure any error but when I run and debug it, the output shows like this:

Enter a number: 1 

Enter a number: 2

Enter a number: 3

Enter a number: 4

Enter a number: 5

*****

but the output I need is:

Enter a number: 1 

Enter a number: 2

Enter a number: 3

Enter a number: 4

Enter a number: 5

*
**
***
****
*****

I am new to java programming. please help me t find the solution.

Upvotes: 0

Views: 2962

Answers (2)

Chris
Chris

Reputation: 36680

You need to read in five integers, and then when you are done, do something with them. This means you need some way to store all five integers.

The obvious solution is to store them in an array.

public class Q034_trial 
{
    public static void main (String[] args) 
    {          
        Scanner input = new Scanner (System.in);
        int[] nums = new int[5];
    
        for (int i = 0; i < 5; i++)
        { 
            System.out.printf("Enter a number: ");
            int num = input.nextInt();
            nums[i] = num;
        }
    }            
}

Having done that you merely need to iterate over each number in the array to print the correct number of asterisks.

public class Q034_trial 
{
    public static void main (String[] args) 
    {          
        Scanner input = new Scanner (System.in);
        int[] nums = new int[5];
    
        for (int i = 0; i < 5; i++)
        { 
            System.out.printf("Enter a number: ");
            int num = input.nextInt();
            nums[i] = num;
        }

        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < nums[i]; j++) 
                System.out.printf("*");

            System.out.println();
        }
    }            
}

Upvotes: 0

troy
troy

Reputation: 76

You can try to break them down individually and try to incorporate an approach like this or use these ideas for your project:

 import java.util.Scanner;
public class Array {
    public static void main(String[] args){

        Array asteriskGenerator = new Array();

        int nb[]=new int[5];
        Scanner input = new Scanner (System.in);
        for(int i=0;i<5;i++)
        {
            System.out.print("Please, Enter a number between 1 - 30 ");
            nb[i]=input.nextInt();
        }
        input.close();


        asteriskGenerator.asteriskGenerator(nb);
    }
    void asteriskGenerator(int nb[])
    {
        for(int i = 0; i <  nb.length; i++)
        {
            for(int j=1;j<=nb[i];j++)
            {
                System.out.print("*");
            }
            System.out.println();
        }
    }

}

I hope this helps in what you are trying to accomplish!

Upvotes: 1

Related Questions