# Thread: HELP with a plotter program ASAP!

1. Member Join Date
Jun 2011
Posts
10
Rep Power
0

## 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);
double b = Double.parseDouble(args);
double x, sf, y;
double max;
double min;
double nSpaces;
int n = Integer.parseInt(args);
System.out.println(" "+args+" "+args+" "+args);
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("*");
}
}
}
}  Reply With Quote

2. ## 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?  Reply With Quote

3. ##  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. Goldest  Reply With Quote

4. Member Join Date
Jun 2011
Posts
10
Rep Power
0

## Thank you both! I figured it out and turned in the project :)  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•