Two Laptop Bag*
The moose likes Beginning Java and the fly likes help plz..wrapper classes .. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help plz..wrapper classes .. " Watch "help plz..wrapper classes .. " New topic
Author

help plz..wrapper classes ..

kieran pattni
Ranch Hand

Joined: Jan 19, 2004
Posts: 47
please can anyone tell me why this doesnt work...thanks advance





public class ByteAndShort
{
public static void main(String[]args)
{

Byte b = new Byte((byte)25);
float f = b.floatValue(); // convert byte to float
short s = 0;

InputStreamReader is = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(is);

System.out.println("Enter a short number > ");
try
{
String inStr = in.readLine(); // read a string
s = Short.parseShort(inStr); // convert to a short
}
catch (Exception e){} // empty catch block

System.out.println(f + " " + s); // output the results
}

}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I guess that depends on your definition of "doesn't work." If you mean why doesn't it compile, it actually looks fine except that it's missing an import statement for BufferedReader. If you mean that it behaves differently than you expect, it would be helpful if you told us what you expect, and what happens instead.

Note that this program contains what I consider to be the most serious evil a Java programmer can commit: an empty catch block. If the parse fails, this will just hide the error and leave the user mystified; you need to print an error message in that catch block. Perhaps this empty catch is actually the source of the behavior you don't understand!


[Jess in Action][AskingGoodQuestions]
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
First follow Ernest's advice by adding

import java.io.*;

you should see some difference.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: help plz..wrapper classes ..