0

I am new to Java and was wondering if anyone would help me out with the logic below. What am trying to do is to iterate over a list and store each list element into Array. Once that's done, then I want to check the array element for correct answers. So for example 3,2,1 is enter then the final score should be 3. Here is the code.

Code

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

public class IterateOverArray {

public static int SCORE = 0;
static List<Integer> list = new ArrayList<Integer>();
static int[] temp;
public static void main(String[] args) throws Exception {

    list.clear();
    list.add(3);
    list.add(2);
    list.add(1);
    getScore();
}

public static void getScore() throws RemoteException {

    temp = new int[3];

    for (Integer value : list) {
        for (int i = 0; i < temp.length; i++) {
            temp[i] += value.intValue();
        }
        if (temp[0] == 3) {
            SCORE++;
        }
        if (temp[1] == 2) {
            SCORE++;
        }
        if (temp[2] == 1) {
            SCORE++;
        }
    }
    System.out.println("total score: " + SCORE); // Final Score should be 3!
}
}

Many Thanks!

2
  • It is unclear what you are asking exactly; e.g. ...for correct answers... - what does it mean: correct? Commented Mar 5, 2013 at 23:19
  • Am basically trying to work on question and answer assignment. The user answer three question and the answers are 3,2,1. Then I put a check with the if statement; if answer 1 at index[0] = 3, then we increment the score + 1, if answer 2 at index[1] == 2, then again increment score by etc and print score at the end.<br> Remember answers are coming from the list.add(3)<br>list.add(2) etc! Commented Mar 5, 2013 at 23:20

3 Answers 3

2

When in doubt with an algorithm: Mental Step-through!

  1. call getScore()
  2. new array tmp, all elements initialized to 0
  3. first iteration, value of value is 3
  4. add 3 to all elements in the array (makes all of them 3)
  5. check if first element in array is 3 --> it is, increase Score
  6. next iteration, value of value is now 2
  7. add 2 to all Elements of the array (makes all of them 5)
  8. ...

You see your flaw here?

The values in tmp will grow higher, and will then never evaluate to true in your if-statements. Did you want to use = instead of += in your inner for-loop?


Here is a sample-implementation:

static List<Integer> list = new ArrayList<Integer>();
// ...
list.add(3);
list.add(2);
list.add(1);
getScore();

public static void getScore() throws RemoteException {
    if (list.get(0) == 3) {
        SCORE++;
    }
    if (list.get(1) == 2) {
        SCORE++;
    }
    if (list.get(2) == 1) {
        SCORE++;
    }
    System.out.println("total score: " + SCORE);
}

A List is just a dynamically growing array. It behaves just like an array and you can get a single item from it by using the get(index)-method (which is the "equivalent" to using [index]).

You don't need to copy your contents, at all. But if you must use an array, you can convert a List to an array by using the List.toArray()-method.

Sign up to request clarification or add additional context in comments.

4 Comments

I want the final score to be 3! please read the comment above for what I want to achieve.
@Learner I know, and this is your problem. After the second iteration, the values in your temp-array are all 5.
how do i make it to print 3 not 5?
@Learner I guess what you're trying to do is copy the contents of the List into an array and then check if the right answer-number was set at the right question-index?
1

I'd just put the correct answers in an array, and compare them as you go. No need to transform things into arrays and whatnot.

int getScore(List<Integer> answers) {
    Integer[] correctAnswers = {3,2,1};
    int score = 0;
    for (int i = 0; i < 3; i++) 
        if (correnctAnswers[i].equals(answers.get(i)))
            score++;
    return score;
}

Comments

0
package farzi;

import java.util.ArrayList;

public class ArrayListToArray 
{
    public static void main (String args[])
    {
        ArrayList<String> arrList = new ArrayList<String>();
        arrList.add("hussi1");
        arrList.add("hussi2");
        arrList.add("hussi3");
        String[] strArray = new String[arrList.size()];
        arrList.toArray(strArray);
        for(int i=0;i<strArray.length;i++)
        {
            System.out.println(strArray[i]);
        }
    }

}

1 Comment

How is this answering my question? Please explain your ans?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.