Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help plz..wrapper classes ..

 
kieran pattni
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First follow Ernest's advice by adding

import java.io.*;

you should see some difference.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic