• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I get my boolean to return true or false and how do I fix error?  RSS feed

 
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, so I have to create some methods for my magic square class. However, I have run into some issues with my boolean logic.  I keep getting the error "method rowSum in class MagicSquare cannot be applied to given types; required int; found no arguments; reason: actual and formal argument lists differ in length. How to I fix this error? Im trying to get my boolean logic to work that if all sums, up diagonal, down diagonal, row sum and column sum  are all equal, the magic square is true. If all those are not true, it should return false. I havent had a chance to test my logic for this since I can't get past this error, but is my logic for all my sums actually going to work, or am I at a brick wall even after I fix this error? Here is my code:

 
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have defined a method with 1 argument and calling that with no arguments provided. Not allowed in Java.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well in line 78 you are calling rowSum() and colSum() without any integer parameter but actually signature is rowSum(int a). means call the method in line 78 with some integer parameter and the things will be all good! hope this helps.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try and add an int in the parameters, like int row or int col, it says .class expected.
 
Sheriff
Posts: 22845
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you must not have "added the int" correctly, right? Anyway one thing to know about Java is this: a compiler error message refers to a particular line of code. So if you get a compiler error message, the thing to do is look at the line of code it refers to.

And if you want to ask somebody else about the error message, which is a completely reasonable thing to do, then that somebody else is going to want to look at the line of code it refers to. Right? So...
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Vach wrote:When I try and add an int in the parameters, like int row or int col, it says .class expected.

Can you post the compilation/execution command and the output of that command.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My compiler highlights line 78 when the error message appears
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Tapas Chand, Im sorry, Im not exactly sure what you are asking. Im rather new to java.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Vach wrote:@Tapas Chand, Im sorry, Im not exactly sure what you are asking. Im rather new to java.

How are you compiling the Java file? from command line or how?
If from command line, what is the command you are using?
What is shown in the command line (Exact text) after you run the command?
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am compiling it using the compile button found on BlueJ compiler. All I do to compile my file is I press the compile button. Then at the bottom of my screen, an error message pops up that says '.class' expected. It highlights in yellow line 78 of my code.
 
Paul Clapham
Sheriff
Posts: 22845
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Vach wrote:It highlights in yellow line 78 of my code.


So... anybody who wanted to help you out with that problem would want to look at line 78 of your code, wouldn't they?
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote: So... anybody who wanted to help you out with that problem would want to look at line 78 of your code, wouldn't they?


Yes I believe so. I think thats where the error is occurring.
 
Paul Clapham
Sheriff
Posts: 22845
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. So if you wanted somebody looking at this forum to help you, they would want to look at line 78 of your code, no? And right now they can't do that because only you can see it.

So nobody looking at this forum can help you yet because they can't see that line of code. But you could fix that problem by copying the line of code and pasting it into your response to this post.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



specifically  is being highlighted by my compiler. Sorry, I didnt realize I was the only one that could see that.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I have already mentioned, you are calling the method with no arguments, but Java is expecting with an argument as specified by the method definition.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I tried what you had said earlier. I had passed in those parameters int row and int col, and just col and row but neither worked. Those are the ones the methods specify, yet they did not work. The compiler only told me that it could not find those variables. So I did try that, but it didn't work.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Vach wrote:Yeah, I tried what you had said earlier. I had passed in those parameters int row and int col, and just col and row but neither worked. Those are the ones the methods specify, yet they did not work. The compiler only told me that it could not find those variables. So I did try that, but it didn't work.

Can you post here the modified code when you tried to pass parameters.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Vach wrote:Yeah, I tried what you had said earlier. I had passed in those parameters int row and int col, and just col and row but neither worked. Those are the ones the methods specify, yet they did not work. The compiler only told me that it could not find those variables. So I did try that, but it didn't work.

The thing about programming is that there are literally millions of ways to do something wrong, and only a few ways to do it right. So when you say "I tried  that, but it didn't work", we have no idea what you actually tried. It does no good for us to guess what you tried and say "well, don't do this". Odds are good you'd say "I didn't do that, I did something else..." and we'd go around and around for a LONG time until we gave up.

