Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java: I don't know how to reject binary values longer than 32 bits [USER INPUT]

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Java: I don't know how to reject binary values longer than 32 bits [USER INPUT]

    As the title says. I am designing a program in-order convert Binary to Decimal values with added features:

    Rejecting binary values longer than 32 bits
    Prompting the user to make multiple entries after completing the binary to decimal conversion of their first entry.
    I was trying to code this in Nested For Loops, but I don't know if I've really done that.

    I own the latest Java Programming 9th edition by Y. Daniel Liang. I don't find this book very helpful.

    Here is what i have so far. Help is appreciated!(:

    public class BinaryToDecimal {
    public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
     
      String binary;
      int decimal=0b10, i, rem;
      boolean isBinary = true;
     
     
      JOptionPane.showInputDialog(null, "How many bit sequences do you want to convert to decimal?");
      binary = JOptionPane.showInputDialog(null, "Enter a string of bits(no spaces)");
      decimal = Integer.parseInt(binary);
     
      char[] bits = binary.toCharArray();
     
      for(i = 0; i < bits.length; i++) {
        if( (bits[i] != '0') && (bits[i] != '1') ){
          isBinary = false;
        }
      }
      if(!isBinary){
      JOptionPane.showMessageDialog(null, "This is not a binary number.");
      JOptionPane.showMessageDialog(null, "Please enter a string of only 1's and 0's.");
      }
      else{
         i = decimal / 2;
        rem = decimal % 2;
        i = Integer.parseInt(binary,2);
        JOptionPane.showMessageDialog(null, " The decimal of " + binary + " is " + i);
        System.exit (0);
      }
    }
    }


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Java: I don't know how to reject binary values longer than 32 bits [USER INPUT]

    Rejecting binary values longer than 32 bits
    -actually you did not meet that condition in your program.
    since you parse your input to integer, therefore the program treated the input as integer of base 10 (decimal) not base 2(binary)
    I think it will be better if you just store it in string, then check if the input's length is less than or equal
    to 32 (cause you want 32 bits) and then convert it to decimal.
    (you just have to analyze the pattern of converting binary to decimal)

    Prompting the user to make multiple entries after completing the binary to decimal conversion of their first entry.
    you must put the statements that's getting the user's input in a loop.
    with a condition that will terminate it if the user wants.

  3. The Following User Says Thank You to dicdic For This Useful Post:

    urbanlime (February 25th, 2014)

Similar Threads

  1. Java user input
    By Camwarp in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 4th, 2014, 02:58 AM
  2. Permanently change variable values based on user input.
    By joseph.clevenger7 in forum Java Theory & Questions
    Replies: 3
    Last Post: April 26th, 2013, 07:32 PM
  3. Replies: 1
    Last Post: May 16th, 2012, 05:15 PM
  4. Replies: 13
    Last Post: December 6th, 2011, 02:17 PM
  5. JTable Updating String Values from User Input
    By aussiemcgr in forum AWT / Java Swing
    Replies: 5
    Last Post: August 3rd, 2010, 01:48 PM

Tags for this Thread