File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes What's wrong with my code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What Watch "What New topic
Author

What's wrong with my code?

Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
Hi everybody. I have this code, it compiles well, but the program doesn;t do what I want to, I really dont know where my problem is. So I want to ask for help. This program consist in a two dimension array. The user has to input the data of the array, and the program through a method called "max" find the max value of all the numbers in the array. The problem is when I try this for example: 12
123
12
14
The program output ha a max value 14. Is like it can't read the numbers in the middle. Please any help?

HERE IS MY CODE:

static int x, y;

public static void main (String [] args)
{
c = new Console ();
c.println ("Enter the dimensions first of the array []");
x = c.readInt ();
c.println ("Enter the dimensions first of the array []");
y = c.readInt ();

int [] [] mark = new int [x] [y];
c.println ("Enter the marks for the 2D Array");

LOOP TO STORE THE VALUES THE USER INPUTED FOR MY VARIABLE
------------------------------------------------------------------------
for (int i = 0 ; i < x ; i++)
{
for (int j = 0 ; j < y ; j++)
{
mark [i] [j] = c.readInt ();
}
}
--------------------------------------------------------------------------

c.println ("The best mark is " + Max (mark));

} // main method
-----------------------------------------------------------------

// METHOD TO FIND MY MAX VALUE ON THE ARRAY
-----------------------------------------------------------------
public static int Max (int [] [] list)
{
int maxNumber = list [0] [0];

for (int i = 1 ; i < list.length ; i++)
{
for (int j = 0 ; j < y ; j++)
{
maxNumber = Math.max (maxNumber, list [i] [j]);
}
}
return maxNumber;
}
-----------------------------------------------------------------------
[ May 12, 2005: Message edited by: Manuel Diaz ]

Note: I love programming.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

Next time, please enclose code inside UBB code tags to make your post more readable.

The problem is in the Max method. You are referring to the variable "y" but if you look at your code again, you'll see two different declarations of y. The y being assigned the value you read from the console is not the y being referred to in the Max method.

BTW, by convention, method names should start with a lowercase letter. Also, you really don't have to refer to y in Max.


Junilu - [How to Ask Questions] [How to Answer Questions]
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
So what do I have to do then?. If I dont need the y in Max, what can I use then?.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

Originally posted by Manuel Diaz:
So what do I have to do then?. If I dont need the y in Max, what can I use then?.


Junilu isn't saying that you don't need "y". The issue is that you have 2 of them. Which variable is the correct one?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
The first one is the correct I think. I dont get something. Why do you call only one y the correct one. Here I will explain a little more what Im doing in the program.

AS you can see, the user can specify the size of the variable through x and y, so my array is: int [][] mark = new int [x][y]. Now, I dont know why the program doesn't output what I want. Can you tell me a little more, what I have to do to make it work?. I mean, if I dont have to use the y on the max mehod. What should I write the, to make this statement:
maxNumber = Math.max (maxNumber, list [i][j]).

Please, Im a little stuck on these.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

Basically, your main method initialize y -- which is not the same y that the max method uses. So, the max method use a y that is equal to zero.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

BTW, Junilu is right. There is an alternate to using y... but fix the problem first.

Henry
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
Ok, I get that part. So, what can i use then in my max method?. If I can't use the y, then, how can I say:

for (int j = 0 ; j < y ; j++)
{
maxNumber = Math.max (maxNumber, list [i][j]);
}

What can be my y variable, if I want to count all the values on my variable?
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
Manual,

what they are saying is:
you have 2 declarations for x and y.

1. static int x,y; - in the class, making thse two static variables.

2. int x, y; - in your main method. which make them local to main.

Now, because of these 2 declarations, whenever you refer to x and y in the main method, you are referring to the local variables x and y and not the static class variables you declared. So, what is happening is, you are getting the value y = c.readInt (); in the local y which is not available to your Max method. the y available to your Max method is the static y declared as the class variable.

the simplest way to resolve THIS issue would be to not declare x and y again in the main method, i.e., remove the int x, y; line from the code.

that way, wherever you refer to x and y, they will be the static class ones.
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
Is the SAME, the program doesn't work. What's the problem NOW??.
I update my code on the post.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

You have something of an off-by-one error in the loop in the max method.

Visually, here's your array:



Try manually tracing through your code and see if you actually touch all of the array elements in your loop.

BTW, it looks to me like you fell victim to premature optimization: You thought you could save a couple of clock cycles by trying to eliminate a redundant test but instead introduced a bug. The price of finding and correcting a bug like this, of course, is wasted development time. Just something to consider next time. (stepping down from soapbox now )
[ May 12, 2005: Message edited by: Junilu Lacar ]
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
Thanks all for your help, I already know what is my error, is on the max method. I fix it.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

OK, since you found the bug, here's code that does not refer to y at all:

 
wood burning stoves
 
subject: What's wrong with my code?