Results 1 to 17 of 17
  1. #1
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default Arrays...Please HELP!!

    Hi All,

    Can someone please help!!

    I am trying to get my main method to print out my Array.

    I keep getting an error though. Can someone tell me where I am going wrong please?

    Java Code:
    import java.util.Arrays;
    
    
    
    public class ReverseArray
    {
       public static void main(String[] args)
       {
            int[] anArray; //Declares an array of integers
             
             anArray = new int[3];
             
             anArray[0] = 1;
             anArray[1] = 2;
             anArray[2] = 7;
             
             
             
       } 
       
       public static void printReverse(int[] data)
       {
             
             System.out.print("Here are your numbers: " + anArray[2]);
             
       }
    The compiler does not seem to like it?

    Sorry, I am a newbie!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What compile error do you get? The code looks correct syntactically, but doesn't do anything, you can print it in the main method or call the methods in the main method.

  4. #4
    BiteMuncher's Avatar
    BiteMuncher is offline Member
    Join Date
    Feb 2011
    Location
    South Africa
    Posts
    51
    Rep Power
    0

    Default

    Hi.
    Try this:

    public static void printReverse(int[] data)
    {

    for(int num : data)
    System.out.print("Here are your numbers: " + data[num]);
    }

    I'm not sure if you want to print the numbers in reverse as your method suggests. If so try:

    public static void printReverse(int[] data)
    {

    for(int i=2 ; i>=0; i--)
    System.out.print("Here are your numbers: " + data[i]);
    }

    Make sure to add your parameter when calling this method:
    printReverse(anArray);
    Sorry, I only speak machine language. Yes or a No?:confused:

  5. #5
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    Yeah sorry about that.

    Okay, the Main runs although it does not actually print anything, so that is fine.

    However, the printReverse method says this:

    Error: Empty Parameter Field.

    I am using BlueJ by the way.

    Many thanks.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by PrimalScientist View Post
    Yeah sorry about that.

    Okay, the Main runs although it does not actually print anything, so that is fine.

    However, the printReverse method says this:

    Error: Empty Parameter Field.

    I am using BlueJ by the way.

    Many thanks.
    Don't paraphrase your error -- instead show the full text of the actual error messages, and also show the line(s) of code that cause the errors.

  7. #7
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by BiteMuncher View Post
    Hi.
    Try this:

    public static void printReverse(int[] data)
    {

    for(int num : data)
    System.out.print("Here are your numbers: " + data[num]);
    }

    I'm not sure if you want to print the numbers in reverse as your method suggests. If so try:

    public static void printReverse(int[] data)
    {

    for(int i=2 ; i>=0; i--)
    System.out.print("Here are your numbers: " + data[i]);
    }

    Make sure to add your parameter when calling this method:
    printReverse(anArray);
    Thank you!!

    I will try this, but I need to show the array for my Lab Class you see.
    Although I like the for loop!! :)

  8. #8
    Chi
    Chi is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    import java.util.Arrays;
    public class ReverseArray
    {
    public static void main(String[] args)
    {
    int[] anArray; //Declares an array of integers

    anArray = new int[3];

    anArray[0] = 1;
    anArray[1] = 2;
    anArray[2] = 7;
    System.out.print("Here are your numbers: " + anArray[2]);
    }
    }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by BiteMuncher View Post
    Hi.
    Try this:
    ....
    Thanks for spoon-feeding an answer rather than try to help the poster think through the problem.

  10. #10
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Don't paraphrase your error -- instead show the full text of the actual error messages, and also show the line(s) of code that cause the errors.
    Okay, thanks again.

    The compiler states there are no syntax errors!
    Only when I try to run the program that the error appears. There are no line numbers or anything.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Te enhanced for loop is nice but it is good to understand the other method as well. Where is the code that calls the reverse method?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by PrimalScientist View Post
    The compiler states there are no syntax errors!
    Only when I try to run the program that the error appears. There are no line numbers or anything.
    Then show the full error that occurs when you run the program. I don't even see where you call that method.

  13. #13
    BiteMuncher's Avatar
    BiteMuncher is offline Member
    Join Date
    Feb 2011
    Location
    South Africa
    Posts
    51
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Thanks for spoon-feeding an answer rather than try to help the poster think through the problem.
    A bit harsh there but I'll keep that in mind.
    Last edited by BiteMuncher; 03-05-2011 at 10:00 PM.
    Sorry, I only speak machine language. Yes or a No?:confused:

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Why do you have a java.util.Array import? Arrays are part of the java Lang package which is automatically imported for you. The code you supplied doesn't fit your questions.

  15. #15
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    Okay, here you go and thank you also for taking the time to help me.

    It is greatly appreciated all.

    Hope this is okay.
    Attached Thumbnails Attached Thumbnails Arrays...Please HELP!!-picture-1.jpg   Arrays...Please HELP!!-picture-2.jpg   Arrays...Please HELP!!-picture-3.jpg  

  16. #16
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Why do you have a java.util.Array import? Arrays are part of the java Lang package which is automatically imported for you. The code you supplied doesn't fit your questions.
    Okay, I need to produce 2 methods. One main, and one called reverseArray.
    I need to produce a very simple array, and get the main method to print that array.

    I am not sure how to get my main method to talk to the reverseArray method.
    The reverseArray is to print the numbers associated with the Array in reverse order.

  17. #17
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Loop through the array in main printing at each iteration, then create a static reverse method and start at the end of the loop and loop down with -- instead of ++ and print each iteration.

Similar Threads

  1. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 11:32 AM
  2. Arrays
    By PhQ in forum New To Java
    Replies: 3
    Last Post: 04-18-2010, 09:40 PM
  3. using arrays
    By monikat in forum New To Java
    Replies: 3
    Last Post: 04-17-2010, 12:47 PM
  4. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  5. Please help me with arrays!
    By ddrcan in forum New To Java
    Replies: 2
    Last Post: 08-10-2009, 05:30 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
  •