Results 1 to 9 of 9
  1. #1
    ianni is offline Member
    Join Date
    Mar 2014
    Posts
    5
    Rep Power
    0

    Default return value from array

    hello everybody,i m new to java and i have one problem,i want to return values from arrays to the main and the problem is i cant use my variables from my constructor,i use new variables in my functions and i know this is no good,when i used the variables from the constructor in my functioni i have a compilations errors,also i want to create un object in main and with this object i want to call the functions,i dont know if you understand what i m trying to say beacuse my english is not so good,sorry about that
    Here is my code

    Java Code:
       package javaapplication4;
    import java.util.Scanner;
    
    
    public class JavaApplication4 {
        
        public static int[] MyInt;
        public static double[] MyDouble;
        public static String[] MyString;
        public static char[] MyChar;
        
        public JavaApplication4()
        {
            MyInt = new int[4];
            MyDouble = new double[4];
            MyString = new String[4];
            MyChar = new char[4];
        }
        
        public static int[] ReturnInt()
        {
            int[] x;
            x = new int[4];
            
            x[0] = 12;
            x[1] = 33;
            x[2] = 55;
            x[3] = 23;
            
            return (x);
        }
        
        public static double[] ReturnDouble()
        {
            double[] y;
            y = new double[4];
                    
            y[0] = 33.4;
            y[1] = 12.32;
            y[2] = 92.3;
            y[3] = 8.44;
            
            return(y);
        }
        
        public static String[] ReturnString()
        {
            String[] aString;
            aString = new String[4];
            
            aString[0] = "hello";
            aString[1] = "my name is paul";
            aString[2] = "i m a java";
            aString[3] = "programmer";
            
            return (aString);
        }
        
       
        public static char[] ReturnChar()
        {
            char[] ch;
            ch = new char[4];
            
            ch[0] = 'a';
            ch[1] = 'b';
            ch[2] = 'c';
            ch[3] = 'd';
            
            return (ch);
        }
        
    
        
    
          public static void main(String[] args) {
            
          Scanner in = new Scanner(System.in); 
          
            int i;
            int[] ArrayOfInt;
            double[] ArrayofDouble;
            String[] ArrayofString;
            char[] ArrayofChar;
            
            ArrayOfInt = ReturnInt();
            ArrayofDouble = ReturnDouble();
            ArrayofString = ReturnString();
            ArrayofChar = ReturnChar();

    The code is working when i run it and i have the right result in my screen but i know this is all wrong with the variables,thank you very much

  2. #2
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: return value from array

    What are you trying to accomplish with this code? And I can't see what you are using Scanner() for?
    Note: Your constructor doesn't have any arguments

    you need to understand your code 1st
    Last edited by Kagiso; 03-11-2014 at 01:14 PM.

  3. #3
    ianni is offline Member
    Join Date
    Mar 2014
    Posts
    5
    Rep Power
    0

    Default Re: return value from array

    i m just try to return an array from the function in the main,but i dont know how to use the variables of the class in the functions

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,651
    Rep Power
    5

    Default Re: return value from array

    First, since your constructor doesn't really do anything special, just move those statements above the constructor.
    You can declare and allocate them at the same time.

    Why are you declaring all of your methods static?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    ianni is offline Member
    Join Date
    Mar 2014
    Posts
    5
    Rep Power
    0

    Default Re: return value from array

    i declare all the method static beacause the compiler make me do this,if i dont do this i have compilations eror,i dont know why is he doing this

  6. #6
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: return value from array

    Quote Originally Posted by ianni View Post
    i m just try to return an array from the function in the main,but i dont know how to use the variables of the class in the functions
    I'm sorry but I do not understand any of this... So I'll assume by saying "variable of class"- you mean a reference variable... and that you want to use a reference variable to call those methods.
    If that's the case then I suggest you learn the difference between statics & instances....

  7. #7
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: return value from array

    Quote Originally Posted by ianni View Post
    i declare all the method static beacause the compiler make me do this,if i dont do this i have compilations eror,i dont know why is he doing this
    Error message says something like "... cannot be reference from a static context" ???

    All that you had to do was instantite your class e.g. MyClass m = new MyClass();
    and then call methods using that instance variable e.g. m.myMethod();

    So your methods don't necesarily have to be static

    like I said in the previous post ... learn the difference between statics & instances
    Last edited by Kagiso; 03-11-2014 at 10:48 PM.

  8. #8
    ianni is offline Member
    Join Date
    Mar 2014
    Posts
    5
    Rep Power
    0

    Default Re: return value from array

    I'm sorry but I do not understand any of this
    i m realy sorry about that,like i say in my first post my english is not good and i cant explain what exactly i m trying to do but i think you understand,anyway thank you for answer me

    Error message says something like "... cannot be reference from a static context" ???
    yes this is the error message.ok i l do what you said,thank you very much for your for helping me

  9. #9
    ianni is offline Member
    Join Date
    Mar 2014
    Posts
    5
    Rep Power
    0

    Default Re: return value from array

    I'm sorry but I do not understand any of this
    i m realy sorry about that,like i say in my first post my english is not good and i cant explain what exactly i m trying to do but i think you understand,anyway thank you for answer me

    Error message says something like "... cannot be reference from a static context" ???
    yes this is the error message.ok i l do what you said,thank you very much for helping me

Similar Threads

  1. return position in array
    By forms in forum New To Java
    Replies: 3
    Last Post: 11-19-2011, 09:08 PM
  2. Return multiply array changes?
    By Teclis in forum New To Java
    Replies: 4
    Last Post: 04-20-2011, 07:16 AM
  3. How to return Jobject array in jni
    By mahesh.gmb in forum Advanced Java
    Replies: 0
    Last Post: 02-02-2011, 08:23 AM
  4. return array
    By rfviki in forum New To Java
    Replies: 2
    Last Post: 11-03-2010, 01:05 PM
  5. return array problem
    By doha786 in forum New To Java
    Replies: 3
    Last Post: 03-30-2010, 05:08 PM

Posting Permissions

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