HELP!!! How To........

Printable View

• 01-22-2005, 11:59 PM
miss_mushroom
HELP!!! How To........
Hi all.... I am new in Java Programming and need your help for the following :

Convert binary numbers to decimal.
The program should prompt user to enter the number. Upon clicking the button, the program should display the number, the positional wightage of each digit of the number ( I have done some researched n found that i have to use pow method for this....is this correct??? ) .........and the decimal equivalent of the number.

Please help. I really appreciate it.

Thanks.
• 01-24-2005, 02:12 AM
sjalle
Try the Integer.parseInt(String s, int radix) method.

Like:

Integer.parseInt("1100110", 2) returns 102

(copied from the sun doc :cool: )

I'm not sure what you mean by wightage though...
• 01-24-2005, 11:00 PM
miss_mushroom
Thanks for the reply...

its weightage...my mistake :"
• 01-25-2005, 08:39 AM
mikeBarr81
Just a thought, but i'm assuming that this is a school/university excercise? While sjalle's method is definately the best way to do it if it's not an excercise, it probably defeats the object if it is an excercise. The point will be to write your own algorithm, rather than use one from the API. :)
• 01-25-2005, 12:15 PM
sjalle
Uups , of course thats obvious :rolleyes:

But then i hope they mean integer/long (no decimals)..hehe
• 01-25-2005, 12:46 PM
mikeBarr81
I'm sure they're not being nasty enough to mean decimals :)

Anyway just to stop anyone giving a lengthy discussion here, miss_mushroom has asked the same question on www.codeguru.com and it has quite a few responses.
• 01-25-2005, 11:08 PM
miss_mushroom
hi Mike....

I post my questions almost on
every Java forums i can get my hands on.... ;)

well anyway i've finish compiling my code (although i haven't finish doing the complete assignment yet..)

However there's one problem here i m not getting any results. just the input but no
outpu

Any comments on my code will be highly appreciated.

Still got a month to get this right.

• 01-26-2005, 08:36 AM
sjalle
Whats that paint job doing in there ? You must deliver
the answer in another TextField by the setText() method.

Your main is only for testing, if it is an applet. here is
a way:

Code:

```public static void main(String[] args) {     NumberSystem ns=new NumberSystem();     ns.init();     Frame f=new Frame();     f.setTitle("Testing NumberSystem Applet");     f.setLayout(new BorderLayout());     f.add(ns,BorderLayout.CENTER);     Button closeBtn=new Button("Close");     f.add(closeBtn,BorderLayout.SOUTH);     closeBtn.addActionListener(ns);     f.setVisible(true);     f.pack();   }```
then you test for the Close button in your applets
actionPerformed() like
Code:

```public void actionPerformed(ActionEvent event) {     if (event.getActionCommand().equals("Close")) System.exit(0);  // not for a real applet...     // must be calculate btn     try {       calculate();     }     catch (NumberFormatException ex) {       messageLbl.setText("Invalid number entry");     }   } and change your```
and perform your calculation in a method like

Code:

```private void calculate() throws NumberFormatException {     int base=Integer.parseInt(this.baseField.getText().trim()); // throws for bad number     String numStr=this.numberField.getText().trim();     .     . }```