Hi everyone! Question??? I have created a source code for converting Decimal numbers to Binary. I haven't done this in a while so I need a litte help on figuring out what is wrong with my code. The problem is I enter the JOptionPane class in order to prompt the user for input and display message but when I do, my source code no longer prints out correctly or excepts the source code. Any Help on this?

Thanks in Advance IT's :)

Below is the code I have created:

import java.io.*;
import java.util.*;
import java.lang.*;
import javax.swing.JOptionPane;
public class Mirage'
{

public static void main (String args[])throws IOException
{
int a=1;
while(a==1)
{
String str,str1;

JOptionPane.showInputDialog("Please Enter Decimal to be converted to Binary:");
InputStreamReader sr=new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(sr);

str=br.readLine();
int x=Integer.parseInt(str);
int y[] = new int[50];
int i=0;
if (x/2!=0 || x==1)
{
while(x!=1)
{
y[i]=x%2;
x=x/2;
i++;
}
y[i]=1;
}
JOptionPane.showMessageDialog(" + 's binary representation is:");
for(int z=i;z>=0;z--)
{
System.out.println(y[z]);
}
JOptionPane.showInputDialog("Enter 1 for enter another value 0 or other value for exit:");
str1=br.readLine();
int b=Integer.parseInt(str1);
if(b==1)
a=1;
else
a=0;}


}
}

Why are you getting the values from System.in when you have an InputDialog?

JOptionPane.showInputDialog returns a String (the user's input), but you are ignoring it.

Yes, I am aware that JOtptionPane returns a String but when I change the System.out.println (line 37 )to JOtionPane.showMessageDialog I get an error. I am trying to invoke the JOtptionPane only but it won't allow me to. So I figured I was missing another important factor here. Am I missing another line error?

it won't allow me to

DO you get an error message? Please post or explain what the problem is.

Am I missing another line error?

What does that mean?

The following lines which are lines 34 & 37 I have changed to:

JOptionPane.showMessageDialog("'s binary representation is:");
for(int z=i;z>=0;z--)
{
JOptionPane.showMessageDialog(y[z]);
}

but I am recieving an error coding stating " cannot find variable JOptionPane"

Do you know what package JOptionPane is in?
Do you have an import statement for that package?

I am only guessing. Unless you post the FULL text of the error message, that's all I can do.
Please copy full text of error message and paste it here. Here is a sample:

TestSorts.java:138: cannot find symbol
symbol  : variable var
location: class TestSorts
         var = 2;
         ^

Thank You very much for the help as well! The Error code I amrecieving is

java:48: cannot find symbol
symbol  : variable JOptionPane
location: class Mirage
JOptionPane.showMessageDialog("'s binary representation is:");
^
Mirage.java:51: cannot find symbol
symbol  : variable JOptionPane
location: class Mirage
JOptionPane.showMessageDialog(y[z]);

Do you know what package JOptionPane is in?
Do you have an import statement for that package?

Is the code inside of a method? Without proper formatting it is very hard to scan your code to see. Please use code tags around your code. Use icon at right above input area.

Do you know what package JOptionPane is in?
Do you have an import statement for that package?

Is the code inside of a method? Without proper formatting it is very hard to scan your code to see. Please use code tags around your code. Use icon at right above input area.

Yes! Here is the complete code. My apologies.

import java.io.*;
import java.util.*;
import java.lang.*;
import javax.swing.JOptionPane.*;


public class Mirage
{

public static void main (String args[])throws IOException
{
int a=1;
while(a==1)
{
String str,str1;
JOptionPane.showInputDialog("Please Enter Decimal to be converted to Binary:");

InputStreamReader sr=new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(sr);

str=br.readLine();

int x=Integer.parseInt(str);
int y[] = new int[50];
int i=0;
if (x/2!=0 || x==1)
{
while(x!=1)
{
y[i]=x%2;
x=x/2;
i++;
}
y[i]=1;
}

JOptionPane.showMessageDialog("'s binary representation is:");
for(int z=i;z>=0;z--)
{
JOptionPane.showMessageDialog(y[z]);
}
JOptionPane.showInputDialog("Enter 1 for enter another value 0 or other value for exit:");

str1=br.readLine();

int b=Integer.parseInt(str1);
if(b==1)
a=1;
else
a=0;}


}
}

Ok, lets try again. You seem to be missing this part of my post:

Do you know what package JOptionPane is in?
Do you have an import statement for that package?

The package is the everything before the class name.

You have an import for a non existent package:
import javax.swing.JOptionPane.*;

Mirage,
(1) NormR1 wants you to know that JOptionPane is a class rather than a package. In your program you use JOptionPane which is in the package : javax.swing. Therefore your import statement should be either:
import javax.swing.*:
or
import javax.swing.JOptionPane;
(2) You have no need to use an input via DOS since you use the JOptionPane
(3) You should learn how to use JOptionPane to create an input. Please read API for the class JOptionPane
I have written a simple code showing how it works.

import java.io.*;
import javax.swing.JOptionPane;
 public class Mirage0{
  public static void main (String args[])throws IOException {
  String str =JOptionPane.showInputDialog("Please Enter an integer a:");
  int a=Integer.parseInt(str);
  str =JOptionPane.showInputDialog("Please Enter another integer b:");
  int b=Integer.parseInt(str);
  JOptionPane.showMessageDialog(null, "The sum of a and b :" + (a+b));
     }
}

Ok, lets try again. You seem to be missing this part of my post:

Do you know what package JOptionPane is in?
Do you have an import statement for that package?

The package is the everything before the class name.

You have an import for a non existent package:
import javax.swing.JOptionPane.*;

My apologies when I made my post, I assumed that you saw that this was in the swing package located in the javax package. The import statement was located at the head of my source code if I am not mistaken.

import javax.swing.*; ".
import java.util.*;
import java.io.*;


Is that what you are looking for you?

No. I meant that the header you used in line 4 on the second last poster on page 1:
import javax.swing.JOptionPane.*;
is wrong. No such a package. JOptionPane is a class.

Ok, lets try again. You seem to be missing this part of my post:

Do you know what package JOptionPane is in?
Do you have an import statement for that package?

The package is the everything before the class name.

You have an import for a non existent package:
import javax.swing.JOptionPane.*;

NormR1,

The JOptionPane is in the javax.swing package
The import statement is:

import javax.swing.*;

~Mirage'

Did adding that statement correct your compiler error?


we have simple programming lesson
about converting decimal to binary to hexadecimal and to octal
mirage urs are so complicated
and all i did is an object oriented program..
plz reply enx :)
more power

Did adding that statement correct your compiler error?

I apologize for the delay in response. Do you have any other suggestions or guidence for me. I am still working on it.


we have simple programming lesson
about converting decimal to binary to hexadecimal and to octal
mirage urs are so complicated
and all i did is an object oriented program..
plz reply enx :)
more power

Okay, What do you suggest I do? Thanks a lot though :)!!!

Can you explain what your current problem is?
Your last error was a compile error. Is that fixed now?

Hey Everyone, Thanks for All the Help! I figured it all out and used a more simple code YES :)while, using only three imports this time;

java.lang.String;
javax.swing.*;
java.util.Scanner;

It helped a great deal. I assume that I was invoking the wrong package :) and I didn't express "null" in my JOptionPane.showMessageDialog...Talk about Working It Out Wheee.

But again thanks for all the help...talk to you all soon!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.