I cannot get boolean to work properly
Code Java:
/*
* 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.
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);
Re: I cannot get boolean to work properly
what would be the object that will call toString in this case?? fragile??
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.
Re: I cannot get boolean to work properly
yes here is the complete code:
Code Java:
/*
* 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;
}
}
Re: I cannot get boolean to work properly
Code java:
/*
* 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.
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??
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)
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?
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.