Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Exclamation Error Message in JBuilder

    Alright, so I'm pretty new here and new to Java, and I'm sitting here working on my homework and I think that I've got the code correct, but I keep getting this error on my program:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:

    The Program itself looks like this:

    Java Code:
    [COLOR="Navy"]import[/COLOR] javax.swing.JOptionPane;
    [COLOR="Navy"]public class[/COLOR] study2 {
    
    	[COLOR="Navy"]public static void[/COLOR] main(String[] args) {
    		String temp1 , temp2;
    		[COLOR="Navy"]int[/COLOR] num1 , num2, max = 0;
    		String choice="";
    			
    		[COLOR="Navy"]do[/COLOR]{
    			JOptionPane.showInputDialog([COLOR="MediumTurquoise"]"The First Number is:"[/COLOR]);
    				num1 = Integer.parseInt(temp1);
    			JOptionPane.showInputDialog([COLOR="MediumTurquoise"]"The Second Number is:"[/COLOR]);
    				num2 = Integer.parseInt(temp2);
    			
    				[COLOR="Navy"]if [/COLOR](num1 > num2)
    					max = num1;
    				[COLOR="Navy"]else[/COLOR] max = num2;
    				
    			JOptionPane.showMessageDialog(null,[COLOR="MediumTurquoise"]"The Maximum is"[/COLOR] +max);
    			JOptionPane.showInputDialog(null, "[COLOR="MediumTurquoise"]Would you like to continue?: "[/COLOR]);
    					
    				} while (choice.equals([COLOR="MediumTurquoise"]"yes"[/COLOR])||choice.equals([COLOR="MediumTurquoise"]"Yes"[/COLOR]));
    			 
    			 
    		}
    		
    		
    
    	}


    I would immensely appreciate any help on this error, or how to fix whatever I did wrong.
    Last edited by RavenNevarmore; 10-08-2008 at 04:58 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Are you sure that your code is correct. I don't think so.

    You have declared two String variables temp1 and temp2. You use them inside the main method without initialization. That's why you get compilation error in your code.

  3. #3
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default

    Then how do I 'initilize' these variables? I thought that all I had to do to use a variable was to declare it.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    If you are using local variables, you must declare them before use. For instance/class variables you don't need to do it, default values are automatically declared.

    Java Code:
    			temp1 = JOptionPane.showInputDialog("The First Number is:");
    				num1 = Integer.parseInt(temp1);
    			temp2 = JOptionPane.showInputDialog("The Second Number is:");
    				num2 = Integer.parseInt(temp2);

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Here is much better code segment. Write it just for fun. Try to learn something on that pal, but I'm not superior on Java. ;)

    Java Code:
    import javax.swing.JOptionPane;
    
    public class study2 {
    
        public static void main(String[] args) {
            String temp1 = null , temp2 = null;
            int num1 = 0 , num2 = 0, max = 0;
            int ret = 0;
            
            inner: do {
                try {
                    temp1 = JOptionPane.showInputDialog("The First Number is:");
                    num1 = Integer.parseInt(temp1);
    
                    temp2 = JOptionPane.showInputDialog("The Second Number is:");
                    num2 = Integer.parseInt(temp2);
                }
                catch(NumberFormatException ex) {
                    JOptionPane.showMessageDialog(null, "Invalid input");
                    continue inner;
                }
                if (num1 > num2)
                    max = num1;
                else 
                    max = num2;
    
                JOptionPane.showMessageDialog(null,"The Maximum is " +max);
                ret = JOptionPane.showConfirmDialog(null, "Continue?", "More", 
                        JOptionPane.YES_NO_OPTION , JOptionPane.INFORMATION_MESSAGE);
    
            } while(ret == JOptionPane.YES_OPTION);
        }
    
    }

Similar Threads

  1. strange Error message
    By little_polarbear in forum New To Java
    Replies: 4
    Last Post: 08-25-2008, 11:45 PM
  2. Can't solve error message while looping
    By BHCluster in forum New To Java
    Replies: 15
    Last Post: 04-22-2008, 10:51 AM
  3. java error message
    By baileyr in forum New To Java
    Replies: 2
    Last Post: 01-23-2008, 03:47 AM
  4. Replies: 7
    Last Post: 08-12-2007, 03:47 PM
  5. error message on jsp
    By sandor in forum Web Frameworks
    Replies: 1
    Last Post: 04-11-2007, 02:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •