Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By ajb5876

Thread: Simple Java Homework Help

  1. #1
    ajb5876 is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Simple Java Homework Help (* NOW SOLVED*)

    I am having trouble with this code, it does half of what I need it to do but not quite exactly.

    Here is what it needs to do:
    Write a method, named printHorizontalBars(), that prompts the user for four values and draws
    corresponding bar graphs using an ASCII character. For example if the user entered 15, 12, 9
    and 4, the program would draw.
    * * * * * * * * * * * * * * *
    * * * * * * * * * * * *
    * * * * * *

    Here is what I get with the current code output:

    Enter four integers...
    15 12 9 4

    * * * *
    * * * *
    * * * *
    * * * *

    Can anyone help me figure out whats wrong?

    Thanks
    -Austin

    Java Code:
    package patterns;
    import java.util.Scanner;
    /**
     *
     * @author Austin J Benner
     */
    public class Patterns  {
    
    public void HorizontalBars(){
        
      int[] numberArr = new int [4]; // Creates and array to read inputs into
    	
            Scanner sc = new Scanner (System.in); // Creates a new instance of the Scanner() method
    	System.out.println("Enter four integers...");
    	
    	int number_1 = sc.nextInt(); // Reads in the four integer inputs from user
    	int number_2 = sc.nextInt();
    	int number_3 = sc.nextInt();
    	int number_4 = sc.nextInt();
    	
    	numberArr[0] = number_1;
    	numberArr[1] = number_2;
    	numberArr[2] = number_3;
    	numberArr[3] = number_4;
    	
    System.out.println();
    
    int max = 0; // Determines the largest integer value in the array
    for(int i = 0; i < numberArr.length;i++) 
    {
    	if (numberArr[i] > max)
    	{
    		max = numberArr[i];
    	}
    
    	for(int j = 0; j < numberArr.length; j++) 
    	{
    		if (numberArr[i] >= j)
    		{
    			System.out.print("* "); // Prints ASCII symbol if largest integer value
    		}
    	}
                    System.out.println();
    }
    }    public static void main(String[ ] args) {
    // TODO code application logic here
         Patterns myTestDriver = new Patterns();
    
         
         System.out.println("printHorizontalBars");
         myTestDriver.HorizontalBars();
         
         
         
       }
       
    }
    Last edited by ajb5876; 02-10-2013 at 07:00 PM.

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Simple Java Homework Help

    I wont give you the solution to all of your problems since it's a homework, but can you spot anything wrong with the following part:

    Java Code:
        for(int j = 0; j < numberArr.length; j++) 
        {
            if (numberArr[i] >= j)
            {
                System.out.print("* "); // Prints ASCII symbol if largest integer value
            }
        }
    Will your program ever print out more * than the length of your array?

  3. #3
    ajb5876 is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Simple Java Homework Help

    Java Code:
        for(int j = 1; j <= numberArr.length; j++)
        {
            if (numberArr[i] >= j) 
            {
                System.out.print("* "); // Prints ASCII symbol if largest integer value
            }
        }
    I'm not 100% sure what you are asking (VERY NEW TO JAVA) because it did display more than one *'s with that code segment...I just messed with the code a little bit, starting to go in the right direction, though it only correctly displays number <= 4 properly. Anything over that it just displays as 4.

    -Austin

  4. #4
    ajb5876 is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Simple Java Homework Help

    Never mind I think I understand Now.

    Java Code:
    for(int j = 1; j <= max; j++)
    Is this what you were alluding to?

    -Austin

  5. #5
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Simple Java Homework Help

    Et voila!

    You solved it!

  6. #6
    ajb5876 is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Simple Java Homework Help

    Thanks for your guidance Zyril!!! Much appreciated.
    Zyril likes this.

Similar Threads

  1. Replies: 11
    Last Post: 03-22-2011, 06:33 AM
  2. Simple POS System in Java
    By amsroxy1125 in forum New To Java
    Replies: 5
    Last Post: 12-08-2009, 10:23 PM
  3. Simple Java program
    By Rolle in forum New To Java
    Replies: 3
    Last Post: 10-26-2009, 04:05 PM
  4. Simple java
    By abhiN in forum New To Java
    Replies: 0
    Last Post: 01-16-2008, 02:44 PM
  5. Simple java
    By abhiN in forum New To Java
    Replies: 1
    Last Post: 01-16-2008, 10:13 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
  •