Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: JAVA problem

  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JAVA problem

    I am just doing some school work here and i am tasked with changing a binary into a decimal using the input dialog box in JOptionPane.
    What i basically need to accomplish is being able to type in a binary and get a message box to pop up with the decimal and if the input is invalid an error message comes up.
    I have found many ways to get an actual print out of the decimal but i can't figure out how to link the dialog box to the actual calculations.

    here is what i have so far

    Code:
    import java.lang.Math;
    
    public class BinaryToDecimal
    {
    	public static void main(String args[])
    	{
    		//declare the original String object
    		String binaryString = "10011110";
    		
    		//declare the char array
    		char[] binaryArray;
    		
    		
    		 int decimalNumber = 0;
    		
    		//convert string into array using toCharArray() method of string class
    		binaryArray = binaryString.toCharArray();
    		
    		// this for loop may be different from others you've seen. instead of starting at 0 and counting up, it's going to start at the max and count down to zero
    		for (int i = binaryArray.length - 1; i >= 0; i--)
    		{
    			// Now i is going to be the power, and binaryArray[i] will be the 1 or 0 value
    			// So if binaryArray[i] = 1, then add 2 to the power of i to the decimalNumber
    			if (binaryArray[i] == "1")
    			{
    				decimalNumber += Math.pow(2, i);
    			}
    		}
    		
    		//
    	}
    }
    Any help on this is appreciated!!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    In the future please select a better title that's more descriptive of the problem.

    This tutorial describes the usage of JOptionPane. You'll need to write a gui to run the program.
    http://download.oracle.com/javase/tu...ts/dialog.html
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    Mar 2011
    Location
    USA
    Posts
    23
    Thanks
    0
    Thanked 1 Time in 1 Post
    Is this what you are looking for. I took the code of yours for calculating decimal values but made soem cahnges. I would suggest to not to just copy the code, but use this an example and build more around this..

    import javax.swing.JOptionPane;

    public class JOptionPaneTest1 {

    public static void main(String[] args) {
    String ans;
    ans = JOptionPane.showInputDialog(null, "Enter Binary Value");
    int answer = new JOptionPaneTest1().convert(ans);
    JOptionPane.showMessageDialog(null, "Decimal Value = " + answer);

    System.exit(0);
    }

    public int convert(String text){

    //declare the original String object
    //String binaryString = "10011110";

    //declare the char array
    char[] binaryArray;


    int decimalNumber = 0;

    //convert string into array using toCharArray() method of string class
    binaryArray = text.toCharArray();
    int j=binaryArray.length-1;
    // this for loop may be different from others you've seen. instead of starting at 0 and counting up, it's going to start at the max and count down to zero
    for (int i = 0; i<binaryArray.length; i++)
    {
    // Now i is going to be the power, and binaryArray[i] will be the 1 or 0 value
    // So if binaryArray[i] = 1, then add 2 to the power of i to the decimalNumber
    if (binaryArray[i] == '1')
    {
    decimalNumber += Math.pow(2, j);
    }
    j--;
    }

    return decimalNumber;
    }


    }

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by spchinta View Post
    Is this what you are looking for. I took the code of yours for calculating decimal values but made soem cahnges. I would suggest to not to just copy the code, but use this an example and build more around this..

    import javax.swing.JOptionPane;

    public class JOptionPaneTest1 {

    public static void main(String[] args) {
    String ans;
    ans = JOptionPane.showInputDialog(null, "Enter Binary Value");
    int answer = new JOptionPaneTest1().convert(ans);
    JOptionPane.showMessageDialog(null, "Decimal Value = " + answer);

    System.exit(0);
    }

    public int convert(String text){

    //declare the original String object
    //String binaryString = "10011110";

    //declare the char array
    char[] binaryArray;


    int decimalNumber = 0;

    //convert string into array using toCharArray() method of string class
    binaryArray = text.toCharArray();
    int j=binaryArray.length-1;
    // this for loop may be different from others you've seen. instead of starting at 0 and counting up, it's going to start at the max and count down to zero
    for (int i = 0; i<binaryArray.length; i++)
    {
    // Now i is going to be the power, and binaryArray[i] will be the 1 or 0 value
    // So if binaryArray[i] = 1, then add 2 to the power of i to the decimalNumber
    if (binaryArray[i] == '1')
    {
    decimalNumber += Math.pow(2, j);
    }
    j--;
    }

    return decimalNumber;
    }


    }
    In the future, please ensure your code is wrapped in [code][/code] or [php][/php] tags to preserve formatting.

    As this is a homework assignment, you should not be providing directly usable code for your replies; instead limit it to minor corrections or little implementation (or other tutorials for examples). Providing full use code does not allow new programmers to teach themselves.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    New Coder
    Join Date
    Mar 2011
    Location
    USA
    Posts
    23
    Thanks
    0
    Thanked 1 Time in 1 Post
    I am new to this forum and using this to refersh my coding skills.
    Thanks for the suggestion.I was wondering how i can wrap code the way others do. I saw this earlier but missed it..

    Also, about the code, I made corrections to the convert method and gave an eaxmple of JOptionsPane. She/he still has to add logic for error messages.

    I agree, In some other posts too, I gave all code. That's my bad habbit to explain everything in clear. Giving examples rather than providing code is a good idea too. I will take care of that next time..


  •  

    Posting Permissions

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