0

I created a Map like so:

private Vector vaildateTestScore(String persTestCode, String score, List<TSpecialTest> specialTestList, Vector errorVtr) {

        Map<String, TSpecialTest> specialTestmap = new HashMap<String, TSpecialTest>();

        for (TSpecialTest specialTest : specialTestList) {
            specialTestmap.put(persTestCode,specialTest);
        }

        if (specialTestList.get(persTestCode).getPersTestScrQyMin() < score){

            errorVtr.add(specialTestList.get(persTestCode)+" max range is "+" "+specialTestList.get(persTestCode).getPersTestScrQyMax());

            System.out.println("There was an error with > "+specialTestList.get(persTestCode).getPerTestTypeCd());
        }
        else if(specialTestList.get(persTestCode).getPersTestScrQyMax() > score){

            errorVtr.add(specialTestList.get(persTestCode).getPerTestTypeCd()+" min range is "+" "+specialTestList.get(persTestCode).getPersTestScrQyMin());

            System.out.println("There was an error with < "+specialTestList.get(persTestCode).getPerTestTypeCd());
        }

        return errorVtr;
    }

However where ever you see persTestCode my IDE shows a runtime error saying: get(int) in List cannot be applied to java.lang.String

This class is set up by calling the following at the beginning of the class like so:

   SpecialTestImpl specialTest = ComponentBuilder.getInstance().getApplicationContext().getBean(SpecialTestImpl.class);

     List<TSpecialTest> specialTestList = specialTest.specialTestList();

Then the method is called like so:

String persTestCode = elem.replace("scr.", "");
                                String score = request.getParameter(elem);
    Vector errorVtr = new Vector();

    //validates inputted test scores
    vaildateTestScore(persTestCode, score, specialTestList,errorVtr);

--------------------SOLVED--------------------------------

This was a comlete oversite on my side. The tab button and the name similarity got the best of me. This is what I am trying to do.

 private Vector vaildateTestScore(String persTestCode, String score, List<TSpecialTest> specialTestList, Vector errorVtr) {

        Map<String, TSpecialTest> specialTestmap = new HashMap<String, TSpecialTest>();

        for (TSpecialTest specialTest : specialTestList) {
            specialTestmap.put(persTestCode,specialTest);
        }

        if (specialTestmap.get(persTestCode).getPersTestScrQyMax() > Integer.parseInt(score)){

            errorVtr.add(specialTestmap.get(persTestCode)+" max range is "+" "+specialTestmap.get(persTestCode).getPersTestScrQyMax());

            System.out.println("There was an error with > "+specialTestmap.get(persTestCode).getPerTestTypeCd());
        }
        else if(specialTestmap.get(persTestCode).getPersTestScrQyMax() > Integer.parseInt(score)){

            errorVtr.add(specialTestmap.get(persTestCode).getPerTestTypeCd()+" min range is "+" "+specialTestmap.get(persTestCode).getPersTestScrQyMin());

            System.out.println("There was an error with < "+specialTestmap.get(persTestCode).getPerTestTypeCd());
        }

        return errorVtr;
    }
1
  • The string persTestCode after elem.replace("scr.", "") represents an integer value? Commented Jul 22, 2016 at 14:24

6 Answers 6

1

Java List get() method takes an integer as an index into the list, and returns the object at that index. You might be confusing this with your map data structure which you add to at the beginning of the method.

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

Comments

1

List just provide the mehtod of get(int index),you can't use get(instanceName) gain the instance which is named as instanceName . supplement: the way that you use map#put(key,value) are incorrect.i think you should read the java#api carefully.

Comments

1

This was a complete oversite on myside. I let the tab button on my IDE get the best of me since specialTestList and specialTestmap are similiar names.

private Vector vaildateTestScore(String persTestCode, String score, List<TSpecialTest> specialTestList, Vector errorVtr) {

        Map<String, TSpecialTest> specialTestmap = new HashMap<String, TSpecialTest>();

        for (TSpecialTest specialTest : specialTestList) {
            specialTestmap.put(persTestCode,specialTest);
        }

        if (specialTestmap.get(persTestCode).getPersTestScrQyMax() > Integer.parseInt(score)){

            errorVtr.add(specialTestmap.get(persTestCode)+" max range is "+" "+specialTestmap.get(persTestCode).getPersTestScrQyMax());

            System.out.println("There was an error with > "+specialTestmap.get(persTestCode).getPerTestTypeCd());
        }
        else if(specialTestmap.get(persTestCode).getPersTestScrQyMax() > Integer.parseInt(score)){

            errorVtr.add(specialTestmap.get(persTestCode).getPerTestTypeCd()+" min range is "+" "+specialTestmap.get(persTestCode).getPersTestScrQyMin());

            System.out.println("There was an error with < "+specialTestmap.get(persTestCode).getPerTestTypeCd());
        }

        return errorVtr;
    }

Comments

0

specialTestList.get(persTestCode) is trying to call List#get(int) passing in a String, so of course that's going to fail.

Perhaps you meant to use the map specialTestmap instead; it will have a get(String) method, since you created the map as being keyed by String.

Also note that

if (specialTestList.get(persTestCode).getPersTestScrQyMin() < score){

...is comparing what I suspect is an int (the result of getPersTestScrQyMin()) with a String score.

And this loop is quite suspect:

for (TSpecialTest specialTest : specialTestList) {
    specialTestmap.put(persTestCode,specialTest);
}

You're repeatedly putting using the same key value, persTestCode. Subsequent puts will overwrite earlier ones, so the loop doesn't make any sense.

Comments

0

The problem seem very clear.

Using a List<TSPecialTest>, the only get function you can use is :

  • List#get(int)

But you're trying to call the method get with a String as parameter.

Your goal was certainly to get the value from the list attached to the String instead of using the List

1 Comment

@T.J.Crowder Indeed, don't know why I thought so.
0

The List.get() method expects an integer index, you are passing a String value.

 if (specialTestList.get(persTestCode).getPersTestScrQyMin() < score){

https://docs.oracle.com/javase/7/docs/api/java/util/List.html#get(int)

You probably are looking to get an object from your map, like this:

 if (specialTestmap.get(persTestCode).getPersTestScrQyMin() < score){

Comments

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.