Results 1 to 8 of 8
  1. #1
    BugginVT is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default 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 **************?

    Thanks in advance!



    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[9999];
    		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!");
            }
        		
    	}
    
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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("*);
    }

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by Eranga View Post
    [COLOR="DarkGreen"]Say the input is 1500, then divide by 100 gives the answer is 14.
    Shoplifting?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  5. #5
    BugginVT is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default

    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!

  6. #6
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Java Code:
    for(int j=0; j<inputSales.length; j++)
      for(int i = 0; index < inputSales[j]/100; i++) 
        System.out.print("*");
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  7. #7
    BugginVT is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default

    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[9999];
    		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!");	
    	}
    
    }

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

Similar Threads

  1. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM
  2. Prime numbers
    By tercius in forum New To Java
    Replies: 3
    Last Post: 05-04-2008, 06:05 AM
  3. printing an "E" out of asterisks via strings
    By hokieman07 in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 05:45 AM
  4. asterisks triangles
    By Dan121 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 07:42 PM
  5. random numbers
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 02:56 AM

Posting Permissions

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