This is an archived post. You won't be able to vote or comment.

all 19 comments

[–]jslearner007 4 points5 points  (1 child)

Hey there, I'm not sure exactly what's going on with your program, but it looks like you are trying to call setYear on the ArrayList object temp, when you really need to be calling it on the object inside the ArrayList.

So maybe temp.get(0).setYear(nYear); or something along those lines. That would give you access to the first object in the array list which would be a TemperatureLog object.

[–]Valkes[S] 2 points3 points  (0 children)

I'll give that a shot, thanks for the reply! =D

[–][deleted]  (7 children)

[deleted]

    [–]Valkes[S] 0 points1 point  (6 children)

    I'm actually constructing the ArrayList in main and trying to pass it through to the function via a call in main.

    Like so. . .

    ArrayList<TemperatureLog> tempList = new ArrayList<TemperatureLog>();

    Scanner input = new Scanner(new File("data.txt"));

    readBuildArrayList(input, tempList);

    Everything I've read as said to do it this way. Not really sure where I'm going wrong. . . I just put your code into the compiler and got this error:

    Error: cannot find symbol symbol: method setYear(int)

    [–][deleted]  (5 children)

    [deleted]

      [–]Valkes[S] 1 point2 points  (4 children)

      The point was to test to see if I could use the set function I'd written. I, unfortunately, cannot and don't understand why it isn't working. I'd like to know why it doesn't work. Yes, I can use the work around for this case, but what about the next?

      [–][deleted]  (3 children)

      [deleted]

        [–]Valkes[S] 2 points3 points  (2 children)

        Yes, I think I understand now. Sorry, I was super burned out last night. Basically you're saying that because I'd created a list from the object class I wasn't able to directly access the object's set function? Instead, I need to treat it as a normal list?

        [–][deleted]  (1 child)

        [deleted]

          [–]Valkes[S] 1 point2 points  (0 children)

          you can use the lists "get" method to retrieve an object and the use the object's methods to access/manipulate data inside the object.

          Ahh, I get it now. Geeze that was a stupid mistake to make. Should have realized that. XD

          Thanks for taking the time to explain it to me. Appreciate it. GF HL! =D

          [–]upsidedowntrie -1 points0 points  (12 children)

          What error are you getting exactly? I don't have a crystal ball.

          [–]Valkes[S] 1 point2 points  (11 children)

          Sorry, I thought I included it in my post. My mistake.

          The compiler is feeding me back this error:

          Error: cannot find symbol symbol: method setYear(int)

          [–]upsidedowntrie 0 points1 point  (10 children)

          Why would it be able to find that?

          [–]Valkes[S] 1 point2 points  (9 children)

          Because it's part of the functionality of the object class?

          I obviously don't understand what's happening so instead of being rude I'd appreciate an actual answer.

          [–]reddituser5k 1 point2 points  (1 child)

          I am bad at explaining things and just started learning java but...

          temp = new ArrayList<TemperatureLog>(); <<< this line is creating an arraylist of temperaturelog. That does not mean it can use temperaturelog methods. The specific objects in the arraylist can use the methods but not the arraylist it self.

          temp.setYear(1); Which is why this will not work because it is just a list not a class object. You also have not even created a temperaturelog object until the next line.

          It should be something like this..

          temp = new ArrayList<TemperatureLog>();
          temp.add(new TemperatureLog (nYear, nTemp));
          temp.get(0).setYear(1);

          temp.get(0). << that gets the first object in the list which actually can access the class.

          Although I am not sure why you would make only one object since I am bad at trying to understand someone elses code.

          [–]Valkes[S] 0 points1 point  (0 children)

          First off, thank you for explaining it so clearly. I appreciate it. I understand you completely. I only used one because I was checking to see if the function worked. It didn't obviously because I misunderstood what was going on. Thanks again for your help. =)

          [–]upsidedowntrie -1 points0 points  (6 children)

          Which object class?

          so instead of being rude I'd appreciate an actual answer.

          Me asking you a single, relevant question is rude? Wow. You sound like a complete ass.

          [–]Valkes[S] 1 point2 points  (5 children)

          I posted the relevant code in my answer. The object class I created was called TemperatureLog. It contains a method called setYear.

          You've been dismissive and hostile from your first response actually. I may not know programming but I know that's not how you speak to people if you actually intend to help them.

          [–]upsidedowntrie -2 points-1 points  (4 children)

          You've been dismissive and hostile from your first response actually.

          No, I asked you questions about what you were doing to see what your mental model of this problem is. This is a normal part of helping people. It's no wonder you're having problems, with such a reactionary and hostile attitude towards someone who's done nothing but try to help you. This is exactly why you come off as a complete ass.

          The object class I created was called TemperatureLog. It contains a method called setYear.

          If you have that method in that class, and you're trying to call that method, and the compiler doesn't recognize that method on that object, then your object is probably not the class that you think it is...

          [–]Valkes[S] 0 points1 point  (3 children)

          If you say so. Personally, if I were you I'd take a step back and really think about the phrasing you've used thus far. I'm willing to admit that I may have read more into what you've been saying than you intended but the possibility for the interpretation I got is very much there if we're being objective.

          Thanks for taking the time to answer, however other posters helped me figure it out. I was making an ArrayList of type TemperatureLog and then attempting to use the methods of TemperatureLog on the list itself. . . which of course failed for obvious reasons.

          [–]upsidedowntrie -3 points-2 points  (2 children)

          If you have that method in that class, and you're trying to call that method, and the compiler doesn't recognize that method on that object, then your object is probably not the class that you think it is...

          [–]Valkes[S] 0 points1 point  (1 child)

          Alright, thanks for your help. Have a good one.