 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Roedy Green Guest
|
Posted: Thu Jul 22, 2004 5:47 pm Post subject: Re: Problem with arrays |
|
|
On Thu, 22 Jul 2004 18:12:19 GMT, "Tripwire2004"
<tripwire (AT) REMOVE_ME (DOT) ntlworld.com> wrote or quoted :
| Quote: | int [][] coins = new int [2][5];
private int moneyInserted;
private int costOfItem;
public ChangeDispenser () {
int coins [][] = {{5,10,20,50,100},{3,3,3,3,0}};
|
you have two different variables called coins an instance and a local.
you probably mean to say:
coins = {{5,10,20,50,100},{3,3,3,3,0}};
or put that initialiser code on the instance variable, or better still
static final int[][] coins = {{5,10,20,50,100},{3,3,3,3,0}};
What you did is one of my most common bugs. Happily, Jikes warns me
about it. see http://mindprod.com/jgloss/jikes.html
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
|
|
| Back to top |
|
 |
Tripwire2004 Guest
|
Posted: Thu Jul 22, 2004 6:12 pm Post subject: Problem with arrays |
|
|
Hi,
I've been working through some exercises trying to learn how to code Java
into classes with constructors and everything, but I seem to be struggling
with even the simplest of programs. I've worked out how to send integers
and strings between the classes, but I am having serious trouble working
with arrays between the classes. I've managed to fill an array with some
data, but it is wrong. It should contain the values 5,10,20,50,100 but
instead it contains 10,3,0,0,0. I am sure that it will be something
simple I've forgotten and I'm hoping that someone here can help me. I
have included an example of the code which should demonstrate my problem.
Thanks
Elaine
/*******************************************************************/
import javax.swing.JOptionPane;
class ChangeDis
{
int [][] coins = new int [2][5];
private int moneyInserted;
private int costOfItem;
public ChangeDispenser () {
int coins [][] = {{5,10,20,50,100},{3,3,3,3,0}};
moneyInserted = 0;
costOfItem = 0;
}
public int[] getCoins() {
int coincount[] = new int[5];
for (int i = 0; i < coincount.length; i++) {
coincount[i] = coins[1][i];
}
return coincount;
}
public void addMoney(int coin) {
/**This method needs to add a coin value to moneyInserted. The
appropriate element of coins[][] needs to be updated, if no match is
found with the coin values 5,10,20,50,100 then the routine exits and
moneyInserted is not updated
**/
boolean printed = false;
for (int i = 0; i < coins.length; i++) {
if (coins[0][i] == coin) {
coins[1][i]++;
printed = true;
}
}
if (!printed)
JOptionPane.showMessageDialog(null,"Incorrect coin amount");
else
moneyInserted += coin;
}
}
class UseChangeDis
{
public static void main(String[] args) {
int coincount[];
int coinvals[];
ChangeDispenser obj = new ChangeDispenser();
coincount = obj.getCoins();
for (int k = 0; k < coincount.length; k++) {
System.out.println(coincount[k] +" ");
}
System.out.println();
obj.addMoney(20);
for (int k = 0; k < coincount.length; k++) {
System.out.print(coincount[k] +" ");
}
System.out.println();
System.exit(0);
}
}
|
|
| Back to top |
|
 |
Tripwire2004 Guest
|
Posted: Thu Jul 22, 2004 7:25 pm Post subject: Re: Problem with arrays |
|
|
"Roedy Green" <look-on (AT) mindprod (DOT) com.invalid> wrote
| Quote: | "Tripwire2004" <tripwire (AT) REMOVE_ME (DOT) ntlworld.com> wrote or quote:
int [][] coins = new int [2][5];
private int moneyInserted;
private int costOfItem;
public ChangeDispenser () {
int coins [][] = {{5,10,20,50,100},{3,3,3,3,0}};
you have two different variables called coins an instance and a local.
you probably mean to say:
coins = {{5,10,20,50,100},{3,3,3,3,0}};
or put that initialiser code on the instance variable, or better still
static final int[][] coins = {{5,10,20,50,100},{3,3,3,3,0}};
|
Yes, I thought I wouldn't need to input this data into coins twice. I
haven't quite got the hang of instance variables/local etc and that is why
I am doing these exercises.
This doesn't solve my main problem though. My main problem is with the
data held in the array - it doesn't display as I want it to. The output
from the program gives me 10,3,0,0,0 when it should print out
5,10,20,50,100. I can't understand why it is not printing this out.
| Quote: |
What you did is one of my most common bugs. Happily, Jikes warns me
about it. see http://mindprod.com/jgloss/jikes.html
I have downloaded Jikes and I will check it out. Hopefully it will help |
me to understand all of the error messages I have been getting.
Thanks
Elaine
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|