Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-03-2011, 09:39 PM   PM User | #1
Rusery
New to the CF scene

 
Join Date: Mar 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rusery is an unknown quantity at this point
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!!
Rusery is offline   Reply With Quote
Old 03-04-2011, 01:53 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 03-04-2011, 05:57 PM   PM User | #3
spchinta
New Coder

 
Join Date: Mar 2011
Location: USA
Posts: 23
Thanks: 0
Thanked 1 Time in 1 Post
spchinta is an unknown quantity at this point
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;
}


}
spchinta is offline   Reply With Quote
Old 03-04-2011, 09:02 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 03-04-2011, 10:12 PM   PM User | #5
spchinta
New Coder

 
Join Date: Mar 2011
Location: USA
Posts: 23
Thanks: 0
Thanked 1 Time in 1 Post
spchinta is an unknown quantity at this point
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..
spchinta is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:20 PM.


Advertisement
Log in to turn off these ads.