I've done all the coding for this mid-term exam I got going on, but I've run into this one snag. Mainly, I cannot seem to get my code to throw an error message at a user when they leave one JTextField empty. I think the issue I am having here is that I've declared double variables for each of the JTextFields (with Double.parseDouble) and as far as all my searching around goes, there is no way to check for an empty double value. I've tried a strange workaround where I declare another set of String variables for the same JTextfields (i.e. they already have double variables attributed to them) and then testing for stringVariable.equals("") but that doesn't work either.
I've tried simply going straight from the JTextField - myJTextField.getText().equals("") - and that doesn't throw my error message either.
Keep in mind I am in a beginning Java course so this should be a simple solution.
My code for the relevant section is below:
// IF statement to display error message if no name entered
// OR no value input for any week's Homework
// OR no JCheckBox isSelected
// remember to check against the null string or non-selection of JCheckBox
if ( ( name.equals ( "" ) ) ||
(homework1JTextField.getText().equals(""))||
//Test against incorrect range of values for homework grades
(tut1 < 0 || tut1 > 10)||
(tut2 < 0 || tut2 > 10)||
(tut3 < 0 || tut3 > 10)||
(tut4 < 0 || tut4 > 10)||
(tut5 < 0 || tut5 > 10)||
(tut6 < 0 || tut6 > 10)||
(tut7 < 0 || tut7 > 10)||
(!discussionBoardZeroJCheckBox.isSelected() &&
!discussionBoardOneTwoJCheckBox.isSelected() &&
!discussionBoardThreeFourJCheckBox.isSelected() &&
!discussionBoardFiveSixJCheckBox.isSelected() &&
!discussionBoardSevenJCheckBox.isSelected()
)
)
{
// display error message
JOptionPane.showMessageDialog( null,
"Full Name, all homework points and at least one discussion board checkbox are
all required information!",
"Missing Information", JOptionPane.ERROR_MESSAGE );
}
An easy test is:
if (homework1JTextField.getText().trim().equals("")) {
// throw error
}
I would say why don't you assign intermediate boolean variables for each of those groups of comparisons. Use these booleans in the if.
Print out the values of the booleans since you're if is pretty complex, there's scope for lots of things to go wrong here.
There are various ways to fix the problem. Here is one. Replace JTextField with JTextFieldWithDefault to cause an out of range value (-1) to be returned if the user does not type something or just types spaces. Your existing code remains unchanged and will now work as you expect.
public class JTextFieldWithDefault extends JTextField {
public JTextFieldWithDefault() {
super();
}
#Override
public String getText() {
String s = super.getText();
return s.trim().equals("")?"-1":s;
}
}
Related
I'm attempting the below:
if(p1S > p2S && p3S)
}
But the IDE doesn't like the && here.
So, how would I compare these 3 ints to find the one with the highest value, thus moving forward.
Aka: If Int 1 is greater than int 2, and int 3, then do this. . .
I suspect I just don't know the syntax well enough just yet (I've only just started college for CS).
Is this what you want?
if(p1S > p2S && p1S > p3S)
}
The reason the previous code didn't work is that the first part
if(p1S > p2S && p3S)
}
evaluates to:
if(boolean && p3S)
}
And you can't do an && between a boolean and an int.
protected void pressedOnSpace(int row, int col) {
if (board[row][col] < board[0][0]) {
canvas.errors = false;
}
if (board[row][col] == GoFrame.WHITE) {
board[row][col] = GoFrame.BLACK;
}
else if (board[row][col] == GoFrame.BLACK) {
board[row][col] = GoFrame.EMPTY;
}
else if (board[row][col] == GoFrame.EMPTY) {
board[row][col] = GoFrame.WHITE;
}
}
I am making the game "Go" for a Java assignment. This is a helper method I'm having issues on. The game is based on arrays and loops. The instructions tell me that I am supposed to implement a bound check and there is a boolean variable "canvas.errors" and if it evaluates to true, a big "X" appears over the board, false is there's no "X". I am supposed to do an array bound check to make sure the "X" does not appear but I am unsure how to go about it because everything I have tried continues to produce an "X" when I click on the board outside of the bounds of the array. I know I am supposed to find all the possible ways the program could error and evaluate those ways to "false" with the variable/function but I cant seem to figure it out. The first "if" statement is what I have as of now for the bound checker after a bunch of tries. Any help would be appreciated as I am a newer coder.
I think maybe you just need help structuring your code to perform a very common operation referred to as validation, or in plain English, making sure you handle situations where your user submits invalid information. All you want to do is make sure the X and Y values represent a legitimate cell on your goban so that your code doesn't choke later on when it tries to process an invalid value.
protected void pressedOnSpace(int row, int col) {
if (row >= 0 && row < 20) && (col >= 0 && col < 20) {
// insert your logic for changing color
} else {
// insert your logic for drawing a big fat X over the board
}
}
my Main contains a conveyor, which transports a carton (my agent) from A to B. I added a radio button with which the user can choose the size of the box ("small", "medium", "big" & "very big"). I now want every carton to save the value of the radio button. I tried a lot of different stuff like linking it to a parameter, but I didn't manage to figure it out.
Picture of my model:
the code I used for the radio button is:
if( value == 0 )
radioValue = 1;
else if( value == 1 )
radioValue = 2;
else if( value == 2 )
radioValue = 3;
else if( value == 3 )
radioValue = 4;
radioValue being the parameter in the Main i linked my radio-button to.
How do I give this parameter to the agent and how do I read it out later?
Thanks in advance for helping!
in your Kist agent create a variable called size of type int.
in the source block, in the properties, on the "on at exit" action, under the action section of the properties write the following code:
agent.size=radioValue;
This is one of the most basic things to do in AnyLogic... so I suggest for you to go through a training before even starting to work on any model. This can be done by going through the tutorials, the anylogic in 3 days book or my course here: noorjax.teachable.com/p/anylogic
later you can use agent.size to access the size of your agent, in any of the blocks.
Without seeing more of the code, my general suggestion would be to consider looping through the agents (e.g., with a for loop) and having them all set a local variable cartSize equal to radioValue (I wouldn't use "size" because its also used for agentsets I believe). Just spitballing here, but something like this?
for (int x = 0; x < carton.size(); x++) {
carton.get(x).cartSize = radioValue;
}
Ok i don't know how to exactly explain completely what my issue is i'm facing to get what i want, but the basis of what i'm trying to accomplish here is...i don't want a -> ; <- to show up if the variable is Null or 0. Something I've attempted so far is a scanner input where when you run the code it asks to input values that are > 0 and if you input one thats not it'll give an invalid input error. Im trying to find a different method where its not needed to keep repeating this method for 20 or more. Like i said im just trying to have it input the numbers automatically, and if theres no number in one of the variables it would skip it and not put another " ; " and just put the ones that do have numbers with the semicolon. So what i'm looking at to accomplish is listed in the image bellow :
I had difficulties to understand your question. I am also not an english speaking person....
If I understood you want this:
String a,b,c,d,e, all;
all = "";
if(a!=null && a!=0){
all += a;
}
if(b!=null && b!=0){
all += b;
}
if(c!=null && c!=0){
all += c;
}
...
if(all != ""){
//something
} else {
// something else
}
Note that I am not permutating, I am sequencially checking for values in each variable then performing the desired effect.... I just concatenated strings, if you want to add stuffs like (space),; (semicolon), its up to you.
I tried using an if statement to see if the index returns null, and skips over if it does, but it still keeps getting an array index out of bounds exception.
...
if(tiles[x+a][y+b] == null){
System.out.println("Would have been an error");
} else if(tiles[x+a][y+b].getType() == type) {
tiles[x+a][y+b].setTile(type);
} else {
System.out.println("Not found");
}
...
Mainly the first two lines are what the question's about. I'm trying to get it so that it goes through the array, but skips over anything that would get an array index out of bounds exception. If you need anymore background info, please let me know. I tried to cut it down as much as I could.
How about something like:
if ( ( (x+a) >= tiles.length ) || ( (y+b) >= tiles[x+a].length ) )
as your test?