Hi - I am a genuine newbie to Java and am having some problems with the basics, mostly to do with types.
I have a method that takes user input from the keyboard and stores it in a String:
----------------------------------------------------------
----------------------------------------------------------
This is a generic method that I want to use to get input for a Strings, ints, doubles and a boolean value. It is inside a class called Dog.
In the main method of the class DogCost1, I am calling the getInput() method of the Dog class, then using the value input to set the value of a variable in the Dog class, for example:
-----------------------------------------------------------
----------------------------------------------------------
However this only works for variables that are strings. I need to be able to set ints, doubles and a boolean in the same sort of way, E.G, where the method setId is expecting an integer
As you would expect this throws a compile time error, but I do not know how to get around it? Any ideas?
I have a method that takes user input from the keyboard and stores it in a String:
----------------------------------------------------------
Code:
public String getInput(String message) throws IOException{
String input;
BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
System.out.println(message);
return input = bin.readLine();
}
This is a generic method that I want to use to get input for a Strings, ints, doubles and a boolean value. It is inside a class called Dog.
In the main method of the class DogCost1, I am calling the getInput() method of the Dog class, then using the value input to set the value of a variable in the Dog class, for example:
-----------------------------------------------------------
Code:
public static void main(String[] args){
try{
Dog myDog = new Dog();
String breed;
breed = myDog.getInput("Please enter the Dog Breed:");
myDog.setBreed(breed);
}.......
However this only works for variables that are strings. I need to be able to set ints, doubles and a boolean in the same sort of way, E.G, where the method setId is expecting an integer
Code:
String id;
id = myDog.getInput("Please enter the Dog ID:");
myDog.setId(id);