• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help ! Newbie !

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it.
Thanks for the help.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Dana Bothner-By
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheating? What rule did I violate? Ockham's razor gives me a clean shave!
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic