Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com JAVA problem

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 03-03-2011, 10: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 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!!
 03-04-2011, 02:53 AM PM User | #2 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,602 Thanks: 4 Thanked 2,598 Times in 2,567 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');  ```
 03-04-2011, 06:57 PM PM User | #3 spchinta 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
03-04-2011, 10:02 PM   PM User | #4
Fou-Lu
God Emperor

Join Date: Sep 2002
Posts: 16,602
Thanks: 4
Thanked 2,598 Times in 2,567 Posts
Quote:
 Originally Posted by spchinta 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
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');  ```

 03-04-2011, 11:12 PM PM User | #5 spchinta 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..

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 02:55 PM.