I’m sorry if this comes through as a “noob” question, but I’m really stuck and I couldn’t find any help, no matter where I looked.
The problem I’m having is this:
The user fills out a textbox saying how many numbers (doubles) he is going to input next (say n)
I store that number inside the OnClickListener method (that works ok)
public double n;
//CODE
n = Integer.parseInt(input.getText().toString());
I then need to store the n numbers that the user will input
He will fill out the textbox, press enter and the number should be stored in an array
Something like:
int i = n;
double x[] = new double[n];
button1.onClick(View view){
x[i] = Double.parseDouble(input.getText().toString());
i--;
}
No matter how I declare the x[i] I get an error message. I’ve done some debugging and the error pops up whenever storring in the array occurs. I know I’m missed out something basic about this and I feel pretty dumb for getting stuck with this, but I’d really appreciate any help or suggestion you guys might have.
Thanks a lot, Eugene
P.S. I have to declare the array AFTER “n” has been inputted, however when I try to use it in the next OnClickListener, it can’t access it unless it’s declared final, or as a class variable (public/private etc.)
should be
double[] x = new double[n];Example:
Edit:
You need to call
i--;beforex[i] = Double.parseDouble(input.getText().toString());if you callnew double[10]then onlyx[0]throughx[9]are valid pointers.