...

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



sabi
04-06-2012, 05:41 AM
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?

Mishu
04-06-2012, 06:18 AM
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.

ckeyrouz
04-06-2012, 04:19 PM
This is a simple sample on how to do it. You have to implement the same logic in your algorithm.


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);
}
}
}

Bushman
04-16-2012, 09:07 PM
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.


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;
}
}

Fou-Lu
04-16-2012, 09:34 PM
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.


public static String strRepeat(String sToRepeat, int iTimes)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < iTimes; ++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));
}

Bushman
04-17-2012, 03:10 AM
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.


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 );
}
}

sabi
05-07-2012, 01:22 AM
oh that seems complicated



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum