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


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: HELP!!! How To........

  1. #1
    Join Date
    Jan 2005
    Posts
    3

    Lightbulb 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.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Try the Integer.parseInt(String s, int radix) method.

    Like:

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

    (copied from the sun doc )

    I'm not sure what you mean by wightage though...
    eschew obfuscation

  3. #3
    Join Date
    Jan 2005
    Posts
    3
    Thanks for the reply...

    its weightage...my mistake :"

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    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.

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Uups , of course thats obvious

    But then i hope they mean integer/long (no decimals)..hehe
    eschew obfuscation

  6. #6
    Join Date
    Feb 2004
    Posts
    541
    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.

  7. #7
    Join Date
    Jan 2005
    Posts
    3
    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.


  8. #8
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    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();
        .
        .
    }
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center