6 Replies - 1937 Views - Last Post: 18 July 2012 - 12:29 AM Rate Topic: -----

#1 brandon12688  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 17-July 12

18Jul2012- Decimal to binary Java

Posted 17 July 2012 - 04:20 PM

First good day to everyone. I really hate being the 1 post Dbag, but I stumbled accross this site while researching and was hoping you all would be able to point me in the right direction. Well its the basic decimal to binary converter, but it must be GUI. Im not expecting an answer, maybe some hints or a link that could help me out. The issue I'm having is after the user has selected thier number and got the conversion, how would I get it to loop back so they may enter a new number to convert or "0" to exit. Basically i want them to select numbers to convert as long as they want, and the "0" will exit the program. I just cant figure out how to reword my code to fit the approriate loop needed. Thanks everyone for your time.

import javax.swing.JOptionPane;
public class pa_2 {
      public static void main(String[] args) {
           
            int number;
            int StopValue = 0;
            String binary = JOptionPane.showInputDialog(null,"Please Enter Decimal to be Converted to Binary Number: ",
            "Binary Number", JOptionPane.QUESTION_MESSAGE);
            number = Integer.parseInt(binary);
              binary = " ";
                        while (number != 0) {
                              binary = (number % 2) + binary;
                              number = number / 2;}
                        JOptionPane.showMessageDialog(null, "Your binary representation is " + binary);
            
      String inputString = JOptionPane.showInputDialog(null,"Enter " + StopValue + 
      " to exit the program, or enter a new positive number to convert to binary: ");
            
            
            if (number == 0)
                  JOptionPane.showMessageDialog(null," Thanks now Exiting! ");
             
                        
                                                }}



Is This A Good Question/Topic? 0
  • +

Replies To: 18Jul2012- Decimal to binary Java

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: 18Jul2012- Decimal to binary Java

Posted 17 July 2012 - 06:17 PM

Moved to Java. Please note that Javascript != Java.
Was This Post Helpful? 0
  • +
  • -

#3 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: 18Jul2012- Decimal to binary Java

Posted 17 July 2012 - 06:42 PM

If you are interested in converting a decimal integer to binary, you can try shifting the bits and concatenating them to a string. There was a post a couple days ago that can give your more insight.

This post has been edited by x68zeppelin80x: 17 July 2012 - 06:42 PM

Was This Post Helpful? 0
  • +
  • -

#4 brandon12688  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 17-July 12

Re: 18Jul2012- Decimal to binary Java

Posted 17 July 2012 - 06:48 PM

View Postmacosxnerd101, on 17 July 2012 - 06:17 PM, said:

Moved to Java. Please note that Javascript != Java.

I apologize for that. Thank you for the move.
Was This Post Helpful? 0
  • +
  • -

#5 brandon12688  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 17-July 12

Re: 18Jul2012- Decimal to binary Java

Posted 17 July 2012 - 09:40 PM

View Postx68zeppelin80x, on 17 July 2012 - 06:42 PM, said:

If you are interested in converting a decimal integer to binary, you can try shifting the bits and concatenating them to a string. There was a post a couple days ago that can give your more insight.

thank you! im looking into it now. hopefully it can help me along.
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: 18Jul2012- Decimal to binary Java

Posted 17 July 2012 - 09:58 PM

Simply indenting your code correctly will show you a lot about how your code will operate. I've indented your code below. I prefer my braces to line up in the left column, but some prefer the other style that leaves the open brace at the end of the line with the close brace by itself on the left column.
import javax.swing.JOptionPane;
public class pa_2
{
	public static void main(String[] args)
	{
		int number;
		int StopValue = 0;
		String binary = JOptionPane.showInputDialog(null,
				"Please Enter Decimal to be Converted to Binary Number: ",
				"Binary Number", JOptionPane.QUESTION_MESSAGE);
		number = Integer.parseInt(binary);
		binary = " ";
		while (number != 0)
		{
			binary = (number % 2) + binary;
			number = number / 2;
		}
		JOptionPane.showMessageDialog(null,
				"Your binary representation is " + binary);

		String inputString = JOptionPane.showInputDialog(null,"Enter " + 
				StopValue + " to exit the program, or enter a new positive" +
		" number to convert to binary: ");

		if (number == 0)
			JOptionPane.showMessageDialog(null," Thanks now Exiting! ");
	}
}

With the code indented properly, you can see that your while clause merely encloses lines 13 - 17. In order for your program to work properly, you need the while statement to include through line 24/25, a new line added to the end of the while clause:

number = Integer.parseInt(inputString);

and then you can remove the 'if' statement that follows (keep the JOptionPane output), because the 'if' is redundant.

Now you'll just have to fix the numerical conversion.
Was This Post Helpful? 1
  • +
  • -

#7 brandon12688  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 17-July 12

Re: 18Jul2012- Decimal to binary Java

Posted 18 July 2012 - 12:29 AM

View PostGregBrannon, on 17 July 2012 - 09:58 PM, said:

Simply indenting your code correctly will show you a lot about how your code will operate. I've indented your code below. I prefer my braces to line up in the left column, but some prefer the other style that leaves the open brace at the end of the line with the close brace by itself on the left column.

With the code indented properly, you can see that your while clause merely encloses lines 13 - 17. In order for your program to work properly, you need the while statement to include through line 24/25, a new line added to the end of the while clause:

number = Integer.parseInt(inputString);

and then you can remove the 'if' statement that follows (keep the JOptionPane output), because the 'if' is redundant.

Now you'll just have to fix the numerical conversion.

Thanks a lot for your time. I cant wait to get home and hopefully get this to work. I really do appreciate the help even though I just signed up.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1