Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi - I am a genuine newbie to Java

Status
Not open for further replies.

Knackers

Technical User
Apr 26, 2001
59
AU
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:
----------------------------------------------------------
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);
As you would expect this throws a compile time error, but I do not know how to get around it? Any ideas?
 
Hi,

As you mention, Java is a strongly typed language. This means that you cannot pass the wrong data type to a method. I suggest you check the API documentation, especially the java.lang section, which documents the data types (or rather, the objects which encapsulate data types). Here you can check all of the conversion functions which Java has. Such as, in your example:
Code:
String id;
Integer idInteger;
id = myDog.getInput("Please enter the Dog ID:");
idInteger = new Integer(id);
myDog.setId(idInteger);

Alternatively, you could use the valueOf() method of the Integer object to convert a String object to an Integer object.
This is exactly the problem I had when learning Java, learning to think of everything as an object, even basic data types!!

David
 
Hi Knackers,

you might want to rename the getInput method to getInputString so that it can be reused. here's how a getInputInt can be done :

public String getInputString(String message) throws IOException
{
String input;
BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
System.out.println(message);
return input = bin.readLine();
}

public int getInputInt(String message) throws IOException,NumberFormatException
{
String inputStr = getInputString(message);
Integer input = new Integer(inputStr);
return input.intValue();
}

here's a simple and working Dog class:

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Dog
{
public Dog()
{
}
public static void main(String[] args)
{
Dog d = new Dog();
String breed = null;
int age = 0;
boolean fierce = false;
double weight = 0;

try
{
breed = d.getInputString("Please Enter the Dog's Breed :");
age = d.getInputInt("What's his age?");
fierce = d.getInputBoolean("Is he fierce? (true/false)");
weight = d.getInputDouble("What's his weight?");
}
catch(IOException e)
{
e.printStackTrace();
}
System.out.println("Breed : " + breed);
System.out.println("Age : " + age);
System.out.println("Fierce: " + fierce);
System.out.println("Weight: " + weight);
}

public String getInputString(String message) throws IOException
{
String input;
BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
System.out.println(message);
return input = bin.readLine();
}

public int getInputInt(String message) throws IOException,NumberFormatException
{
String inputStr = getInputString(message);
Integer input = new Integer(inputStr);
return input.intValue();
}

public boolean getInputBoolean(String message) throws IOException
{
String inputStr = getInputString(message);
Boolean input = new Boolean(inputStr);
return input.booleanValue();
}

public double getInputDouble(String message) throws IOException,NumberFormatException
{
String inputStr = getInputString(message);
Double input = new Double(inputStr);
return input.doubleValue();
}
}

Hope this is what you wanted.
regards,
shahid
 
[bigsmile]
You guys rock.
David - cheers, I have been working with the API and slowly coming to terms with it.

Shahid, excellent post and this is definitely a valid way of solving it. I was hoping to get away with just creating a single method to get input. After a little more thinking about it, I came up with the following:

Code:
String id;
  id = myDog.getInput("Please enter the Dog ID:  ");
  myDog.setId(Integer.parseInt(id));
similarly, for doubles:
Code:
String age;
  age = myDog.getInput("Please enter the Dog age:  ");
  myDog.setAge(Double.parseDouble(age));
[c/ode]
I have also come up with something for booleans:
[code]
String neutered;
neutered = myDog.getInput("Has the dog been neutered?:  ");
 if (neutered.equalsIgnoreCase("yes")){
     myDog.setNeutered(true);
  }
  if (neutered.equalsIgnoreCase("no")){
       myDog.setNeutered(false);
  }
I guess I am only posting this in the hope that it helps someone else.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top