Stumble on a simple program help!!! Im new at Java so any help would be appreciated thnx... The code
import javax.swing.JOptionPane;
public class Identifier { public static void main(String[] args) { double firstNoIn, secondNoIn;//to store the integers
firstNoIn = JOptionPane.showInputDialog ("Please type in a number...");//displays first request secondNoIn = JOptionPane.showInputDialog ("Please type in another number...");//displays second request
if (firstNoIn > secondNoIn) { JOptionPane.showMessageDialog (null, "The highest number is" + firstNoIn, JOption.PLAIN_MESSAGE); } else if (firstNoIn < secondNoIn) { JOptionPane.showMessageDialog (null, "The highest number is" + secondNoIn, JOption.PLAIN_MESSAGE); else if (firstNoIn == secondNoIn) { JOptionPane.showMessageDialog (null, "The two numbers have the same value" , JOption.PLAIN_MESSAGE); } else { OptionPane.showMessageDialog (null, "Invalid format!" , JOption.PLAIN_MESSAGE); } System.exit(0); } }
-------------------------------- ------------------------------------ The error messages:
Identifier.java:23: 'else' without 'if' else if (firstNoIn == secondNoIn) ^ Identifier.java:35: '}' expected } ^ Identifier.java:10: incompatible types found : java.lang.String required: double ("Please type in a number...");//displays first request ^ Identifier.java:12: incompatible types found : java.lang.String required: double ("Please type in another number...");//displays second request ^ Identifier.java:17: cannot resolve symbol symbol : variable JOption location: class Identifier (null, "The highest number is" + firstNoIn, JOption.PLAIN_MESSAGE); ^ Identifier.java:22: cannot resolve symbol symbol : variable JOption location: class Identifier (null, "The highest number is" + secondNoIn, JOption.PLAIN_MESSAGE); ^ 6 errors
import javax.swing.JOptionPane;
publicclass Identifier
{
publicstaticvoid main(String[] args)
{
double firstNoIn, secondNoIn;//to store the integers
firstNoIn = JOptionPane.showInputDialog("Please type in a number...");//displays first request
secondNoIn = JOptionPane.showInputDialog
("Please type in another number...");//displays second request
if (firstNoIn > secondNoIn)
{
JOptionPane.showMessageDialog
(null, "The highest number is" + firstNoIn, JOption.PLAIN_MESSAGE);
}
elseif (firstNoIn < secondNoIn)
{
JOptionPane.showMessageDialog
(null, "The highest number is" + secondNoIn, JOption.PLAIN_MESSAGE);
}
elseif (firstNoIn == secondNoIn)
{
JOptionPane.showMessageDialog
(null, "The two numbers have the same value" , JOption.PLAIN_MESSAGE);
}
else
{
OptionPane.showMessageDialog
(null, "Invalid format!" , JOption.PLAIN_MESSAGE);
}
System.exit(0);
}
}
I have corrected the issue of the else. However you seems to be passing wrong values to the showMessageDialog method. Please consult the API before passing the values. Also showInputDialog seems to be returning a String according to the API but you are assigning into a double variable?
refer to this url for info on API.An API provides all the in-built class,methods and variables provided for us by java.For eg. JOptionPane is an in-built class.
Also go to JOptionPane class (on the left side)in this url and see whats the return type for the method showInputDialog.As the return type for this is string u cant use double.
public Identifier1b() { int firstNumber, secondNumber; //To store the integers String number;
number = JOptionPane.showInputDialog ("Please enter 1st number");//Displays First Request firstNumber = Integer.parseInt (number);//firstNumber is conerted to int type
number = JOptionPane.showInputDialog ("Please enter 2nd number");//Displays Second Request secondNumber = Integer.parseInt (number);//SecondNumber is conerted to int type
if (firstNumber > secondNumber) {
JOptionPane.showMessageDialog (null, "The highest number is " + firstNumber);
}//If firstNumber is higher in vlaue than secondNumber then execute the command above
if (firstNumber == secondNumber) {
JOptionPane.showMessageDialog (null, "The two numbers have the same value");
}//If the values of firstNumber and secondNumber are the same then execute the command above
else {
JOptionPane.showMessageDialog (null, "The highest number is " + secondNumber);
}//Finally if none of the above stand then execute the command above
}
public static void main (String args[]) {
Identifier1b app = new Identifier1b(); System.exit(0);//end main
Actually, I just got a new machine and I hadn't even installed the JDK; however, in the meantime, I've now downloaded the JDK+NetBeans and now I'm installing it... I'm used to IDEs like Borland's Delphi and C++ Builder and more recently Microsoft Visual Studio .Net, so I hope NetBeans isn't a letdown (I've heard good things about it, so I'm optimistic about all the refactoring and code smarts stuff, but my past experience with Java form designers makes the optimism a little guarded in that arena).
In this case the fact that the code is unformatted is mittigated by the fact that it is nice and short and fits on one page. So the missing else stuck out like a sore thumb even without formatting... It might even be one of those things that ends up being better disguised by formatting, depending on how it is indented.
Anyway, the more important thing I should have mentioned is that it is pretty poor form to have a class that does all its work in the constructor. It is much better to do the work in an appropriately named method. In this case, it could just be a single static method, instead of creating an object with no state for the sole purpose of executing a method.
By the way, the other thing that might be good to have is some error handling and maybe a chance for the user to retry in the case where they didn't enter something that could be parsed as an integer.