Hi there, I'm a student from Singapore learning Java Programming Currently facing some problems on my assignment.
how do i display a variable of value 0000
i tried the following
short oNum = 0000; System.out.println(+ oNum );
it would display as 0 Any way to display all the 4 zeros.
Many Thank in advance.
Jaime M. Tovar
Ranch Hand
Joined: Mar 28, 2005
Posts: 133
posted
0
Try using DecimalFormat. It is an util object that helps to display numbers in the format you need. Look for java api in the web so you can take a look on the documentation.
She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
Jeremy Chee
Greenhorn
Joined: Dec 20, 2006
Posts: 4
posted
0
got it. Thanks for the help.
Dana Bothner-By
Ranch Hand
Joined: Jul 28, 2006
Posts: 37
posted
0
Burkhard Hassel
Ranch Hand
Joined: Aug 25, 2006
Posts: 1274
posted
0
all events occur in real time
Dana Bothner-By
Ranch Hand
Joined: Jul 28, 2006
Posts: 37
posted
0
Cheating? What rule did I violate? Ockham's razor gives me a clean shave!
Originally posted by Dana Bothner-By: Cheating? What rule did I violate? ...
I think Bunkhard is simply pointing out (with a friendly ) that assigning a String literal is one thing, but formatting a primitive value is another.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Jeremy Chee
Greenhorn
Joined: Dec 20, 2006
Posts: 4
posted
0
Haha.. You guys are like the pros man. I get stuck sometimes. Heres another one.
public class ABCInput { public static void main(String[]args) { char c = 'x'; System.out.print("Enter a character:"); c =(char)System.in.read(); System.in.read(); System.in.read(); System.out.println("You entered["+c+"]"); } }
with this, i get an error message on line 7,8 and 9 unreported exception java.io.IOException; must be caught or declared to be thrown
Btw, i'm using Jcreator. The codes may seem simple, coz i'm learning the basics in school.
You should read the Java API. Your line 7 calls System.in.read(). So, look up the System class (it's listed in that bottom left pane in the API). There, you can find that the 'in' is an "InputStream". So, look up that object. it has a method called "read".
Have you read up on error handling and exceptions? There is a quick, easy fix that will haunt you later if you continue using it, and then there is a slightly more complicated but better fix, but that requires some understanding of exceptions.
Never ascribe to malice that which can be adequately explained by stupidity.
Jeremy Chee
Greenhorn
Joined: Dec 20, 2006
Posts: 4
posted
0
Sorry, but i'm just learning the basic, so we're not taught all the way. Is there any way to modify the code to make it work ? That was from a sample code, but i couldnt get it to work. I'm trying to implement it in one of my projects.
Java has a concept called "exceptions". Basically, they are used to help keep your program from dying when something goes horribly awry. When "Something Bad Happens", a special object is created, and you have to deal with it.
The problem you are seeing is that the method you want to use, the read() method, is defined as one that can throw an IOException. You can't change the fact that this method MIGHT throw that exception, when "Something Bad Happens".
If you want to use this method, you have to tell Java that you know this exception might be thrown, and then you have to deal with it somehow.
the simplest way would be to wrap your three calls to read() in a try/catch block, and then go from there. something like this:
this should let you get past the compilation error you are seeing (although it might need to be tweaked a little - this is just off the cuff). Then, when "Something Bad Happens", you should see that line print, and you will know why things don't go as planned.
You should read up on error handling, try/catch blocks, and exceptions (they're all related).