Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-06-2012, 04:41 AM   PM User | #1
sabi
New Coder

 
Join Date: Oct 2011
Posts: 27
Thanks: 2
Thanked 0 Times in 0 Posts
sabi is an unknown quantity at this point
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?
sabi is offline   Reply With Quote
Old 04-06-2012, 05:18 AM   PM User | #2
Mishu
Banned

 
Join Date: Mar 2012
Posts: 306
Thanks: 1
Thanked 28 Times in 28 Posts
Mishu can only hope to improve
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.
Mishu is offline   Reply With Quote
Old 04-06-2012, 03:19 PM   PM User | #3
ckeyrouz
Senior Coder

 
ckeyrouz's Avatar
 
Join Date: Jun 2009
Location: Montreal, Canada
Posts: 1,044
Thanks: 5
Thanked 179 Times in 179 Posts
ckeyrouz is on a distinguished road
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.
ckeyrouz is offline   Reply With Quote
Old 04-16-2012, 08:07 PM   PM User | #4
Bushman
New Coder

 
Join Date: Apr 2012
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Bushman is an unknown quantity at this point
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;
    }
}
Bushman is offline   Reply With Quote
Old 04-16-2012, 08:34 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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));

Fou-Lu is offline   Reply With Quote
Old 04-17-2012, 02:10 AM   PM User | #6
Bushman
New Coder

 
Join Date: Apr 2012
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Bushman is an unknown quantity at this point
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 );
    }
}
Bushman is offline   Reply With Quote
Old 05-07-2012, 12:22 AM   PM User | #7
sabi
New Coder

 
Join Date: Oct 2011
Posts: 27
Thanks: 2
Thanked 0 Times in 0 Posts
sabi is an unknown quantity at this point
Need help with Two-Dimensional array

oh that seems complicated
sabi is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:01 PM.


Advertisement
Log in to turn off these ads.