wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes String and Numeric Parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String and Numeric Parameters" Watch "String and Numeric Parameters" New topic
Author

String and Numeric Parameters

Henry Bueno
Greenhorn

Joined: Apr 03, 2003
Posts: 8
I am trying to get a method to accept parameters which might be String or int... I want the end result to be String.
My code currently looks like this:
//add first row to table
TableRow row = new TableRow();
row.set("firstName", "Mickey");
row.set("lastName", "Mouse");
row.set("phone", "407-555-1212");
row.set("age", 50);
table.addRow( row );
//add second row to table
row = new TableRow();
row.set("firstName", "Donald");
row.set("lastName", "Duck");
row.set("phone", "800-555-1212");
row.set("age", 40);
table.addRow( row );
---------------------------
the following is the method which I want to accept Strings and int values and then convert them to Strings. I know I will end up using toString() method but I am stuck on what class object/primitive type to assign p1 and p2 on the receiving end.
---------------------------
public void set(String p1, String p2){

data.put(p1, p2);
}
----------------------------
below is the compilable code
----------------------------
[ edited to help preserve formatting using the [code] and [/code] UBB tags -ds ]
[ April 08, 2003: Message edited by: Dirk Schreckmann ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
To convert int data into String data, you can simply call the function like this:

However, there are probably better ways to design this program. Of course, if this is a school assignment, you have to follow the instructions. If it isn't, though, you should look into use some of Java's database tools.
If you really need to keep your own Table class. It may be more efficient to allow storing various types of Objects in it, instead of just Strings. For example, you can use Integer objects to store int primitives. You then simply need to write a set() method that takes String and Integer parameters.
HTH
Layne


Java API Documentation
The Java Tutorial
Henry Bueno
Greenhorn

Joined: Apr 03, 2003
Posts: 8
But is there something to accept both (String and int) as entry parameters and then later convert them to Strings?
I have been trying to find something in the API's, but I can't find it.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Yah, but it isn't exactly an API. In fact, there are several different solutions that I can think of.
First, you can make two different methods:

This makes it so you can pass an int directly and then it converts the int into a String.
Alternatively, you can use the Integer class to store your data. In fact, I think I would prefer this since you have to use the Integer class to convert a String back to an int anyway:

The main idea behind this method is that the HashMap class only stores Objects. Both String and Integer inherit from the Object class and can be stored in the HashMap with equal ease. In fact, the HashMap can contain a pair of Strings in one entry and a String, Integer pair in another entry.
However, pulling the data back out of the HashMap may cause problems. You will need to know which class to cast it back to, if you need to access any of its methods directly.
HTH
Layne
p.s. I am only scratching the surface of a much more complicated subject. For more information, you should find a text book or article that explains the Collections API in more detail.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String and Numeric Parameters