Can't reassign boolean value!
Hi guys,
I am having an issue with some Java that I have written.
I have a class called Journey and within this class I have some methods. In my code I have a boolean data member set as false by default. Then in a method I change the value of the data member to true if the criteria of an if statement is met.
When I test the code it seems as if the boolean value doesn't get changed to true if the if statement criteria is met which means the boolean value remains as false.
Any ideas regarding what I am doing wrong?
Data members
Code :
private int year = 0;
private int month = 0;
private int day = 0;
private int lastDayOfMonth = 0;
private boolean discount = false;
Methods
Code :
public void lastDay() {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
GregorianCalendar date = new GregorianCalendar( year, month, day );
lastDayOfMonth = date.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
if (day == lastDayOfMonth)
discount = true;
}
Code :
public double getPrice() {
if (discount == true)
return journeyPrices[departing][destination] * 100;
else
return journeyPrices[departing][destination];
}
Re: Can't reassign boolean value!
Why do you think the boolean is being set to true? Add a println statement next to where you are setting it true to verify that it is being set to true. Print out the value of the condition in the if statement to see if it is true or false.
Re: Can't reassign boolean value!
The boolean isn't being set as true which is the issue.
Any dates that I input which should meet the criteria of the if statement don't set the boolean to true. It stays false.
Re: Can't reassign boolean value!
Quote:
The boolean isn't being set as true
Is this statement executed?
discount = true;
If you don't execute the above, the value won't change.
Did the println you put next to it print out anything?
Quote:
should meet the criteria of the if statement
Did you print out the value of the condition tested in the if statement? What printed?
Re: Can't reassign boolean value!
Sorry I don't understand where I am supposed to add the println statements.
And how would the discount=true; executed?
Re: Can't reassign boolean value!
Add the println right after the assignment statement.
The assignment would be executed when the if statement is true. The question is: Is the if statement true?
Print out the values tested in the if to see.
Re: Can't reassign boolean value!
I was confused by what Norm is trying to say when he posted it in my post. Basically, whenever you want to see if a variable is being assigned to something, use a println to say ("Assigned variable is: " + varname) It helps you figure out which part of the code is dysfunctional. Also, if your IDE has a way to step through your program line by line, that also helps with debug.
Re: Can't reassign boolean value!
I have narrowed down the problem to the lastDayOfMonth = date.getActualMaximum(GregorianCalendar.DAY_OF_MON TH); line.
For some reason that line returns 0 for any form of date when ran in the method however when I just test that one line of code in a main program then it works as intended.
Any idea why the line is returning the wrong value?
Re: Can't reassign boolean value!
How are you generating the date object?
Can you show the values of the arguments you are using to create the date?
Post some code that returns the 0 value from the call to getActualMaximum()
Re: Can't reassign boolean value!
Main class
Code :
import java.util.*;
import java.text.*;
import javax.swing.*;
public class MainClass {
public static void main(String[]args){
Scanner scanner = new Scanner(System.in);
Journey journey = new Journey();
System.out.println("Enter the date of travel");
String userSelection = scanner.next();
journey.setDate(userSelection);
System.out.println( journey.lastDayTest() );
}
}
Journey Class
Code :
import java.util.*;
import java.text.*;
import javax.swing.*;
public class Journey {
private int year;
private int month;
private int day;
private int lastDayOfMonth;
private boolean discount = false;
private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
private GregorianCalendar date = new GregorianCalendar( year, month, day );
public void setDate(String userSelection) {
year = Integer.parseInt( userSelection.substring( 6,userSelection.length() ) );
month = Integer.parseInt( userSelection.substring(3,5) ) - 1;
day = Integer.parseInt( userSelection.substring(0,2) );
}
public String getDate() {
return sdf.format( date.getTime() );
}
public void lastDay() {
lastDayOfMonth = date.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
if (day == lastDayOfMonth)
discount = true;
}
public int lastDayTest() {
return lastDayOfMonth;
}
public int dayTest() {
return day;
}
public int monthTest() {
return month;
}
}
Re: Can't reassign boolean value!
Where is the data that this program uses? If you have a console that shows the values of the input and the values of the variables as the program executes, post it. Be sure to include the print out the values of all the variables used in the program.
Otherwise:
Can you write 5-6 lines of code that shows the problem?
Define year, month and day with hardcoded values,
Create the date variable
and get the lastDayOfMonth.
Print out the results that shows the problem.
The rest of the code is not needed for testing.
Re: Can't reassign boolean value!
Even with hardcoded values, 0 is returned for lastDayOfMonth.
I rewrote part of the code just for testing and put it into a single class and it all works fine. lastDayOfMonth returns the correct value for any date that is input however when it is in a separate class as I have previously posted then it doesn't work.
Code :
import java.util.*;
import java.text.*;
import javax.swing.*;
public class Test {
public static void main(String[]args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the date of travel");
String userSelection = scanner.next();
int year = Integer.parseInt( userSelection.substring( 6,userSelection.length() ) );
int month = Integer.parseInt( userSelection.substring(3,5) ) - 1;
int day = Integer.parseInt( userSelection.substring(0,2) );
GregorianCalendar date = new GregorianCalendar( year, month, day );
int lastDayOfMonth = date.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
System.out.println(lastDayOfMonth);
}
}
Re: Can't reassign boolean value!
What is the data that this code uses?
What is the value of userSelection?
Change the code you posted to have it assign a value to userSelection.
Something like this:
String userSelection = "123";
Print out the values of year, month and day so you see all the variables that the code is using.
Re: Can't reassign boolean value!
The data that the program uses is input by the user through a command prompt window. The correct values of year, month and day are being printed.
I have also now narrowed down the issue being related to the date variable but I cannot understand what the issue is.
The date variable seems to be working fine in the main program class but in a separate class with different methods, it doesn't work correctly.
Re: Can't reassign boolean value!
If you can not provide some values for userSelection that shows the problem, there is no way to test the code and try to fix the problem.
Re: Can't reassign boolean value!
I've fixed it!
A couple of lines of code needed rearranging between methods!
Thanks for your help, I'm sure that I wasn't entirely clear with my problems!
Re: Can't reassign boolean value!
Re: Can't reassign boolean value!
A problem I see with your coding technique is that you use too many global variables(class variables). You set the value of a class member in one method and use it in another. Methods set global variables vs returning a value. If you call the methods in the wrong order sometime, values will be changed and bugs introduced
Re: Can't reassign boolean value!
I will agree with Norm. If you only need a quick variable for a function inside a method, instantiate it within the method. Otherwise, if you need multiple methods to use a single variable, or a variable that needs a constant value throughout all it's uses, it's best to make it a class variable. I also create a class in my programs called varLib, wherein I store all the variables that I need throughout the whole program, make them static, and reference them that way. Just another thought. Glad you could fix your program!
Re: Can't reassign boolean value!
I would not recommend using static variables.
Re: Can't reassign boolean value!
Thanks for the advice guys.
I've amended my code so that values that will be used by multiple methods are class variables and any other variable is local to a particular method.