# HELP with a plotter program ASAP!

• 06-01-2011, 08:24 PM
acole5
HELP with a plotter program ASAP!
I'm SUPER lost on this project and keep getting this error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Plotter.main(Plotter.java:3)

Here's the assingment:
Create a simple plotter (x-axis runs vertically, y -axis runs horizontally) using command line arguments for a (left end point), b (right end point), and n (number of sub-intervals of [a, b].)

Steps:

compute maximum and minimum values of the function on [a, b].
compute scaling factor sf. For example, does the following accomplish all we need?
sf = graphPageWidth/(max - min).
construct for each point in the interval the required number of spaces (one for each column) preceding the plot (an *)
#cols = sf ( f(a +idx) - min ).

and here's my code:public class Plotter {
public static void main(String[] args) {
double a = Double.parseDouble(args[0]);
double b = Double.parseDouble(args[1]);
double x, sf, y;
double max;
double min;
double nSpaces;
int n = Integer.parseInt(args[2]);
System.out.println(" "+args[0]+" "+args[1]+" "+args[2]);
double dx = (b-a)/n;
max=0;
min=0;
for (int i =0; i<=n; i++) {
x= a + i * dx;
y= x*x*x;
if (y>max) {
max=y;
}
else if (y<min) {
min=y;
}
}
sf = 120/(max - min);
System.out.println(dx);
System.out.println(max);
System.out.println(min);

for(int k=0; k<=n; k++) {
x = a + k * dx;
y = x*x*x;
nSpaces = (y - min) * sf;
int j = 0;
if(j<nSpaces)
{
System.out.print(" ");
j++;
}
else {
System.out.println("*");
}
}
}
}
• 06-01-2011, 08:35 PM
Norm
Quote:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Plotter.main(Plotter.java:3)
You need to learn how to read the error message.
It says that at line 3 of your program there is an array that does not have a 0th element. In other words the array is empty.
You should test if the length of the array is greater than 0 before trying to get the 0th element.

What do you expect to be in the args array?

Are you passing arguments to your program when you start its execution?
• 06-01-2011, 08:42 PM
goldest
Quote:

Originally Posted by Norm
Are you passing arguments to your program when you start its execution?

acole5,

Read this above line again and again. If you can understand this, then that's it. :8):

Goldest
• 06-03-2011, 12:16 AM
acole5
Thank you both! I figured it out and turned in the project :)