Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Subroutine Help

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    28
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Subroutine Help

    Hello! I'm making a madlibs-like program, and I made a subroutine so I don't have to keep assigning the same variables to the same values. Here is my code:

    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.*;
    class Project
    {
    	public static void main (String[]args)
    	{
    	 try
    	 {	
    		int choice2, answer2, answer4;
    			choice2 = 1;
    		String noun, adjective, verb, noun2, adjective2, verb2, choice, answer, answer3;
     
    	  while (choice2 == 1)	
    	  {
    		answer = JOptionPane.showInputDialog(null, "Hello! Which story would you like to see? Type either '1', '2', or '3' to choose your story.", "Choose your story!", JOptionPane.INFORMATION_MESSAGE);
    		answer2 = Integer.parseInt(answer);	
     
    		 if (answer2 == 1)	
    		 {
    		 	typein();	
    			JOptionPane.showMessageDialog(null, "The " + noun + " went on a stroll in the " + adjective + " park and " + verb + " the tree. Then, a " + noun2 + " came out of nowhere and " + verb2 + " on the " + adjective2 + " mole.", "Story", JOptionPane.INFORMATION_MESSAGE);	
    			choice = JOptionPane.showInputDialog(null, "Do you want to redo the story? Press '1' for yes and '2' for no.", "Restart?", JOptionPane.INFORMATION_MESSAGE);
    			choice2 = Integer.parseInt(choice);
    		 }
     
    		 else if (answer2 == 2)
    		 {
    		 	typein();	
    			JOptionPane.showMessageDialog(null, "The " + noun + " went on a stroll in the " + adjective + " park and " + verb + " the tree. Then, a " + noun2 + " came out of nowhere and " + verb2 + " on the " + adjective2 + " mole.", "Story", JOptionPane.INFORMATION_MESSAGE);	
    			choice = JOptionPane.showInputDialog(null, "Do you want to redo the story? Press '1' for yes and '2' for no.", "Restart?", JOptionPane.INFORMATION_MESSAGE);
    			choice2 = Integer.parseInt(choice);
    		 }
     
    		 else if (answer2 == 3)
    		 {
    		 	typein();	
    			JOptionPane.showMessageDialog(null, "The " + noun + " went on a stroll in the " + adjective + " park and " + verb + " the tree. Then, a " + noun2 + " came out of nowhere and " + verb2 + " on the " + adjective2 + " mole.", "Story", JOptionPane.INFORMATION_MESSAGE);	
    			choice = JOptionPane.showInputDialog(null, "Do you want to redo the story? Press '1' for yes and '2' for no.", "Restart?", JOptionPane.INFORMATION_MESSAGE);
    			choice2 = Integer.parseInt(choice);
    		 }
    		 else
    		 {
    		 	JOptionPane.showMessageDialog(null, "Please type in a number from 1 to 3!", "Wrong Number!", JOptionPane.INFORMATION_MESSAGE);
    		 }			
    	  }
    	  if (choice2 == 2)
    	  {
    	  	answer3 = JOptionPane.showInputDialog(null, "Do you want to see another story? Please type '1' for yes and '2' for no.", "Other Story", JOptionPane.INFORMATION_MESSAGE);
    	  	answer4 = Integer.parseInt(answer3);
    	   if (answer4 == 1)
    	   {
    	   	choice2 = 1;
    	   }
    	   else if (answer4 == 2)
    	   {
    	   	JOptionPane.showMessageDialog(null, "Goodbye!", "End of Stories", JOptionPane.INFORMATION_MESSAGE);
    	   	System.exit(0);
    	   }
    	   else
    	   {
    	   	JOptionPane.showMessageDialog(null, "Please choose either 1 or 2!", "Wrong Number!", JOptionPane.INFORMATION_MESSAGE);
    	   }
    	  }
    	 }
    	 catch (java.lang.Throwable n)
    	 {
    	 	JOptionPane.showMessageDialog(null, "Goodbye!", "Clicked Cancel", JOptionPane.INFORMATION_MESSAGE);
    	 }			
    	}
     
    	static void String typein()
    	{		
    			noun = JOptionPane.showInputDialog(null, "Type in a noun.", "Noun");
    			adjective = JOptionPane.showInputDialog(null, "Type in an adjective.", "Adjective");
    			verb = JOptionPane.showInputDialog(null, "Type in the past form of a verb.", "Verb");
    			noun2 = JOptionPane.showInputDialog(null, "Type in another noun.", "Noun");
    			verb2 = JOptionPane.showInputDialog(null, "Type in another past form of a verb.", "Verb");
    			adjective2 = JOptionPane.showInputDialog(null, "Type in another adjective.", "Adjective");
    	}	
    }

    I'm not entirely sure how to do subroutines in this language, but it claims that I need to put "(" right before the "typein" in the subroutine. Can anyone help me with this? Thanks in advance!
    Last edited by Kimimaru; October 6th, 2010 at 04:48 PM.


  2. #2
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Subroutine Help

    Instead of the code tags, surround your code in [highlight=java][/highlight]. What part are you talking about exactly? I'm not sure what you really need/want help with.

  3. The Following User Says Thank You to Brt93yoda For This Useful Post:

    javapenguin (October 6th, 2010)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Subroutine Help

    You do realize that by defining those variables inside the try, that, I think, they will be lost when the try block ends?

    Also, how will it know what they are in your never called other method?

    Also, when it asks the JOptionPane to print out noun, noun at that point is null.

    Also, I'm not sure what you mean by "subroutine" , but I do know that your error message would be resolved if you only had one return type:

    static void  typein()
    	{		
    			noun = JOptionPane.showInputDialog(null, "Type in a noun.", "Noun");
    			adjective = JOptionPane.showInputDialog(null, "Type in an adjective.", "Adjective");
    			verb = JOptionPane.showInputDialog(null, "Type in the past form of a verb.", "Verb");
    			noun2 = JOptionPane.showInputDialog(null, "Type in another noun.", "Noun");
    			verb2 = JOptionPane.showInputDialog(null, "Type in another past form of a verb.", "Verb");
    			adjective2 = JOptionPane.showInputDialog(null, "Type in another adjective.", "Adjective");
    	}

    or
    static String  typein()
    	{		
    			noun = JOptionPane.showInputDialog(null, "Type in a noun.", "Noun");
    			adjective = JOptionPane.showInputDialog(null, "Type in an adjective.", "Adjective");
    			verb = JOptionPane.showInputDialog(null, "Type in the past form of a verb.", "Verb");
    			noun2 = JOptionPane.showInputDialog(null, "Type in another noun.", "Noun");
    			verb2 = JOptionPane.showInputDialog(null, "Type in another past form of a verb.", "Verb");
    			adjective2 = JOptionPane.showInputDialog(null, "Type in another adjective.", "Adjective");
     
    return typeString;
    	}	
    // also, this method is never called so it's pretty much doing nothing more than taking up space at the moment.
    .








    in fact, all your verbs, adjectives, nouns, etc will all be null as you've never initialized them in your main method and you've never called the method typein. Also, unless perhaps you stored them in an array, I don't see how you could return more than one String with a method, and even with a String array, I think it just gets the reference to the array. An ArrayList of type String may be able to return them all, or at least get their names from each index, but it think it can only return them like this:

    noun, adjective, verb, etc. Not as single entities.
    Last edited by javapenguin; October 6th, 2010 at 02:01 PM.

  5. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile What about this?

    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.*;
    class Project
    { // beginning of class
    	public static void main (String[]args)
    	{ // beginning of main
    int choice2, answer2, answer4;
    			choice2 = 1;
    answer2 = 0; // initializes it so it won't give error
    answer4 = 0;
    		String noun, adjective, verb, noun2, adjective2, verb2, choice, answer, answer3;
    noun = "";
    adjective = ""; 
    verb = "";
    noun2 = "";
    adjective2 = "";
    verb2 = "";
    choice = "";
    answer = "";
    answer3 = "";
     
    	 try
    	 {	// beginning of try
     
     
    	  while (choice2 == 1)	
    	  { // beginning of while
    		answer = JOptionPane.showInputDialog(null, "Hello! Which story would you like to see? Type either '1', '2', or '3' to choose your story.", "Choose your story!", JOptionPane.INFORMATION_MESSAGE);
    		answer2 = Integer.parseInt(answer);	
    			noun = typeinNoun();
                           verb = typeinVerb();
                       adjective = typeinAdjective();
                   noun2 = typeinNoun2();
               verb2 = typeinVerb2();
             adjective2 = typeinAdjective2();
     
    		 if (answer2 == 1)	
    		 { // beginning of if
    		 	typein();	
    			JOptionPane.showMessageDialog(null, "The " + noun + " went on a stroll in the " + adjective + " park and " + verb + " the tree. Then, a " + noun2 + " came out of nowhere and " + verb2 + " on the " + adjective2 + " mole.", "Story", JOptionPane.INFORMATION_MESSAGE);	
    			choice = JOptionPane.showInputDialog(null, "Do you want to redo the story? Press '1' for yes and '2' for no.", "Restart?", JOptionPane.INFORMATION_MESSAGE);
    			choice2 = Integer.parseInt(choice);
    		 } // end of if
     
    		 else if (answer2 == 2)
    		 { // beginning of else if
    		 	typein();	
    			JOptionPane.showMessageDialog(null, "The " + noun + " went on a stroll in the " + adjective + " park and " + verb + " the tree. Then, a " + noun2 + " came out of nowhere and " + verb2 + " on the " + adjective2 + " mole.", "Story", JOptionPane.INFORMATION_MESSAGE);	
    			choice = JOptionPane.showInputDialog(null, "Do you want to redo the story? Press '1' for yes and '2' for no.", "Restart?", JOptionPane.INFORMATION_MESSAGE);
    			choice2 = Integer.parseInt(choice);
    		 } // end of else if
     
    		 else if (answer2 == 3)
    		 { // beginning of else if
    		 	typein();	
    			JOptionPane.showMessageDialog(null, "The " + noun + " went on a stroll in the " + adjective + " park and " + verb + " the tree. Then, a " + noun2 + " came out of nowhere and " + verb2 + " on the " + adjective2 + " mole.", "Story", JOptionPane.INFORMATION_MESSAGE);	
    			choice = JOptionPane.showInputDialog(null, "Do you want to redo the story? Press '1' for yes and '2' for no.", "Restart?", JOptionPane.INFORMATION_MESSAGE);
    			choice2 = Integer.parseInt(choice);
    		 } // end of else if
    		 else
    		 { // beginning of else
    		 	JOptionPane.showMessageDialog(null, "Please type in a number from 1 to 3!", "Wrong Number!", JOptionPane.INFORMATION_MESSAGE);
    		 }			// end of else
    	  } // end of while
    	  if (choice2 == 2)
    	  { // beginning of if
    	  	answer3 = JOptionPane.showInputDialog(null, "Do you want to see another story? Please type '1' for yes and '2' for no.", "Other Story", JOptionPane.INFORMATION_MESSAGE);
    	  	answer4 = Integer.parseInt(answer3);
    	   if (answer4 == 1)
    	   { // beginning of if
    	   	choice2 = 1;
    	   } // end of if
    	   else if (answer4 == 2)
    	   { // beginning of else if
    	   	JOptionPane.showMessageDialog(null, "Goodbye!", "End of Stories", JOptionPane.INFORMATION_MESSAGE);
    	   	System.exit(0);
    	   } // end of else if
     
    	   else
    	   { // beginning of else
    	   	JOptionPane.showMessageDialog(null, "Please choose either 1 or 2!", "Wrong Number!", JOptionPane.INFORMATION_MESSAGE);
    	   } // end of else
    	  } // end of if
    	 } // end of try
    	 catch (java.lang.Throwable n)
    	 { // beginning of catch
    	 	JOptionPane.showMessageDialog(null, "Goodbye!", "Clicked Cancel", JOptionPane.INFORMATION_MESSAGE);
    	 }			// end of catch
    	} // end of main
     
    // if it still can't find them as is, have the respective Strings as parameters.
    // like this
     
    /*
    static String typeinNoun(String noun)
    { // beginning of method
    noun = JOptionPane.showInputDialog(null, "Type in a noun.", "Noun");
    return(noun);  
    } // end of method
    I'm not sure if it needs you to pass the parameters, so I'm leaving this in case you do
     
    if you do need them, call it like this:
     
    noun = typeInNoun(noun);
    */
    	static String typeinNoun()
    	{		// beginning of method
    			noun = JOptionPane.showInputDialog(null, "Type in a noun.", "Noun");
    return(noun);  
     
    	}	// end of method
    static String typeinAdjective()
    { // beginning of method
    adjective = JOptionPane.showInputDialog(null, "Type in an adjective.", "Adjective");
    return(adjective);
    } // end of method
     
    static String typeinVerb()
    { // beginning of method
    			verb = JOptionPane.showInputDialog(null, "Type in the past form of a verb.", "Verb");
    return(verb);
    } // end of method
     
    static String typeinNoun2()
    { // beginning of method
    			noun2 = JOptionPane.showInputDialog(null, "Type in another noun.", "Noun");
    return(noun2);
    } // end of method
     
    static String typeinVerb2()
    { // beginning of method
    			verb2 = JOptionPane.showInputDialog(null, "Type in another past form of a verb.", "Verb");
    return(verb2);
    } // end of method
     
    static String typeinAdjective2()
    { // beginning of method
    			adjective2 = JOptionPane.showInputDialog(null, "Type in another adjective.", "Adjective");
    return(adjective2);
    } // end of method
     
    } // end of program
    .

  6. #5
    Junior Member
    Join Date
    Jun 2010
    Posts
    28
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Subroutine Help

    I tried some of those codes, but it still didn't work. I initialized all of the variables, but now it says it can't find the symbols of noun, adjective, verb, noun2, adjective2, and verb2.

  7. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Subroutine Help

    Quote Originally Posted by Kimimaru View Post
    I tried some of those codes, but it still didn't work. I initialized all of the variables, but now it says it can't find the symbols of noun, adjective, verb, noun2, adjective2, and verb2.
    At what point can't it find the symbols?

    In the outside methods, or in the beginning?

    What does your current code look like?



    Wait....


    Now I think I know what's going on.
        static  String typeinNoun()
        {       // beginning of method
          String      noun = JOptionPane.showInputDialog(null, "Type in a noun.", "Noun");
    return(noun);  
     
        }   // end of method
    static String typeinAdjective()
    { // beginning of method
    String adjective = JOptionPane.showInputDialog(null, "Type in an adjective.", "Adjective");
    return(adjective);
    } // end of method
     
    static String typeinVerb()
    { // beginning of method
          String      verb = JOptionPane.showInputDialog(null, "Type in the past form of a verb.", "Verb");
    return(verb);
    } // end of method
     
    static String typeinNoun2()
    { // beginning of method
          String      noun2 = JOptionPane.showInputDialog(null, "Type in another noun.", "Noun");
    return(noun2);
    } // end of method
     
    static String typeinVerb2()
    { // beginning of method
           String     verb2 = JOptionPane.showInputDialog(null, "Type in another past form of a verb.", "Verb");
    return(verb2);
    } // end of method
     
    static String typeinAdjective2()
    { // beginning of method
           String     adjective2 = JOptionPane.showInputDialog(null, "Type in another adjective.", "Adjective");
    return(adjective2);
    } // end of method

    Now it should find your symbols.

    Oh, it would be better if it asked you to enter a 1,2, or 3 immediately, instead of asking for all those nouns, verbs, and adjectives first, which it is currently coded to do.

    Also, then can click cancel every time and get null placed in there for adjective, verb, noun, noun2, verb2, and adjective2. Is that what you want?

    When it asks to exit, I enter a 3 and it exits, even though 2 is for exit, and it never tells me that I entered the wrong number.

    Also, if I enter

    3
    Noun
    Verb
    Adjective
    Noun
    Verb
    Adjective

    It gives me: "The Noun went on a stroll in the Adjective park and Verb the tree. Then, a Noun came out of nowhere and Verb on the Adjective mole."

    I noticed it if do this:

    Enter a 1,2, or 3: Click OK button

    exits like I clicked CANCEL.
    Last edited by javapenguin; October 6th, 2010 at 06:17 PM.

  8. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile Re: Subroutine Help

    Now it should work, sorta. A few kinks to work out, which I pointed out earlier.


    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.*;
    class Project
    { // beginning of class
        public static void main (String[]args)
        { // beginning of main
    int choice2, answer2, answer4;
                choice2 = 1;
    answer2 = 0; // initializes it so it won't give error
    answer4 = 0;
            String noun, adjective, verb, noun2, adjective2, verb2, choice, answer, answer3;
    noun = "";
    adjective = "";
    verb = "";
    noun2 = "";
    adjective2 = "";
    verb2 = "";
    choice = "";
    answer = "";
    answer3 = "";
     
         try
         {  // beginning of try
     
     
          while (choice2 == 1) 
          { // beginning of while
            answer = JOptionPane.showInputDialog(null, "Hello! Which story would you like to see? Type either '1', '2', or '3' to choose your story.", "Choose your story!", JOptionPane.INFORMATION_MESSAGE);
            answer2 = Integer.parseInt(answer);
                noun = typeinNoun();
                           verb = typeinVerb();
                       adjective = typeinAdjective();
                   noun2 = typeinNoun2();
               verb2 = typeinVerb2();
             adjective2 = typeinAdjective2();
     
             if (answer2 == 1) 
             { // beginning of if
               // typein();  
                JOptionPane.showMessageDialog(null, "The " + noun + " went on a stroll in the " + adjective + " park and " + verb + " the tree. Then, a " + noun2 + " came out of nowhere and " + verb2 + " on the " + adjective2 + " mole.", "Story", JOptionPane.INFORMATION_MESSAGE);   
                choice = JOptionPane.showInputDialog(null, "Do you want to redo the story? Press '1' for yes and '2' for no.", "Restart?", JOptionPane.INFORMATION_MESSAGE);
                choice2 = Integer.parseInt(choice);
             } // end of if
     
             else if (answer2 == 2)
             { // beginning of else if
               // typein();  
                JOptionPane.showMessageDialog(null, "The " + noun + " went on a stroll in the " + adjective + " park and " + verb + " the tree. Then, a " + noun2 + " came out of nowhere and " + verb2 + " on the " + adjective2 + " mole.", "Story", JOptionPane.INFORMATION_MESSAGE);   
                choice = JOptionPane.showInputDialog(null, "Do you want to redo the story? Press '1' for yes and '2' for no.", "Restart?", JOptionPane.INFORMATION_MESSAGE);
                choice2 = Integer.parseInt(choice);
             } // end of else if
     
             else if (answer2 == 3)
             { // beginning of else if
              //  typein();  
                JOptionPane.showMessageDialog(null, "The " + noun + " went on a stroll in the " + adjective + " park and " + verb + " the tree. Then, a " + noun2 + " came out of nowhere and " + verb2 + " on the " + adjective2 + " mole.", "Story", JOptionPane.INFORMATION_MESSAGE);   
                choice = JOptionPane.showInputDialog(null, "Do you want to redo the story? Press '1' for yes and '2' for no.", "Restart?", JOptionPane.INFORMATION_MESSAGE);
                choice2 = Integer.parseInt(choice);
             } // end of else if
             else
             { // beginning of else
                JOptionPane.showMessageDialog(null, "Please type in a number from 1 to 3!", "Wrong Number!", JOptionPane.INFORMATION_MESSAGE);
             }          // end of else
          } // end of while
          if (choice2 == 2)
          { // beginning of if
            answer3 = JOptionPane.showInputDialog(null, "Do you want to see another story? Please type '1' for yes and '2' for no.", "Other Story", JOptionPane.INFORMATION_MESSAGE);
            answer4 = Integer.parseInt(answer3);
           if (answer4 == 1)
           { // beginning of if
            choice2 = 1;
           } // end of if
           else if (answer4 == 2)
           { // beginning of else if
            JOptionPane.showMessageDialog(null, "Goodbye!", "End of Stories", JOptionPane.INFORMATION_MESSAGE);
            System.exit(0);
           } // end of else if
     
           else
           { // beginning of else
            JOptionPane.showMessageDialog(null, "Please choose either 1 or 2!", "Wrong Number!", JOptionPane.INFORMATION_MESSAGE);
           } // end of else
          } // end of if
         } // end of try
         catch (java.lang.Throwable n)
         { // beginning of catch
            JOptionPane.showMessageDialog(null, "Goodbye!", "Clicked Cancel", JOptionPane.INFORMATION_MESSAGE);
         }          // end of catch
        } // end of main
     
    // if it still can't find them as is, have the respective Strings as parameters.
    // like this
     
    /*
    static String typeinNoun(String noun)
    { // beginning of method
    noun = JOptionPane.showInputDialog(null, "Type in a noun.", "Noun");
    return(noun);  
    } // end of method
    I'm not sure if it needs you to pass the parameters, so I'm leaving this in case you do
     
    if you do need them, call it like this:
     
    noun = typeInNoun(noun);
    */
        static String typeinNoun()
        {       // beginning of method
              String  noun = JOptionPane.showInputDialog(null, "Type in a noun.", "Noun");
    return(noun);  
     
        }   // end of method
    static String typeinAdjective()
    { // beginning of method
    String adjective = JOptionPane.showInputDialog(null, "Type in an adjective.", "Adjective");
    return(adjective);
    } // end of method
     
    static String typeinVerb()
    { // beginning of method
             String   verb = JOptionPane.showInputDialog(null, "Type in the past form of a verb.", "Verb");
    return(verb);
    } // end of method
     
    static String typeinNoun2()
    { // beginning of method
          String      noun2 = JOptionPane.showInputDialog(null, "Type in another noun.", "Noun");
    return(noun2);
    } // end of method
     
    static String typeinVerb2()
    { // beginning of method
              String  verb2 = JOptionPane.showInputDialog(null, "Type in another past form of a verb.", "Verb");
    return(verb2);
    } // end of method
     
    static String typeinAdjective2()
    { // beginning of method
               String adjective2 = JOptionPane.showInputDialog(null, "Type in another adjective.", "Adjective");
    return(adjective2);
    } // end of method
     
    } // end of program
    project.txt
    Attached Images Attached Images
    Last edited by javapenguin; October 6th, 2010 at 08:24 PM.

  9. #8
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Subroutine Help

    Just since nobody has said this so far, Java doesn't have subroutines. Java has methods.

    It's not just a matter of using the correct terminology, using the correct word helps you to more correctly conceptualize the working of a Java program. Classes have methods (keyword: static), objects have methods. Methods alter state and/or return values.

    db