Click here to Skip to main content
15,900,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was supposed to make a program where it asks user to input the score and show it in a bar chart where the character is *. Here is my code, but I want to make it shorter and concise. Can someone help me reformat this? Thank you.

import java.util.Scanner;
public class casa2 
{
    public static void main (String[] args) 
    {
        Scanner input = new Scanner(System.in);
        int art, bob, cal, dan, eli;
        System.out.print("Enter points earned by Art >> ");
        art = input.nextInt();
        System.out.print("Enter points earned by Bob >> ");
        bob = input.nextInt();
        System.out.print("Enter points earned by Cal >> ");
        cal = input.nextInt();
        System.out.print("Enter points earned by Dan >> ");
        dan = input.nextInt();
        System.out.print("Enter points earned by Eli >> ");
        eli = input.nextInt();
        System.out.println("\nPoints for Game\n");

        System.out.print("\nArt  ");        
        for (int num = 0; num < art; num += 1)
        {
            System.out.print("*");
        }

        System.out.print("\nBob  ");
        for (int num = 0; num < bob; num += 1)
        {
            System.out.print("*");
        }

        System.out.print("\nCal  ");
        for (int num = 0; num < cal; num += 1)
        {
            System.out.print("*");
        }

        System.out.print("\nDan  ");
        for (int num = 0; num < dan; num += 1)
        {
            System.out.print("*");
        }

        System.out.print("\nEli  ");
        for (int num = 0; num < eli; num += 1)
        {
            System.out.print("*");
        }
    }
}


What I have tried:

Is it possible to do it using array?
Posted
Updated 18-Feb-21 20:31pm

Yes, of course you can
Java
import java.util.Scanner;
public class BarChart
{

    public static void main (String[] args)
    {
        Player players[] = {  new Player("Art") , new Player("Bob"),  new Player("Cal"), new Player("Dan"), new Player("Eli") };

        Scanner input = new Scanner(System.in);
        for (Player player : players)
        {
          System.out.printf("Enter points earned by %s >> \n", player.name);
          player.points = input.nextInt();
        }

        for (Player player : players)
        {
          System.out.printf("\n%s  \n", player.name);
          for (int num = 0; num < player.points; num += 1)
          {
              System.out.print("*");
          }
        }

    }
}
class Player
{
  public String name;
  public int points;
  public Player(String name) { this.name = name; this.points = 0; }
}
 
Share this answer
 
Comments
Member 15074855 19-Feb-21 2:42am    
Om, thank you so much. I'll take note and learn more of arrays. Thank you again!
CPallini 19-Feb-21 2:44am    
You are welcome.
Quote:
Is it possible to do it using array?

Yes.

In fact, I'd look at using an array to hold your data, and methods do do "similar code".
If you look at your code, there are "chunks" which repeat, differing only by the variables they use. For example:
System.out.print("\nArt  ");
for (int num = 0; num < art; num += 1)
{
    System.out.print("*");
}
System.out.print("\nBob  ");
for (int num = 0; num < bob; num += 1)
{
    System.out.print("*");
}
Put that code in a method, and pass it the variables so the code only exists once, and you code starts to get more concise without using an array at all. This also has the advantage that changes to the method only have to be made once - so your code becomes more reliable.

I'd start by identifying "duplicate" code, and converting it to methods, then think about using an array once I'd got that working, as it is a little more complex to to (and doing things one at a time - making sure it still works before moving on - is a good way to develop code).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900