1

I'm creating a method to be able to "print a histogram," and I'm trying to get it to reference the histogramData.length, to be able to loop and build each row, but it's not recognizing histogramData.length in the method.

I have the code in 2 separate files, one for main and one to build the methods.

Main looks like:

import becker.robots.*;
public class DrawHistogram extends Object
{   public static void main(String[] args)
   {   City Edmonds = new City(12, 12);
       HistogramRobot drawingBot = new HistogramRobot(Edmonds, 1, 1, Direction.EAST, 1000);
       HistogramPrinter histPrinter = new HistogramPrinter();

  int [] histogramData = new int[7];

     histogramData[0] = 3; // The first element holds 3
     histogramData[1] = 5; // The second element holds 5
     histogramData[2] = 1; // The third element holds 1
     histogramData[3] = 0; // The fourth element holds 0
     histogramData[4] = 4; // The fifth element holds 4
     histogramData[5] = 2; // The sixth element holds 2
     histogramData[6] = 1; // The seventh element holds 1

    drawingBot.drawRow();
   }
}

And my method file looks like

import becker.robots.*;
class HistogramRobot extends Robot
{
HistogramRobot(City c, int st, int ave, Direction dir, int num)
{
    super(c, st, ave, dir, num);
}    
 public void drawRow()
 {
    for(int counter = 0; counter < histogramData.length; counter++)
       {
          if( histogramData[counter] == 0) 
         {
         this.turnRight();
                 this.move();
                 this.turnLeft();
                 }
              for( int histoDrop = 0; histoDrop < histogramData[counter]; histoDrop++)
              {
         this.putThing();
                 this.move();
          }
        this.turnAround();
        for (int moves = 0; moves < histogramData[counter]; moves++)
           {
           this.move();
           }
        this.turnLeft();
        this.move();
        this.turnLeft();
     }
  }

 public void turnRight()
        {
    this.turnLeft();
  this.turnLeft();
  this.turnLeft();
 }

 public void turnAround()
 {
    this.turnLeft();
  this.turnLeft();
 }
}

And the error I'm getting for each mention of "histogramData" including "histogramData.length"

HistogramRobot.java:24: error: cannot find symbol
            for( int histoDrop = 0; histoDrop < histogramData[counter]; histoDrop++)
                                                ^
  symbol:   variable histogramData
  location: class HistogramRobot

What's causing the error and how can I fix it?

//I'm sorry! I could not get the spacing in the second part to work in my favor, let me know if you need clarification to answer.

1
  • Your problem is you don't define an array called histogramData anywhere in your HistogramRobot class. Try pass it into the drawRow method Commented Jun 5, 2014 at 6:48

1 Answer 1

1

you have declared your

int [] histogramData = new int[7];

in your main method and so its scope is limited to this method.

Pass it as a parameter to where it is needed

Such as

drawingBot.drawRow(histogramData);

and you method declaration would be

public void drawRow(int [] histogramData)
Sign up to request clarification or add additional context in comments.

4 Comments

histogramData is not even in the same class as where you try to use it. just to add a bit :)
Forgive me if I seem like I know what I'm talking about... but I have no idea where I would put it otherwise? Should I put it with the creation of the drawRow method, or in a new file entirely?
@TaylerMaybe Its OK, it does not look like you know what you are talking about ;-). I have updated my answer
I KNOW IT SAYS TO AVOID COMMENTS LIKE "THANKS" BUT THAT HONESTLY HELPED ME MORE THAN YOU COULD EVER KNOW SO THANK YOU THANK YOU THANK YOU.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.