The Artima Developer Community
Sponsored Link

Java Answers Forum
Help with 6 errors basic stuff :p

9 replies on 1 page. Most recent reply: Oct 7, 2003 9:11 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 9 replies on 1 page
MS

Posts: 3
Nickname: leath
Registered: Oct, 2003

Help with 6 errors basic stuff :p Posted: Oct 6, 2003 5:41 PM
Reply to this message Reply
Advertisement
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


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Help with 6 errors basic stuff :p Posted: Oct 6, 2003 6:41 PM
Reply to this message Reply
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);
  }
}


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?

MS

Posts: 3
Nickname: leath
Registered: Oct, 2003

Re: Help with 6 errors basic stuff :p Posted: Oct 6, 2003 7:01 PM
Reply to this message Reply
im a bit confused and a bit new to this... What does API stand for and also what is wrong with using double?

sindu

Posts: 15
Nickname: sindu
Registered: Sep, 2003

Re: Help with 6 errors basic stuff :p Posted: Oct 6, 2003 8:43 PM
Reply to this message Reply
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.

http://java.sun.com/j2se/1.3/docs/api/

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.

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Help with 6 errors basic stuff :p Posted: Oct 6, 2003 9:51 PM
Reply to this message Reply
Here is a more specific link to JOptionPane

http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html

MS

Posts: 3
Nickname: leath
Registered: Oct, 2003

Re: Help with 6 errors basic stuff :p Posted: Oct 7, 2003 12:49 AM
Reply to this message Reply
Thnx for pointing me in the right direction guys :) have managed to sort out all my queries appreciations. Heres the code now that i fixed it.....

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class Identifier1b
{

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

}

}

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Help with 6 errors basic stuff :p Posted: Oct 7, 2003 1:06 AM
Reply to this message Reply
Quite a big leap from where you started and where you are now. Good Job. Keep it up :-)

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Help with 6 errors basic stuff :p Posted: Oct 7, 2003 1:27 PM
Reply to this message Reply
> Quite a big leap from where you started and where you
> are now. Good Job. Keep it up :-)

Except for two problems:

1) Still hasn't started using the java tags for posting code.

2) Try entering the larger number first (eg. first 444 then 4) and I think you'll find a small bug in the program...

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: Help with 6 errors basic stuff :p Posted: Oct 7, 2003 7:06 PM
Reply to this message Reply
As a side note, did you notice that by reading the (unformatted) code? Or did you actually compile and execute it?

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Help with 6 errors basic stuff :p Posted: Oct 7, 2003 9:11 PM
Reply to this message Reply
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.

Flat View: This topic has 9 replies on 1 page
Topic: overriding methods in java.lang.object Previous Topic   Next Topic Topic: package import.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use