• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help ! Newbie !  RSS feed

 
Jeremy Chee
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeremy Chee
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it.
Thanks for the help.
 
Dana Bothner-By
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Dana Bothner-By
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheating? What rule did I violate? Ockham's razor gives me a clean shave!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeremy Chee
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".

that method throws an IOException.

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.
 
Jeremy Chee
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!