Home > Blockchain >  How to validate input in my Java program and add the option for the user to run the program again or
How to validate input in my Java program and add the option for the user to run the program again or

Time:03-22

So I have an assignment in my CISP 1 class that requires me to create a program that displays a bar chart comprised of asterisks based on the amount of sales 5 different stores have had. I've got a base made, but I still want to add a loop that validates the input from the user(i.e. throws an error when the user tries to enter a negative number), and I want to add the option to run the program or exit it. I'm just a little lost on how to do all of that, so I figured I'd reach out on this website and ask. I know a lot of this code could be simplified with arrays, but we haven't started studying that yet - so I'm afraid to be messing with something I don't fully understand. Below is my code:

import java.util.Scanner;

public class BarChart 

{
    public static void main(String[] args)

    {
        int store1, store2, store3, store4, store5;

        Scanner keyboard = new Scanner(System.in);

        System.out.println("This program will display a bar chart "   
        " comprised of astericks based on five different stores' "  
        "sales. 1 asterick = $100 in sales.");

        System.out.print("Enter today's sales for store 1: ");
        store1 = keyboard.nextInt();

        System.out.print("Enter today's sales for store 2: ");
        store2 = keyboard.nextInt();

        System.out.print("Enter today's sales for store 3: ");
        store3 = keyboard.nextInt();

        System.out.print("Enter today's sales for store 4: ");
        store4 = keyboard.nextInt();

        System.out.print("Enter today's sales for store 5: ");
        store5 = keyboard.nextInt();

        System.out.println("Sales \t Bar Chart");
        System.out.println("----- \t ---------");

        System.out.print("\nStore 1: ");        
        
        for (int num = 0; num < store1; num  = 100)
        {
            System.out.print("*");
            
        }

        System.out.print("\nStore 2: ");
        for (int num = 0; num < store2; num  = 100)
        {
            System.out.print("*");
        }

        System.out.print("\nStore 3: ");
        for (int num = 0; num < store3; num  = 100)
        {
            System.out.print("*");
        }

        System.out.print("\nStore 4: ");
        for (int num = 0; num < store4; num  = 100)
        {
            System.out.print("*");
        }

        System.out.print("\nStore 5: ");
        for (int num = 0; num < store5; num  = 100)
        {
            System.out.print("*");
        }

    }
    
}

I've tried adding if statements each time the user is asked to enter a sales amount, but that didn't work.

CodePudding user response:

while (true) {
  Scanner keyboard = new Scanner(System.in);
  <Do something...>
  if (condition) {
    break;
  }
}

CodePudding user response:

You have to put if or while condition after reads store. If entered store < 0, You get suggestion message "Please enter positive number". You can reduce so much boilerplate code using array.

Here down is code:

import java.util.Scanner;

public class BarChart
{
    static void message()
    {
        System.out.println("Please enter positive number");
    }
    public static void main(String[] args)

    {
        int store1, store2, store3, store4, store5;

        Scanner keyboard = new Scanner(System.in);

        System.out.println("This program will display a bar chart "   
        " comprised of astericks based on five different stores' "  
        "sales. 1 asterick = $100 in sales.");

        System.out.print("Enter today's sales for store 1: ");
        store1 = keyboard.nextInt();
        if(store1 < 0)
        {
            message();
            store1 = keyboard.nextInt();
            
        }

        System.out.print("Enter today's sales for store 2: ");
        store2 = keyboard.nextInt();
        if(store2 < 0)
        {
            message();
            store2 = keyboard.nextInt();
            
        }

        System.out.print("Enter today's sales for store 3: ");
        store3 = keyboard.nextInt();
        if(store3 < 0)
        {
            message();
            store3 = keyboard.nextInt();
            
        }

        System.out.print("Enter today's sales for store 4: ");
        store4 = keyboard.nextInt();
        if(store4 < 0)
        {
            message();
            store4 = keyboard.nextInt();
            
        }

        System.out.print("Enter today's sales for store 5: ");
        store5 = keyboard.nextInt();
        if(store5 < 0)
        {
            message();
            store5 = keyboard.nextInt();
            
        }

        System.out.println("Sales \t Bar Chart");
        System.out.println("----- \t ---------");

        System.out.print("\nStore 1: ");        
        
        for (int num = 0; num < store1; num  = 100)
        {
            System.out.print("*");
            
        }

        System.out.print("\nStore 2: ");
        for (int num = 0; num < store2; num  = 100)
        {
            System.out.print("*");
        }

        System.out.print("\nStore 3: ");
        for (int num = 0; num < store3; num  = 100)
        {
            System.out.print("*");
        }

        System.out.print("\nStore 4: ");
        for (int num = 0; num < store4; num  = 100)
        {
            System.out.print("*");
        }

        System.out.print("\nStore 5: ");
        for (int num = 0; num < store5; num  = 100)
        {
            System.out.print("*");
        }

    }
    
}

You can reduce so much boilerplate code using array

import java.util.Scanner;

public class BarChart
{
    static void message()
    {
        System.out.println("Please enter positive number");
    }
    public static void main(String[] args)
    {
        int store[] = new int[5];

        Scanner keyboard = new Scanner(System.in);

        System.out.println("This program will display a bar chart "   
        " comprised of astericks based on five different stores' "  
        "sales. 1 asterick = $100 in sales.");
        
        for (int i = 0; i < store.length; i  )
        {
            System.out.print("Enter today's sales for store "   (i   1)   ": ");
            store[i] = keyboard.nextInt();
            if(store[i] < 0)
            {
                message();
                store[i] = keyboard.nextInt();
            }
            
            System.out.print("*");
        }

        System.out.println("Sales \t Bar Chart");
        System.out.println("----- \t ---------");

        for (int i = 0; i < store.length; i  )
        {
            System.out.print("\nStore "   (i   1)   ":");        
        
            for (int num = 0; num < store[i]; num  = 100)
            {
                System.out.print("  *");
                
            }
        }

    }
    
}
  • Related