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 7 of 7

Thread: Need help my values wont return

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help my values wont return

    hi guys just joined the site im new to programming taking a course in uni started this year and had no previous programming knowledge before i started

    import javax.swing.*;
    class Project
    {
    	public static void main (String [] args)
    	{
    		String Title [] = new String [5]; //  array 1
    		int Length [] = new int [5]; // array 2
    		String Artist [] = new String [5]; // array 3
     
    		Menu(Title, Length, Artist);
    	}
     
    	public static void Menu(String Title [],int Length [] , String Artist [])
    	{
    		//menu given, user can choose from options, has been converted into int for the next part
    		int Menu = Integer.parseInt(JOptionPane.showInputDialog("\t" + "\t" +"Menu" + "\n" + "If you would like to enter more collections please enter: 1" +
    															     "\n" + "If you would like to search for a specific song please enter: 2" + "\n" +
    																  "If you would like to print the whole collection please enter: 3" + "\n" +
    																   "If you would like to terminate the program enter: 4"));
     
    		//if else statments will tell the program what to do with the information entered by the user.
     
    		if (Menu == 1)
    		{
    			Input(Title, Length, Artist);
    		}
    		else if (Menu == 2)
    		{
    			Search(Title, Length, Artist);
    		}
    		else if (Menu == 3)
    		{
    			Print(Title, Length, Artist);
    		}
    		else if (Menu == 4)
    		{
    			final int Quit = 4;
    		}
    		else if ((Menu < 1) || (Menu > 4))
    		{
    			JOptionPane.showMessageDialog(null, "Invalid entry please click OK to choose again");
    		}
     
    	}
     
    	// method to input information
     
    	public static void Input(String Title [], int Length [] , String Artist [])
    	{
    		// this loop here fills my arrays
    		for (int count = 0; count < 5; count++)
    		{
    			String title = JOptionPane.showInputDialog("Enter Title of the Song");// this will fill array 1
    			return Title[count] = title;
     
    			int length = Integer.parseInt(JOptionPane.showInputDialog("Enter Length of the song in Seconds"));// this will fill array 2
    			return Length[count] = length;
     
    			String artist = JOptionPane.showInputDialog("Enter the name of the Artist");// this will fill array 3
    			return Artist[count] = artist;
    		}
     
     
     
    		Menu(Title, Length, Artist);
    	}
     
    	// method to search
     
    	public static void Search(String Title [], int Length [] , String Artist [])
    	{
    		int Find = Integer.parseInt(JOptionPane.showInputDialog("Please Enter the Array you would like to access"));
     
    		if (Find == 0)
     		{
    			JOptionPane.showMessageDialog(null, Title[0] + " " + Length[0] + " " + Artist[0]);
    		}
    		else if (Find == 1)
    		{
    			JOptionPane.showMessageDialog(null, Title[1] + " " + Length[1] + " " + Artist[1]);
    		}
    		else if (Find == 2)
    		{
    			JOptionPane.showMessageDialog(null, Title[2] + " " + Length[2] + " " + Artist[2]);
    		}
    		else if (Find == 3)
    		{
    			JOptionPane.showMessageDialog(null, Title[3] + " " + Length[3] + " " + Artist[3]);
    		}
    		else if (Find == 4)
    		{
    			JOptionPane.showMessageDialog(null, Title[4] + " " + Length[4] + " " + Artist[4]);
    		}
     
    		Menu(Title, Length, Artist);
     
    	}
     
    	//method to print the whole colloction available
     
    	public static void Print(String Title [], int Length [] , String Artist [])
    	{
    		// print out entire array (collections)
    		JOptionPane.showMessageDialog(null, "1 " + Title[0] + " " + Length[0] + " " + Artist[0] + "\n" +
    											 "2 " + Title[1] + " " + Length[1] + " " + Artist[1] + "\n" +
    						  					  "3 " + Title[2] + " " + Length[2] + " " + Artist[2] + "\n" +
    									   	       "4 " + Title[3] + " " + Length[3] + " " + Artist[3] + "\n" +
    						       					"5 " + Title[4] + " " + Length[4] + " " + Artist[4] + "\n");
    		Menu(Title, Length, Artist);
    	}
     
    	//method to quit
     
    	public static void Quit()
    	{
    		System.exit(0);
    	}
    }

    this is my error



  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Need help my values wont return

    you have a method named Input, which has a return type 'void' means that this method doesnt return anything so it should not have a return statement inside of it


    you might have a look at this
    Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help my values wont return

    thanks for the reply
    I realised that before and changed public static void to public static int however it wouldn't let the strings pass and gave me an error saying incompatible data types and referred to the Title and the Artist

    been stuck on this for over a day and i just cant get my head around it

  4. #4
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Need help my values wont return

    Firstly, make the three arrays global. They are only visible inside the main method. After that, just remove the three 'return' statements in the input method but leave the Title[count] = title; part in place - as Norm suggested.

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help my values wont return

    sorry if this sounds dumb how do i make the arrays global

  6. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Need help my values wont return

    the term global is used in some other language like C and C++ or VB, what he mean is data member(an instance data member particularly), instead of creating those things(arrays in your case) locally inside a method(main method in your case), you should define those array outside your methods but inside your class like

    private int[] myGlobalArray = {somethinginside};

    now with that, any methods or statements inside your class can share this array(Globally)


    please take a look at this
    Declaring Member Variables (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Last edited by chronoz13; January 7th, 2012 at 08:26 AM.

  7. #7
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Need help my values wont return

    The OP PM'ed me with a question on global variables. I am posting my response here for the greater good

    Quote Originally Posted by sajeed
    thanks for you post but im new to programming how do i make the array global
    Oh that's cool. You should read up on variable scope and visibility.

    Basically, when you define a variable - be it a primitive data type (int, double, String, etc), an object or an array the variable is only visible to the 'block' it is in. For example, consider this simple if statement.

    if (true) {
        int myInteger = 3;
    }
    System.out.println(myInteger);

    This code will not compile, there is a syntax error. It will complain that it cannot find myInteger when it tries to println. This is because myInteger falls out of scope and is not visible outside of the if statement. The correct way to do this is:

    int myInteger = 0;
    if (true) {
        myInteger = 3;
    }
    System.out.println(myInteger);

    This will compile. Do you see the difference? Instead of defining the variable inside the if statement, I instantiated it before hand. This makes the variable visible to the if statement and the println after it.

    Making a variable global is the same idea. This time, we create the variable in the class, not in a method. Like so.

    public class MyClass {
     
         public int myGlobalInteger = 0;
     
         public void foo() {
                int myLocalInteger = 3;
                myGlobalInteger = 3;
         }
     
        public void bar() {
                System.out.println(myLocalInteger);  
                System.out.println(myGlobalInteger);      
        }
    }

    In this example, the first println will not compile. myLocalInteger is not visible to the method bar(). The second println will compile because myGlobalInteger is visible to the entire class.

    I hope this helps.

Similar Threads

  1. [SOLVED] compare form values with database values
    By VaniRathna in forum Java Servlet
    Replies: 2
    Last Post: October 24th, 2011, 02:48 AM
  2. Return Object does not return the expected output
    By Nour Damer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 13th, 2011, 07:24 AM
  3. WHY WONT THIS COMPILE!
    By usmc0311 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 12th, 2011, 02:42 PM
  4. Character Values Inside of Number Values
    By bgroenks96 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 2nd, 2011, 08:27 PM
  5. Return the rgb values from a jpg image
    By newparticipant in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 25th, 2011, 03:47 PM