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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listarray question

Status
Not open for further replies.

ilovelinux2006

Programmer
Jun 6, 2006
32
US
Hello everyone,

If I have the method:
<Li
public Cars findByNumber(String carNumber){
private ListArray<Object> car = new ListArray<Object>();

Now If I convert the String into an Integer:
Integer carnum = new Integer(carNumber);
-- and someone passes letters instead of numbers in the string, I cannot upcast:

Cars whatcar = (Cars)car.get(carnum);

That will come out with an error if the string is anything other than numbers (integers). Is there another way to get a list arrays object other than with an int value, instead a string?
 
What is a ListArray?

You cannot build an Integer with letter, anyway.

Cheers,
Dian
 
First, if this is homework, don't post here. See the terms and conditions of the site.

Second, please use code tags to post code.

Third, what is a Listarray defined as?

Fourth,
Code:
public Cars findByNumber(String carNumber){
 private ListArray<[red]Object[/red]> car = new ListArray<[red]Object[/red]>();
Why Object and not Cars?
If objects, why not use an ArrayList()

Fifth,
Code:
Integer carnum = new Integer(carNumber);
Cars whatcar = (Cars)car.get(carnum);
Er, get in List and ArrayList take int not Integer. Consider:
Code:
[red]try{
  int[/red] carnum = Integer[red].parseInt[/red](carNumber);
  Cars whatcar = (Cars)car.get(carnum);
[red]}catch(NumberFormatException e){
  //do error stuff here
}[/red]
Changes in red.

Integer.parseInt(...) is a static method that returns the numaric value of a string representation of a number, and throws a NumberFormatException if the passed string isn't an integer (read: has letters, punctuation or is a float).

If this homework, and it seems to contrived not to be, please talk to your instructor and get help.

[plug=shameless]
[/plug]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top