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 10 of 10

Thread: I cannot get boolean to work properly

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I cannot get boolean to work properly

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package plantnursery;
    import java.util.Scanner;
    /**
     *
     * @author Kl2eativ
     */
    public class Plant {
     
        boolean fragile;
     
        public static void main(String[] args) {
     
            Scanner s = new Scanner(System.in);
            System.out.println("Is it fragile? Yes or No.");
            boolean f = s.nextBoolean();
     
     }
        @Override
       public String toString(){
            return "The Height of this plant is:" + heightInFeet + " . The common name is " + commonName + " . The scientific name is :" + scientificName + " It's price is:" + price + " and it is " + fragile;
        }

    When I execute the program and when it asks me if its fragile or no, w.e i type i get this error:
    [Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextBoolean(Scanner.java:1756)
    at plantnursery.Plant.main(Plant.java:30)
    Java Result: 1]

    and then my tostring does not execute... Help is appriciated before 10, or I will have to submit it like this.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: I cannot get boolean to work properly

    Reason you get error would be because the inputs you provide aren't "true" or "false".
    as you state nextBoolean(), the only correct inputs would be "true" or "false", where "yes","no","maybe" aren't boolean and will throw an InputMismatchException.

    toString() is called when you try printing the object itself, eg:
    MyObjectClass myObj = new MyObjectClass();
    System.out.println(myObj);
    Last edited by newbie; January 17th, 2011 at 04:53 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I cannot get boolean to work properly

    what would be the object that will call toString in this case?? fragile??

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: I cannot get boolean to work properly

    No as fragile is a boolean variable.
    I assumed you had more code that what you posted,
    ie. you have " heightInFeet" in your code which isn't declared anywhere in what you posted.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I cannot get boolean to work properly

    yes here is the complete code:

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package plantnursery;
    import java.util.Scanner;
    /**
     *
     * @author Kl2eativ
     */
    public class Plant {
        double heightInFeet;
        String commonName;
        String scientificName;
        double price;
        boolean fragile;
     
        public static void main(String[] args) {
            System.out.println("Height of the plant in feet.");
            Scanner s = new Scanner(System.in);
            double h = s.nextDouble();
            System.out.println("What is the common name?");
            String cn = s.next();
            System.out.println("What is the scientific name?");
            String sn = s.next();
            System.out.println("What is the price of the plant?");
            double p = s.nextDouble();
            System.out.println("Is it fragile? True if it is fragile or False if it is not.");
            boolean f = s.nextBoolean();
     
     }
        @Override
       public String toString(){
            return "The Height of this plant is:" + heightInFeet + " . The common name is " + commonName + " . The scientific name is :" + scientificName + " It's price is:" + price + " and it is " + fragile;
        }
     
       public Plant (double heightInFeet, String commonName, String scientificName, double price, boolean fragile){
        this.heightInFeet = heightInFeet;
        this.commonName = commonName;
        this.scientificName = scientificName;
        this.price = price;
        this.fragile = fragile;
        }
     
    }

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: I cannot get boolean to work properly

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package plantnursery;
    import java.util.Scanner;
    /**
     *
     * @author Kl2eativ
     */
    public class Plant {
        private double heightInFeet;
        private String commonName;
        private String scientificName;
        private double price;
        private boolean fragile;
     
        public static void main(String[] args) {
            System.out.println("Height of the plant in feet.");
            Scanner s = new Scanner(System.in);
            double h = s.nextDouble();
            System.out.println("What is the common name?");
            String cn = s.next();
            System.out.println("What is the scientific name?");
            String sn = s.next();
            System.out.println("What is the price of the plant?");
            double p = s.nextDouble();
            System.out.println("Is it fragile? True if it is fragile or False if it is not.");
            boolean f = s.nextBoolean();
     
            Plant plant = new Plant(h,cn,sn,p,f);
            System.out.println(plant); //print out object - object calls toString()
     
     }
        @Override
       public String toString(){
            return "The Height of this plant is:" + heightInFeet + " . The common name is " + commonName + " . The scientific name is :" + scientificName + " It's price is:" + price + " and it is " + fragile;
        }
     
       public Plant (double heightInFeet, String commonName, String scientificName, double price, boolean fragile){
        this.heightInFeet = heightInFeet;
        this.commonName = commonName;
        this.scientificName = scientificName;
        this.price = price;
        this.fragile = fragile;
        }
     
    }

    When you run that now, it will use the toString.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  7. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I cannot get boolean to work properly

    So this calls the toString?

    Plant plant = new Plant(h,cn,sn,p,f);
    System.out.println(plant); //print out object - object calls toString()


    and why Plant plant was use twice??

  8. #8
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: I cannot get boolean to work properly

    Whenever an attempt to print an object is made, it automatically calls its toString method, which will print in a format you do not want, with numbers and @ etc. So by overriding toString, it means when you try and print your object, it will automatically call your toString method.

    Plant - The class you want to make an instance of.
    plant = name of object of type Plant.
    = new Plant(x,x,x,x,x) initialise your object with values matching constructor specification.

    lower case plant is just a name for your object.
    could use
    Plant myLovelyObject = new Plant(x,x,x,x,x)
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  9. #9
    Junior Member
    Join Date
    Jan 2011
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I cannot get boolean to work properly

    Awesome.. I wish my teacher would explain like this, since this is an intro class!! Anyways, every time toString is used, i will have to do this same procedure?

  10. #10
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: I cannot get boolean to work properly

    When ever you print out your object directly, you will most likely always need to define the method yourself so that you get the output required for your program.

    When you try and print out an object without defining your own toString method, the object will be printed out in a format similar to "Plant@173a10f" which is just the class name and its hash code value, which you obviously do not want.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. 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
  2. Constructor doesn't fill in array properly
    By Whyareall in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 6th, 2010, 04:24 AM
  3. if else statement not working properly
    By tina G in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 29th, 2010, 08:04 AM
  4. Buffered Reader is not reading my file properly... HELP!
    By mannyT in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: November 8th, 2009, 08:14 PM
  5. Certain Chinese Characters not displayed properly.
    By kerwintang in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 20th, 2009, 08:23 AM