Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: Can't reassign boolean value!

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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
    	private int year = 0;
    	private int month = 0;
    	private int day = 0;
    	private int lastDayOfMonth = 0;
    	private boolean discount = false;

    Methods
            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;
    	}

    	public double getPrice() {
    		if (discount == true)
    		return journeyPrices[departing][destination] * 100;
    		else
    		return journeyPrices[departing][destination];
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Can't reassign boolean value!

    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?

    should meet the criteria of the if statement
    Did you print out the value of the condition tested in the if statement? What printed?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member Blackrabbitjack's Avatar
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Fine
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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.
    Blackjack

  8. #8
    Junior Member
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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()
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Can't reassign boolean value!

    Main class

    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

    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;
    	}
    }

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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.

    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);
    	}	
    }

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  16. The Following User Says Thank You to Norm For This Useful Post:

    IanSawyer (March 19th, 2012)

  17. #16
    Junior Member
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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!

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Can't reassign boolean value!

    Glad you got it fixed.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  20. The Following User Says Thank You to Norm For This Useful Post:

    IanSawyer (March 20th, 2012)

  21. #19
    Junior Member Blackrabbitjack's Avatar
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Fine
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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!
    Blackjack

  22. The Following User Says Thank You to Blackrabbitjack For This Useful Post:

    IanSawyer (March 20th, 2012)

  23. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Can't reassign boolean value!

    I would not recommend using static variables.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #21
    Junior Member
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. [SOLVED] What's wrong with this boolean?
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 8th, 2011, 06:41 PM
  2. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM
  3. Some help with boolean
    By JPetroSS in forum Java Theory & Questions
    Replies: 3
    Last Post: November 2nd, 2010, 05:38 AM
  4. Need help with what I believe is Boolean & add branching
    By JavaBeginner123 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 1st, 2010, 01:55 PM
  5. [SOLVED] Modification of Boolean function in Java program
    By lotus in forum Java SE APIs
    Replies: 4
    Last Post: July 10th, 2009, 10:15 AM