1

I have an arraylist of Integers. When there is a button click the data in the text field gets converted and added to the array list as an int. Here is my code for that below

public void onClick(View v) {
            // TODO Auto-generated method stub

            String rate = happy.getText().toString();
            int rating = Integer.valueOf(rate);
            counter += 1;

            if (counter == 10) {
                train();
            }

            if (rating > 0 && rating < 11) {
                happyRating.add(rating);

                test = happyRating.get(0);
                Log.d("Value: ", Integer.toString(test));

            } else {

                InputAlertHandler.post(new Runnable() {
                    public void run() {
                        int duration = Toast.LENGTH_LONG;
                        CharSequence text = "Please enter Happiness Indicator between 1 and 10";
                        Context context = getApplicationContext();
                        inputAlert = Toast
                                .makeText(context, text, duration);
                        inputAlert.show();
                    }
                });
            }
        }

In the function train(); I am getting data from this array list and using it in calculations.

public void train() {

    for (int i = 2; i < 12; i++) {

        if (happyRating.get(i - 1) < happyRating.size()){

            int x, x1, x2, y, y1, y2;
            double learningRate = -0.00002;

            x1 = happyRating.get(i - 1);
            x2 = happyRating.get(i - 2);
            y1 = iteration[i - 1];
            y2 = iteration[i - 2];

            x = x2 - x1;
            y = y2 - y1;

            if (x == 0) {
                slope = 0;
            } else {
                slope = (y2 - y1) / (x2 - x1);
            }
            j++;

            Log.d("J: ", Integer.toString(j));

            double weightAdj = happyRating.get(j) * slope * learningRate;

            weighting = (weighting + weightAdj);

            Log.d("WEIGHTING: ", Double.toString(weighting));// break;

        }
        else
        {
            break;
        }

    }

As I am using slope for my calculations I am getting the slope of the previous two points, so that's why I'm setting int i = 2 in my for loop.

I've put in the check to ensure I don't get an Index out of bounds exception. My problem is, neither J nor WEIGHTING are showing up in my logcat. Now whether this means they're not calculated or what I don't know, but they are initialised as two globals so at least that value should print? I need to figure this out as soon as possible so if there's anyone out there that could shed some light on this, I'm sure it's something simple, but I've been looking at this project for so long my eyes hurt!!!

Thanks!

4

2 回答 2

0

代替

      Log.d("WEIGHTING: ", Double.toString(weighting));// break;

在 logcat 控制台中声明一个关键字,例如“输出”和写入:

     Log.d("output","WEIGHTING: "+Double.toString(weighting));// break;

Log的第一个参数。必须是您的 logcat 输出的关键字。

编辑

Logcat 似乎不是问题。所以,问题在于 for 循环。如果您的 happyRating 肯定是 10,则会发生以下情况:

        for (int i = 2; i < 12; i++) {

    if (happyRating.get(i - 1) < happyRating.size()){

当 i 达到 11 时,它会尝试获取项目 nr。10 来自happyRating(因为你分 1)。但是没有,最大值是项目编号。9(因为java是从0开始计数的)。你可以在这里达到的最大大小是9,所以如果你需要执行12次for循环,你必须尝试另一种方式

编辑

对不起,我暂时无法测试代码,如果有错误,请不要投反对票,我会在家里测试它。但这也应该工作......

    public void train() {

for (int i = 0; i < happyRating.size()-1; i++) {

    if (happyRating.get(i) < happyRating.size()){

        int x, x1, x2, y, y1, y2;
        double learningRate = -0.00002;

        x1 = happyRating.get(i+1);
        x2 = happyRating.get(i);
        y1 = iteration[i+1];
        y2 = iteration[i];

        x = x2 - x1;
        y = y2 - y1;

        if (x == 0) {
            slope = 0;
        } else {
            slope = (y2 - y1) / (x2 - x1);
        }
        j++;

        Log.d("J: ", Integer.toString(j));

        double weightAdj = happyRating.get(j) * slope * learningRate;

        weighting = (weighting + weightAdj);

        Log.d("WEIGHTING: ", Double.toString(weighting));// break;

    }
    else
    {
        break;
    }

}

在您的代码的此修改中,for 循环仅迭代直到happyRating 达到最大大小-2。到目前为止,我可以看到,您希望在每个循环中获取每个第一项的值并从第一的。这与您的代码之间的区别在于,您可以毫无例外地获得最后一个值。在您的代码中,当循环达到最大值-1 时,您尝试获取一项不存在的happyRating 项。例如,假设整数“i”的值为 10。这就是您的代码所做的:

        x1 = happyRating.get(i - 1);//x1 = happyRating.get(9)
        x2 = happyRating.get(i - 2);//x2 = happyRating.get(8)
        y1 = iteration[i - 1];//y1 = iteration[9]
        y2 = iteration[i - 2];//y2 = iteration[8]

如果您的 happyRating 大小最大为 9,则 x1 希望有一个不存在的值。ArrayList(或数组)的大小与项目编号不同。如果此大小为 9,则最后一项的编号为 8,因为 Java/Android 从 0 开始计数。因此,您无法获得第 9 项。for 循环说,只要“i”小于happyRating.size 就进行迭代()。如果happyRating.size() 为9,“i”迭代到8,然后结束迭代。如果happyRating.size() 为9,这就是我的代码修改所做的:

         x1 = happyRating.get(i+1);//x1=happyRating.get(8)
         x2 = happyRating.get(i);//x2=happyRating.get(7)
         y1 = iteration[i+1];//y1=iteration[8]
         y2 = iteration[i];//y2=iteration[7]

循环只迭代到 7,因为它说 -> 只要 i 小于 happyRating,size()-1 就迭代(小于意味着最大 8 --> -1 意味着 7)。

于 2013-04-22T11:05:15.780 回答
-1

这应该有效:

Log.d("J:", "" + j);

Log.d("加权:", "" + Double.toString(weighting));

于 2013-04-22T11:10:30.930 回答