Thread: Turning numbers into asterisks.

1. Member Join Date
Feb 2009
Posts
3
Rep Power
0 Turning numbers into asterisks.

I've only been working with java for probably two weeks so you'll have to excuse the mess. The program I have here has an array set for a high number, runs down and asks the user to input numbers (assuming numbers are in increments of 100), then divides the numbers by 100 and outputs them. What I'm looking to do is instead of just outputting the numbers, I'd like to have asterisks represent for numbers. For every 100 that the user types in, will output a single *. So say the user inputs 1400, it divides it by 100, from this point, how do I get an output of **************?

Java Code:
import java.util.Scanner;

public class Astericks3
{

/**
* @param args
*/
public static void main(String[] args)
{

//priming reed
int exitCheck = 0;  //used to exit loop when 999 is typed
int salesCount = 1;  //used to help indicate order during final output
int Count = -1;  //used to cut off the 999 once exited loop
int[] inputSales = new int;
Scanner input = new Scanner(System.in);

//initial statement
System.out.println("Please enter first sales figure");

//main loop
for (int i = 0; i < inputSales.length; i++ )
if(exitCheck != 999)
{
inputSales[i] = input.nextInt();
System.out.println("Enter next sales figure, or 999 to exit: ");
exitCheck = inputSales[i];
++Count;
}

//dividing the input by 100
for (int i = 0; i < Count; ++i)
{
inputSales[i] = inputSales[i]/100;
}

//system output
{
for ( int i = 0; i < Count; ++i )
{
System.out.println("Number " + salesCount +  " sales: "  + inputSales[i]);
salesCount++;
}
System.out.println("Thank you!");
}

}

}  Reply With Quote

2. You can find the whole number for division. Say the input is 1500, then divide by 100 gives the answer is 14. So in a loop print the asterisk sign. Don't use the new line, that's all.

Java Code:
for(int index = 0; index < value; index++) {
System.out.print("*);
}  Reply With Quote

3. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
17  Originally Posted by Eranga [COLOR="DarkGreen"]Say the input is 1500, then divide by 100 gives the answer is 14.
Shoplifting?  Reply With Quote

4. Oops, it's a simple mistake. Once divide by 100, answer should be 15. ;)  Reply With Quote

5. Member Join Date
Feb 2009
Posts
3
Rep Power
0 So from what I'm understanding, you're saying my output statement should look something like this(below). I'm doing that and it's only printing the amount of numbers I put in. Say I input 900, 1000, 1100, it's only outputting three asterisks.

Java Code:
for(int i = 0; index < inputSales[i]; i++)
{
System.out.print("*");
}

Thanks for the help up to this point!  Reply With Quote

6. Java Code:
for(int j=0; j<inputSales.length; j++)
for(int i = 0; index < inputSales[j]/100; i++)
System.out.print("*");  Reply With Quote

7. Member Join Date
Feb 2009
Posts
3
Rep Power
0 Alright, I think I understood it. I took angryboys code fragment and modified it a bit. I included the final program in case anybody else bumps into this during a search.

Thanks for the help guys!

Java Code:
import java.util.Scanner;

public class Asterisks3
{

/**
* @param args
*/
public static void main(String[] args)
{

//priming reed
int exitCheck = 0;  //used to exit loop when 999 is typed
int salesCount = 1;  //used to help indicate order during final output
int Count = -1;  //used to cut off the 999 once exited loop
int[] inputSales = new int;
Scanner input = new Scanner(System.in);

//main loop
for (int i = 0; i < inputSales.length; i++ )
if(exitCheck != 999)
{
System.out.println("Enter sales figure " + salesCount + ", or 999 to exit: ");
inputSales[i] = input.nextInt();
exitCheck = inputSales[i];
++Count;
++salesCount;
}
//resetting salesCount
salesCount = 1;

//System output
for(int r=0; r<Count; r++)
{
System.out.print("Number " + salesCount +  " sales: ");
for(int i = 0; i < inputSales[r]/100; i++)
{
System.out.print("*");
}
++salesCount;
System.out.println(" ");
}
System.out.println("Thank you!");
}

}  Reply With Quote

8. While working on read more about arrays. How to use elements and length of it.

Anyway, it's pleasure to help you. If you have solved the problem please mark thread solved.  Reply With Quote Posting Permissions

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