Results 1 to 3 of 3
  1. #1
    addictz04 is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Exclamation Algorithm for converting binary/hex to decimal

    We were asked to convert hex/bin to dec without using the parseInt parseDouble through the Scanner class.

    I started using the switch statement. My problem is that I don't know how to loop the number of inputs in the scanner class, since every input out of 6 inputs would have to undergo the process of conversion. I'm trying a while loop but I still cant get it. Second, i tried a switch statement to link to the methods of conversion to the input but it still doesnt work. please help!

    for(int i=1; i<=6; i++){
    if(i<=6){
    String num = s.nextLine();
    String number = num(i).substring(0, num(i).length()-3);
    char each = num(i).charAt(num(i).length()-1);

    switch(each){
    case 'b': System.out.println(c.binToDecimal(num));
    break;
    case 'h': System.out.println(c.hexToDecimal(num));
    break;
    default: System.out.println ("Inconvertible. Please try again. :s");
    }
    }
    else{
    System.out.println("------------------");
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by addictz04 View Post
    Java Code:
                        String number = num(i).substring(0, num(i).length()-3);
    That's one hell of an (incorrect) expression. You were trying a top down approach to your problem and you ended up with an entirely different problem. Try a bottom up approach and try to implement those binToDec( ... ) and hexToDec( ... ) methods first. If needed you can do all sorts of fancy input later.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    addictz04 is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    Revised it to this but it still doesnt work. Ugh!

    while(s.hasNextLine()){
    String m = "---";
    String num = s.nextLine();
    if(num.equals(m))
    System.out.println("----------------------");
    else{
    String number = num.substring(0, num.length());
    char base = num.charAt(num.length()-1);

    switch(base){
    case 'b': System.out.println(c.binToDecimal(num));
    break;
    case 'h': System.out.println(c.hexToDecimal(num));
    break;
    default: System.out.println ("Inconvertible. Please try again. :s");
    }
    }
    }
    }

Similar Threads

  1. Converting whole number into decimal
    By jim01 in forum New To Java
    Replies: 2
    Last Post: 09-23-2010, 07:58 PM
  2. Replies: 4
    Last Post: 10-26-2009, 10:17 PM
  3. Convert binary into decimal
    By WarmRegards in forum New To Java
    Replies: 8
    Last Post: 10-18-2009, 02:32 PM
  4. converting a decimal to an int
    By shuks in forum New To Java
    Replies: 9
    Last Post: 10-12-2009, 09:41 AM
  5. Replies: 3
    Last Post: 07-25-2009, 01:44 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •