5 Replies - 299 Views - Last Post: 29 October 2017 - 07:47 PM Rate Topic: -----

#1 blackmask85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-October 17

Array not printing while Histogram is?

Posted 29 October 2017 - 03:56 AM

Hi! I'm learning how to use Java this year and decided to do some activities given to me by my coworker. I've spent countless hours trying to figure out how to print Array and Histogram together. I have my histogram with the "*" done correctly but I am very confused to why my Array won't print how I want it to. Any help would be very appreciated!
public class test2{

   static final int SIZE = 51;
   static final int LIMIT = 45; //random range limit
   
   public static void main ( String[] args ) {
   
   int[ ]ranArray = new int [SIZE];
   Random rand = new Random();
   int rNum = 0;
   
   System.out.print(ranArray);
   
   System.out.println("\n" + "-------------------------------------------------------");   
   
   System.out.println("Histogram: ");
   
      for(int i=0; i<ranArray.length; i++){
           
      ranArray[i] = rand.nextInt(LIMIT)+5; //limits range to 5 or greater
      System.out.print(ranArray[i] + "\t");
      
         for(int j=0; j<ranArray[i]; j++)
            System.out.print("*");
            System.out.println();
      }
           
      
      
   }//ends class



The output to this code is correct and how I wanted it to be but I can't print the Arrays by itself. I want it to look like this:
Arrays:
6
5
8
4
2
Histogram:
6 ******
5 *****
8 ********
4 ****
2 **

Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Array not printing while Histogram is?

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3444
  • View blog
  • Posts: 15,606
  • Joined: 20-September 08

Re: Array not printing while Histogram is?

Posted 29 October 2017 - 04:38 AM

Then don't nest the loops. Have them following each other
Was This Post Helpful? 1
  • +
  • -

#3 blackmask85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-October 17

Re: Array not printing while Histogram is?

Posted 29 October 2017 - 01:38 PM

View Postg00se, on 29 October 2017 - 04:38 AM, said:

Then don't nest the loops. Have them following each other

Can I see a possible example? Even then, I'm pretty confused as to what to do to print out my arrays and histograms
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,312
  • Joined: 20-March 10

Re: Array not printing while Histogram is?

Posted 29 October 2017 - 01:56 PM

What Goose meant was this,
Print off the label 'Array:' first then have 2 loops on the integer variable i, one that prints out the array first then the label 'Histogram'

then just print Histogram as you are doing.....

System.out.println("Array: ");

    

      for(int i=0; i<ranArray.length; i++){

            

      ranArray[i] = rand.nextInt(LIMIT)+5; //limits range to 5 or greater
      System.out.print(ranArray[i] + "\t");
       
}
     System.out.println("Histogram: ");

       for(int i =0; i<ranArray.length, i++){

         for(int j=0; j<ranArray[i]; j++)
          System.out.print("*");

            System.out.println();

}



Was This Post Helpful? 1
  • +
  • -

#5 blackmask85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-October 17

Re: Array not printing while Histogram is?

Posted 29 October 2017 - 04:42 PM

View Postsnoopy11, on 29 October 2017 - 01:56 PM, said:

What Goose meant was this,
Print off the label 'Array:' first then have 2 loops on the integer variable i, one that prints out the array first then the label 'Histogram'

then just print Histogram as you are doing.....

System.out.println("Array: ");

    

      for(int i=0; i<ranArray.length; i++){

            

      ranArray[i] = rand.nextInt(LIMIT)+5; //limits range to 5 or greater
      System.out.print(ranArray[i] + "\t");
       
}
     System.out.println("Histogram: ");

       for(int i =0; i<ranArray.length, i++){

         for(int j=0; j<ranArray[i]; j++)
          System.out.print("*");

            System.out.println();

}




Oh my god, after countless hours figuring out how to deal with this problem I finally finished thanks to you and goose! Thank you very much!!!
Was This Post Helpful? 0
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,312
  • Joined: 20-March 10

Re: Array not printing while Histogram is?

Posted 29 October 2017 - 07:47 PM

You're welcome.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1