Results 1 to 5 of 5
  1. #1
    ricardo5222 is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default If Statement return

    Hello..I'm new to java and have been trying to figure this out for couple of hours now. I'm trying to use the If statement in one of my methods to make my JOptionpanes appear when my second JFrame is visible. I managed to get it to work by using the following code but the problem is I can't return the userName. I get the error "missing return statement" when compiling. If there is anybody who can give me a solution or a better way of doing this please help me out...I just want to be able to make my JOptionpanes appear when my secondJFrame is visible..I'm using action listener to run this method when a JButton is clicked. But I need the userName to be returned so I can use it on my other methods. This method is in my main and thanks to the If statement I put its showing the JOptionpanes when my second JFrame shows up just can't return my userName...

    Java Code:
     
    
    public String WelcomeDialog() //JOPtionpane dialog
    	{
    
    		if (secondJPanel.isVisible())  //IF starts Here
    		{
    //welcome message
    	String welcome = "<html><font size=\"5\" face" 
    												 + "=\"SimHei\">"
    												 + "Welcome to Final Escape VIII !"
    												 + " <br>Please enter your name below "
    												 + "to continue!" 
    												 + "</font></html>";
    
      	
    	//Get usersname
    	userName = (String)JOptionPane.showInputDialog (null,
    													  ""+welcome+"",
    													  "Final Escape VIII",
    													  JOptionPane.PLAIN_MESSAGE,
    													  auron,
    													  null,
    													  "Type your name here!");
    	
    
    	// Burpback											 
    	String welcome2 = "<html><font size=\"5\" face" 
    												 + "=\"SimHei\">"
    												 + "Thank you for entering your name "+userName+"!"
    												 + "</font></html>";
    	//Game Info									 
    	String gameinfo = "<html><font size=\"5\" face" 
    												 + "=\"Baskerville Old Faceei\">"
    												 + "Hello "+userName+" and welcome to my escape game! "
    												 + "<br>In my game you'll take control of the main character "
    												 + "<br>named Squall. He is a character from FinalFantasy VIII"
    												 + "<br>a playstation one game that was released in 1998."
    												 + "<br>The goal of my game will be to control Squall and guide him "
    												 + "<br>escape from a dungeon from his original game. In order to do so you will"
    												 + "<br>need the key item in this game which will be his main weapon from "
    												 + "<br>his original game. The reason you'll need this key item is to defeat "
    												 + "<br>the boss who is guarding the exit to the dungeon. Your goal is to find " 
    												 + "<br>the key item first and then navigate through the dungeon and defeat the "
    												 + "<br>boss to escape!"    
    												 + "</font></html>";
    	//Registration info											 
    	String registration = "<html><font size=\"6\" face" 
    												 + "=\"Baskerville Old Faceei\">"
    												 + "The regestration fees for Final Escape VIII are as follows"
    												 + "<br><li>$2.50 for a Beginner Player (age under 4)"
    												 + "<br><li>$5.00 for a Average Player (ages 4 - 12) "
    												 + "<br><li>$7.50 for a Advanced Player  (ages 13 - 17)"
    												 + "<br><li>$9.75 for a Veteran Player(ages 18 and above)"
    												 + "<br><li>$1.25 for each additional feature: a “teamate”, “boss”  “extra key weapon”"
    												 + "</font></html>";
       //Underconstruction message												 
    	String alert = "<html><font size=\"5\" face" 
    												 + "=\"SimHei\">"
    												 + "Now that you've seen the fees and read about the game proceed by clicking on"
    												 + "<br>on the continue button to proceed to the registration form"
    												 + "</font></html>";
    												 
    	/////////////////////////////////////////////////////////////////////////
    
    
    
    	
    
    	/////////////////////////////////////////////////////////////////////////
    
    	//welcome message	
    	JOptionPane.showMessageDialog( null,""+welcome2+"",
    												"Final Escape VIII",													
    												JOptionPane.WARNING_MESSAGE,
    												squall2 );
    	//Game information											
    	JOptionPane.showMessageDialog( null,""+gameinfo+"",
    												"Final Escape VIII",													
    												JOptionPane.WARNING_MESSAGE,
    												squall);
    	//Registration fees											
    	JOptionPane.showMessageDialog( null,""+registration+"",
    												"Final Escape VIII",													
    												JOptionPane.WARNING_MESSAGE,
    												cloud );
    	//Under Construction
    	JOptionPane.showMessageDialog( null,""+alert+"",
    												"Registration alert!",													
    												JOptionPane.WARNING_MESSAGE,
    												choco );
    												
    	
    	
    	
    	return userName;	  //need this returned! 					
    	}
     }      //need to return here as well?
    Sorry for the crappy code paste...I'm still new to this :(

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Basically this is what your code looks like:

    Java Code:
    if (secondJPanel.isVisible())  {
       // a whole lot of code here ...
       return userName;
    }
    } // end of method
    Think what would happen if secondJPanel is not visible; the entire if-statement is skipped and nothing is returned; the compiler doesn't like that because you state that your method returns a String; always.

    kind regards,

    Jos
    Last edited by JosAH; 10-31-2010 at 11:36 AM.

  3. #3
    ricardo5222 is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    Thank you for your reply so I can not use an if statement in this situation...Any suggestions on what do use though to control when my JOptionpane dialogs will show up? Can you put one in a JFrame so when the JFrame is visible so will the JOptionpane?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by ricardo5222 View Post
    Thank you for your reply so I can not use an if statement in this situation...Any suggestions on what do use though to control when my JOptionpane dialogs will show up? Can you put one in a JFrame so when the JFrame is visible so will the JOptionpane?
    You could stick with this code if there is a valid "else" to that if. That is, what happens when that panel isn't visible?

Similar Threads

  1. Return statement error
    By Exothesis in forum New To Java
    Replies: 2
    Last Post: 10-13-2010, 02:56 AM
  2. Return Statement and Boolean Help
    By GhostShaman in forum New To Java
    Replies: 8
    Last Post: 03-09-2010, 12:15 PM
  3. problem while using return statement
    By shaluchandran in forum New To Java
    Replies: 10
    Last Post: 12-12-2008, 07:29 PM
  4. there is no return statement
    By gabriel in forum New To Java
    Replies: 17
    Last Post: 12-03-2008, 05:55 PM
  5. [SOLVED] return statement
    By Nakira in forum New To Java
    Replies: 8
    Last Post: 11-13-2008, 12:00 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
  •