Results 1 to 4 of 4
  1. #1
    LordStark is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Post Program that converts user input of ints into asterisks

    Basically these are the assignment specs :

    Create a program that will allow the user to enter an arbitrary number of integers that are in the range 1 to 10 inclusive. After all input has been accepted, display all of the values that were entered in the form of a graph. Use a modular approach in your solution.

    graph example:

    1. ****
    2. ***
    3. *********
    4. *******
    5. ***
    6. *
    7. *******
    8. *********
    9. **
    10. ****
    etc...

    So far what i have gotten is :

    ---------------------------------------------------------------------------------------------

    package projects;

    public class SimpleGraph
    {
    static Utilities util = new Utilities();
    public static void main(String[] args)
    {
    int maxSize = 20, count = 0;
    int[] aGraph = new int[maxSize];

    int input = util.getSafeInt("Enter a whole number between 1-10 [0 to quit]: ");

    while (input != 0 && input < 11 && count < maxSize)
    {
    aGraph[count] = input;
    count++;
    if (count < maxSize)
    {
    input = util.getSafeInt("Enter another whole number between 1-10 [0 to quit]: ");
    }
    }
    util.writeln(" ");
    for(int index = 0; index < count; index ++)
    {
    util.writeln((index+1)+ ". " + aGraph[index]);
    }
    }
    }

    ---------------------------------------------------------------------------------------------

    What i have gotten so far asks the user for input in ints (up to 20) and displays the numbers in order back at the user after the user is done entering the numbers. The user may also exit early by pressing 0 which will display the numbers they have entered in order as well.

    All input is welcome :)

  2. #2
    Jingoism is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default Re: Program that converts user input of ints into asterisks

    If you want people to read your code you must put the CODE tags around them.

    Java Code:
    public class SimpleGraph
    {
    	static Utilities util = new Utilities();
    	public static void main(String[] args)
    	{
    		int maxSize = 20, count = 0;
    		int[] aGraph = new int[maxSize];
    
    		int input = util.getSafeInt("Enter a whole number between 1-10 [0 to quit]: ");
    
    		while (input != 0 && input < 11 && count < maxSize)
    		{
    			aGraph[count] = input;
    			count++;
    			if (count < maxSize)
    			{
    				input = util.getSafeInt("Enter another whole number between 1-10 [0 to quit]: ");
    			}
    		}
    		util.writeln(" ");
    		for(int index = 0; index < count; index ++)
    		{
    			util.writeln((index+1)+ ". " + aGraph[index]);
    		}	
    	}
    }

  3. #3
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default Re: Program that converts user input of ints into asterisks

    You only need an array of 10 that will accept an infinite amount of inputs. All you need to do is just:
    Java Code:
    arrayOf10Name[userInput - 1]++;
    This will store then number of occurances to the corresponding array index; minus 1 of course.

    The way that you are proceeding would force you to look through the entire array and count the occurances for each number before displaying the correct amount of asterisks. Using the way I depicted would be much easier, and allow an unlimited number of inputs.
    Catching exceptions is for communists

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Program that converts user input of ints into asterisks

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Program not waiting for user input
    By kkid in forum New To Java
    Replies: 2
    Last Post: 11-11-2012, 06:17 AM
  2. Replies: 5
    Last Post: 03-07-2012, 05:10 AM
  3. user input program
    By myalani in forum New To Java
    Replies: 4
    Last Post: 10-28-2011, 05:07 AM
  4. Replies: 2
    Last Post: 09-14-2010, 02:24 AM
  5. Replies: 13
    Last Post: 04-16-2010, 03:51 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
  •