Maynia's Profile
Reputation: 6
Worker
- Group:
- Members
- Active Posts:
- 34 (0.02 per day)
- Joined:
- 17-March 09
- Profile Views:
- 438
- Last Active:
Yesterday, 09:43 AM- Currently:
- Offline
Previous Fields
- Dream Kudos:
- 0
Posts I've Made
-
In Topic: The method is undefined for type string?
Posted 18 Apr 2013
You're trying to call the method setByteToThisString() on a String variable, that you've named 'junk'. The compiler is telling you exactly what you need to know here - there is no method defined on Strings by that name. Perhaps you meant to call it on a different variable? I will admit I'm having difficulty seeing what this code is actually doing overall. -
In Topic: Tic Tac Toe in 2D Array; keeps looping and won't return winner
Posted 15 Apr 2013
I'll certainly give it a crack!
To put it as simply as I can, variables in Java and indeed most programming languages have a scope - this defines where they are 'visible' if you like.
Variables declared at the highest level, inside the class braces ({}) are visible to everything in that class, and indeed (with the 'public' and 'protected' keywords) can be visible even to other classes.
On the other hand, variables declared inside methods, loops and other constructs are local to that construct and cannot be accessed from outside it. This can be confusing when a variable outside a method and one inside are given the same name.
To give an example:
public class ExampleClass { private int integer = 0; private String string = "this is a variable"; private boolean flag = false; public void doAThing() { boolean flag; flag = true; } }
Interrogating the value of flag within the braces of doAThing() will result in true. Interrogating the value of flag outside that method will result in false, because what you're asking for is the declaration outside the method. If we remove this declaration:
private boolean flag = false;
from the initial section, attempting to simply interrogate the value of flag outside the method braces will result in an error, because the variable was declared inside the method and effectively only exists in there.
In your specific case, you were calling the method checkForWinner(), which sends back (returns) the boolean value of its own flag variable so that it can be used outside the method braces. What you weren't doing was assigning that return value to a variable you could 'see' in that scope, or indeed using it at all.
Hopefully that clears this up a bit. -
In Topic: Tic Tac Toe in 2D Array; keeps looping and won't return winner
Posted 15 Apr 2013
I think you've got four different variables called flag, all with different scope, which seems a tad unnecessary. Assuming the method your professor has provided does, in fact, work, the call to checkForWinner equates to a boolean value that can be assigned to a variable, thus:
boolean boolVar = checkForWinner(board)
Alternatively, you can call equality checks on the method itself, thus:
if (checkForWinner(board)) { doAThing(); }
You shouldn't need all these flag variables. -
In Topic: Why are these 2 strings not equal?
Posted 14 Apr 2013
Don't use == to compare Strings, broadly speaking. That compares the equality of the underlying String objects (reference equality), and since you created two of those:
String a = "This is a test!"; String b = "This is a";
they're not equal on the object level. What you want for comparing the contents of Strings is equals(), called on one String and passing in another, thusly:
string1.equals(string2)
-
In Topic: Getter method - Cannot find symbol
Posted 10 Mar 2013
Those variables are local to the constructor, and thus out of scope for the object's methods. If you want to use them outside the constructor, declare them outside of it and change the lines inside it to assignment only.
My Information
- Member Title:
- New D.I.C Head
- Age:
- Age Unknown
- Birthday:
- Birthday Unknown
- Gender:
Contact Information
- E-mail:
- Private
Friends
Maynia hasn't added any friends yet.
|
|


Find Topics
Find Posts
View Reputation Given
|
Comments
Maynia has no profile comments yet. Why not say hello?