Now, even the statement "The compiler only told me that it could not find those variables." is a clue as to what the problem is. you used a variable that didn't exist, or at least didn't exist in scope of where you used it.  But without seeing your actual code, there's not a lot we can do to help. What variable are you passing in, and where is it declared?

You might want to read this web site. Basically, it says you should provide a short, self-contained, complete example that illustrates your problem.  Ideally, I should be able to cut'n'paste your code into my editor/compiler of choice, compile it, and see the exact error you see.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do the two diagSum methods do? I can't see a difference between them.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What do the two diagSum methods do? I can't see a difference between them.


They are supposed to add the sums of the diagonal rows in both directions.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
David Vach wrote:Yeah, I tried what you had said earlier. I had passed in those parameters int row and int col, and just col and row but neither worked. Those are the ones the methods specify, yet they did not work. The compiler only told me that it could not find those variables. So I did try that, but it didn't work.

The thing about programming is that there are literally millions of ways to do something wrong, and only a few ways to do it right. So when you say "I tried  that, but it didn't work", we have no idea what you actually tried. It does no good for us to guess what you tried and say "well, don't do this". Odds are good you'd say "I didn't do that, I did something else..." and we'd go around and around for a LONG time until we gave up.

Now, even the statement "The compiler only told me that it could not find those variables." is a clue as to what the problem is. you used a variable that didn't exist, or at least didn't exist in scope of where you used it.  But without seeing your actual code, there's not a lot we can do to help. What variable are you passing in, and where is it declared?

You might want to read this web site. Basically, it says you should provide a short, self-contained, complete example that illustrates your problem.  Ideally, I should be able to cut'n'paste your code into my editor/compiler of choice, compile it, and see the exact error you see.


I will definitely check out that website! Thanks! Also, as I have gone through my coding course, I have realized exactly that. I probably have done more things wrong in my short time coding then I will ever do right. However, that's what I am currently finding so satisfying about coding. When I finally find a solution to a problem, it is incredibly satisfying.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas Chand wrote:
David Vach wrote:Yeah, I tried what you had said earlier. I had passed in those parameters int row and int col, and just col and row but neither worked. Those are the ones the methods specify, yet they did not work. The compiler only told me that it could not find those variables. So I did try that, but it didn't work.

Can you post here the modified code when you tried to pass parameters.


I ended up giving up with that version of my boolean. I was having trouble finding the logic in it, so I decided to rewrite it and low and behold, it actually worked, after much tweaking.
Here is the new code I ended up working out:



it is somewhat along the lines of what I was trying to go for, but it makes a lot more sense now.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ended up going with my gut using for loops in this method. For some reason I was convinced before that I couldn't put a for loop in this method and then use it when passing row and colSum. But when I went with my gut, it ended up working.
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you change the implementation of your diagonal sums? Because one is wrong... They both return the sum of only one of the diagonals. (Check what fields go into that sum... grid[0][0], grid[1][1], ... , grid[n][n] in both implementations. )
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christian Dillinger wrote:Did you change the implementation of your diagonal sums? Because one is wrong... They both return the sum of only one of the diagonals. (Check what fields go into that sum... grid[0][0], grid[1][1], ... , grid[n][n] in both implementations. )


Yeah, I did some more research regarding creating diagonal sums from 2d arrays and I came up with these methods, which appear to work. Although I somewhat understand how they work, I am still a little bit confused by how they work. I have been getting my desired output now, but could someone explain to me how these methods exactly work? I have a basic idea but I'm still a little fuzzy. Here are the methods I came up with:



Specifically the confusing parts are these two lines of code:  and I kind of get why these work, but I dont exactly get what they are doing to get my desired function.
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe visualizing it like this will help: (This is diagonally down to the right)
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah visualizing it definitely helps. Thanks!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!