9 Replies - 725 Views - Last Post: 16 November 2017 - 10:20 AM Rate Topic: -----

#1 nosba  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-October 17

Loop through arraylist & return value if 2 params are met using ge

Posted 26 October 2017 - 08:30 PM

The full question is: How do I iterate through an arraylist and return a value if two parameters are met, all within a get method?

In the GameGrid class, I have an Arraylist 'info' with each DigSpot object in the list having three values: xcoord, ycoord, and winnings. I need to go through the arraylist and check to see if xcoord and ycoord match - if they do, then it returns winnings.

I feel like I understand what's being done but I don't know how to go about doing it. I figure I need to use some kind of loop - not sure whether it's a for loop or for each loop, as well as a nested conditional to check for equality (if xcoord and ycoord both match) and then return winnings, otherwise null? Can't find anything on getters passing two parameters and returning a value, let alone anything more complex.

The get method is in the GameGrid class and only have parameters xcoord and ycoord. The xcoord, ycoord, and winnings variables are in the DigSpot class with getters and setters but I'm not sure how to access it. I need a set method too but I might be able to figure it out once I complete the get method.

public class DigSpot{	
	private int xcoord;
	private int ycoord;
	private double winnings;
	
	
	public DigSpot(int x, int y, double w) {
		setXcoord(x);
		setYcoord(y);
		winnings = w;
	}

	public void setXcoord(int x) {
		xcoord = Math.max(0, x);
	}
	
	public void setYcoord(int y) {
		ycoord = Math.max(0, y);
	}
	
	public void setWinnings(double w){
		winnings = w;
	}
	
	public int getXcoord() {
		return x;
	}
	
	public int getYcoord() {
		return y;
	}

	public double getWinnings() {
		return w;
	}

public class GameGrid{
	private ArrayList<DigSpot> info;
	private String name;

	public GameGrid(String name){
		info = new ArrayList<DigSpot>();
		this.name = name;
	}

	public ArrayList<DigSpot> getInfo() {
		return info;
	}
	
	public String getName() {
		return name;
	}

	public Double get(int xcoord, int ycoord) {
		
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Loop through arraylist & return value if 2 params are met using ge

#2 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,473
  • Joined: 13-June 14

Re: Loop through arraylist & return value if 2 params are met using ge

Posted 26 October 2017 - 10:45 PM

View Postnosba, on 27 October 2017 - 04:30 AM, said:

I feel like I understand what's being done but I don't know how to go about doing it. I figure I need to use some kind of loop - not sure whether it's a for loop or for each loop, as well as a nested conditional to check for equality (if xcoord and ycoord both match) and then return winnings, otherwise null?


That sounds reasonable, so why not try it out? Surely you must have used collections (e.g. ArrayLists before, so you should know how to iterate over them.

Quote

Can't find anything on getters passing two parameters and returning a value, let alone anything more complex.


I'm not sure what there is to find,presumably on the Internet or in a book. Those resources aren't going to have answers to very specific questions like this. When you learn programming, you learn in general about methods and loops and so on and it's up to you as the programmer to make use of them to solve the problem at hand. Of course methods can take multiple parameters if they need to and have loops and whatever else in them.

Quote

The xcoord, ycoord, and winnings variables are in the DigSpot class with getters and setters but I'm not sure how to access it.


Are you saying that given an instance of DigSpot, you don't know how to call methods on it? For example, given

DigSpot d = new DigSpot(1, 2, 3.0);


how would you get d's winnings?
Was This Post Helpful? 0
  • +
  • -

#3 nosba  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-October 17

Re: Loop through arraylist & return value if 2 params are met using ge

Posted 28 October 2017 - 03:00 AM

Quote

The xcoord, ycoord, and winnings variables are in the DigSpot class with getters and setters but I'm not sure how to access it.


Are you saying that given an instance of DigSpot, you don't know how to call methods on it? For example, given

DigSpot d = new DigSpot(1, 2, 3.0);


how would you get d's winnings?
[/quote]

Would that not just be
System.out.println(d.getWinnings());
?
Was This Post Helpful? 0
  • +
  • -

#4 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,473
  • Joined: 13-June 14

Re: Loop through arraylist & return value if 2 params are met using ge

Posted 28 October 2017 - 03:02 AM

Well, of course the printing is just extra, but yes, you'd call the method as you have. Good, now what about the rest? Have you tried iterating over the collection and doing what you need to? Show us your attempt if you're still stuck.
Was This Post Helpful? 0
  • +
  • -

#5 nosba  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-October 17

Re: Loop through arraylist & return value if 2 params are met using ge

Posted 28 October 2017 - 05:52 AM

I still don't know what I'm doing. I know there's a clear issue with the get methods but it may just be the entire thing.

  public Double get(int xcoord, int ycoord) {
    Iterator itr = info.iterator();
      while(itr.hasNext())
        if(info.getXcoord() == xcoord && info.getYcoord() == ycoord)
    	  return info.getWinnings();
    	    else
    	  return null;
  }

Was This Post Helpful? 0
  • +
  • -

#6 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,473
  • Joined: 13-June 14

Re: Loop through arraylist & return value if 2 params are met using ge

Posted 29 October 2017 - 04:14 AM

You need to tell us what the issue is - that small sample of code really doesn't tell us anything at all. Are you getting compiler errors? If so, what are they and what do you think they're telling you?

You might also want to show the rest of your code, so people can see how this get method is being used. Along with that, if the program is compiling but is not working correctly, tell us what and what you've done to try and work out what's causing the problem.
Was This Post Helpful? 0
  • +
  • -

#7 nosba  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-October 17

Re: Loop through arraylist & return value if 2 params are met using ge

Posted 29 October 2017 - 08:33 PM

I thought there would be glaring errors in the method. There really isn't much more than what I've posted. I'm getting errors when I call the get methods. eg. "The method getXcoord() is undefined for the type ArrayList<DigSpot>" with the option to cast to 'info'. Of course, I can't cast DigSpot to ArrayList<DigSpot>. Not sure of what I'm missing

I'm not too concerned about the output at this point; just want to know what needs to be done to fix the errors.
Was This Post Helpful? 0
  • +
  • -

#8 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,473
  • Joined: 13-June 14

Re: Loop through arraylist & return value if 2 params are met using ge

Posted 30 October 2017 - 11:30 PM

info has type ArrayList<DigSpot> and ArrayList obviously doesn't have a method on it called getXcoord, for example. You need to get the instance of DigSpot at the next position in the ArrayList and call the methods on that (hint: you're using an Iterator to iterate over the collection).

This post has been edited by ndc85430: 30 October 2017 - 11:30 PM

Was This Post Helpful? 0
  • +
  • -

#9 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,473
  • Joined: 13-June 14

Re: Loop through arraylist & return value if 2 params are met using ge

Posted 06 November 2017 - 10:46 PM

Did you get anywhere with this?
Was This Post Helpful? 0
  • +
  • -

#10 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,473
  • Joined: 13-June 14

Re: Loop through arraylist & return value if 2 params are met using ge

Posted 16 November 2017 - 10:20 AM

Also, once you're done, you may consider using Optional instead of returning null to indicate there's no value. I've written a tutorial on this here.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1