just wondering what am i doing wrong here ? i am trying to Create a Random Number from 1-45 and add that to the Array and from there output it to my Edit Text Fields.
The first snippet won't compile because it uses a variable that is not defined - i.
The second has two problems - it runs as long as "i<=rand.size()", which is one iteration too long ("i<rand.size()" would be the correct clause), but even then it doesn't achieve the objective since it sets the same value in all 6 fields (which I'm guessing is not what you want).
I advise to use the first code snippet with "i" replaced by 0 to 5, respectively. Since the names of the 6 fields are hardcoded, you can't easily treat them in a loop anyway.
So for me to be able to Generate a Random Number from 0 - 45 six times and store that into my ArrayList
And then pass it on from its java.class to my MyActivity Class.
im declearing my RandomMath Page Class by
RandomMath page = new RandomMath();
i am also declearing my Arraylist. and the Method getList()
ArrayList<Integer> rand = page.getList();
Ulf Dittmer wrote:I'm not sure if that's meant as a question (and if it is, what the question might be). Does it work? At a cursory glance it seems that it should.
ok it could be that i was getting a
Null Pointer Exception on Randommath.class
Oh right, there are a couple of problems in that class. You're not creating an object in line 7 for "myList" to point to. That's why line 15 would throw an NPE.
And in line 14/15 you're clobbering the value of the loop variable "i". Something like "myList.add(r.nextInt(45))" would fix that.
I think there's now a problem in line 13 - the loop will run indefinitely, because "myList.size()" is dynamically evaluated, and will constantly increase. Something like "i < 6" would add exactly 6 elements to the list.
You probably assumed that the "6" in line 7 is the size of the list, but it's not, it's the initial capacity (which is not related to the size of the list).