DavidA - 8 months ago 49

Java Question

For one of my assignments, I need to modify the original code to work with a string that will be passed to another function so i'm converting this for loop to a while loop, but I cant seem to get it right. Where am I going wrong?

`public void smallestError(double[] errorCalculation, double[] mean, double[][] ratios, int length) {`

double small = errorCalculation[0];

computedRatios = new double[length];

int holder = 0;

//int x = 0;

for (int i =0; i<7; i++)

{

if(errorCalculation[i] < small)

{

small = errorCalculation[i];

holder = i;

}

}

computedError = small;

computedRatios = new double[length];

for (int x = 0; x < length; x++) {

computedRatios[x] = ratios[holder][x];

}

//String str1 = String.valueOf(holder);

// bigODeter = str1;

bigODeter = holder;

}

My attempt:

`public void smallestError(double[] errorCalculation, double[] mean, double[][] ratios, int length) {`

double small = errorCalculation[0];

computedRatios = new double[length];

int holder = 0;

int x = 0;

/*for (int x = 0; x < 7; x++) {

if (errorCalculation[x] < small) {

small = errorCalculation[x];

holder = x;

}

}*/

while (x == 0) {

if (errorCalculation[x] < small) {

small = errorCalculation[x];

}

holder = x;

}

while (x == 1) {

if (errorCalculation[x] < small) {

small = errorCalculation[x];

holder = x;

}

}

while (x == 2) {

if (errorCalculation[x] < small) {

small = errorCalculation[x];

holder = x;

}

}

while (x == 3) {

if (errorCalculation[x] < small) {

small = errorCalculation[x];

holder = x;

}

}

while (x == 4) {

if (errorCalculation[x] < small) {

small = errorCalculation[x];

holder = x;

}

}

while (x == 5) {

if (errorCalculation[x] < small) {

small = errorCalculation[x];

holder = x;

}

}

while (x == 6) {

if (errorCalculation[x] < small) {

small = errorCalculation[x];

holder = x;

}

computedError = small;

computedRatios = new double[length];

for (x = 0; x < length; x++) {

computedRatios[x] = ratios[holder][x];

}

String str1 = String.valueOf(holder);

bigODeter = str1;

}

}

Also, I had to convert the holder from an int to a String because we have to convert it.

Answer Source

Try this one...

```
public void smallestError(double[] errorCalculation, double[] mean, double[][] ratios, int length) {
double small = errorCalculation[0];
computedRatios = new double[length];
int holder = 0;
//int x = 0;
int i =0;
while (i<7)
{
if(errorCalculation[i] < small)
{
small = errorCalculation[i];
holder = i;
}
i++;
}
computedError = small;
computedRatios = new double[length];
int x= 0;
while (x < length){
computedRatios[x] = ratios[holder][x];
x++;
}
//String str1 = String.valueOf(holder);
// bigODeter = str1;
bigODeter = holder;
}
```