Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    how do I display an asterisk (*) for every 100 someone has entered?

    I am prompting the user for a total sales amount in dollars, such as 1200, or 900, etc. For every 100 dollars, I need to display a * character. So if the user enters 500, ***** will show on the screen. So does anyone know how would I do this?

  • #2
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    Divide the number by 100 and that will tell you how many stars you need. Then use a loop to build a string of that many stars and then output the string.

  • #3
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    This is a simple sample on how to do it. You have to implement the same logic in your algorithm.

    Code:
    public class Asterisk
    {
        public static void main(String[] args)
        {
            if(args.length == 0)
            {
                System.out.println("No arguments passed");
                System.exit(-1);
            }
            try
            {
                long value = Long.valueOf(args[0]).longValue();
                long hundreds = value / 100;
                System.out.println("number of asterisks ["+hundreds+"]");
                for(long i=0; i<hundreds; i++)
                {
                    System.out.print("*");
                }
            }
            catch(NumberFormatException ex)
            {
                ex.printStackTrace();
                System.exit(-1);
            }
        }
    }
    Software and cathedrals are much the same - first we build them, then we pray.

  • #4
    New Coder
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Lol ckeyrouz, I don't think the op wanted that type of user input to asterisk conversion.

    I think he wanted something at the terminal level.

    Here is some freshly written personal code of mine that does the job.

    Code:
    import java.util.Scanner;
    
    public class BalanceToAsterisk
    {
        //variables
            //establish variable to hold string of asterisks
            static String asterisk_list = "";
            //establish prompt cycle controller
            static String answer = "";
            //establish variable to get user input of value we want to compare to 100
            static int balance = 0;
            //establish variable to accept user input
            static Scanner scanner = new Scanner ( System.in );
        public static void main ( String [ ] args )
        {
            do
            {
                //prompt user
                System.out.println ( );
                System.out.print ( "Enter value : " );
                balance = scanner.nextInt ( );
                
                if ( is100 ( balance ) )
                    asterisk_list += "*";
                    
                System.out.println ( );
                System.out.print ( "Continue ? " );
                answer = scanner.next ( );
                
            } while ( answer.equals ( "yes" ) || answer.equals ( "YES" ) || answer.equals ( "y" ) || answer.equals ( "Y" ) );
            //when no is toggled, jump out of while loop and print results
            System.out.println ( "ANSWER : " + asterisk_list );
        }
        //function to check if user input is divisible by 100 - essentially every time 100 is entered
        static public boolean is100 ( int value )
        {
            boolean flag = false;
            if ( value % 100 == 0 )
                flag = true;
            else
                flag = false;
            return flag;
        }
    }

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    This code doesn't match what the OP is looking for. If I enter 700 in there, it would give me a result of *. The OP wants 700 = ******* from their description.
    ckeyrouz's is closer, but assumes its part of a argument on the program invocation. The same logic would apply to the scanner.
    PHP Code:
    public static String strRepeat(String sToRepeatint iTimes)
    {
        
    StringBuilder sb = new StringBuilder();
        for (
    int i 0iTimes; ++i)
        {
            
    sb.append(sToRepeat);
        }
        return 
    sb.toString();
    }

    public static 
    void main(String... argv)
    {
        
    Scanner s = new Scanner(System.in);
        
    // I won't bother with looping or error checking.
        
    int iDollars s.nextInt();
        
    System.out.println(strRepeat("*"iDollars 100));


  • #6
    New Coder
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    My bad OP. I guess I read through the code way to quickly.
    In that case, use this code, very short and includes a prompt.

    Code:
    import java.util.Scanner;
    public class BalanceToAsterisk
    {
        public static void main ( String [ ] args )
        {
            String asterisk_list = ""; //string to accept new asterisks
            int balance = 0; //user input
            Scanner scanner = new Scanner ( System.in ); //user input scanner
            System.out.print ( "Enter balance : " ); balance = scanner.nextInt ( );
            for ( int valid_balances = 0; valid_balances < balance / 100; valid_balances ++ )
                    asterisk_list += "*";
            System.out.println ( "Asterisk list : " + asterisk_list );
        }
    }

  • #7
    New Coder
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Need help with Two-Dimensional array

    oh that seems complicated


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •