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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 31

Thread: Need help with creating method for Array.

  1. #1
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    125
    My Mood
    Nerdy
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Need help with creating method for Array.

    A quickly brief history of my Homework assignment.

    Using the code examples that you created, sort the following items: Rocket J. Squirrel, Bullwinkle J. Moose, Boris Badenov, Natasha Fatale, Fearless Leader, Mr. Big, Cloyd, Gidney, Metal-Munching Moon Mice, Capt. Peter "Wrongway" Peachfuzz, Edgar, and Chauncy
    Your program should perform the following tasks.
    1. Read the data in from the keyboard into the array interactively.
    2. Print the array.
    3. Sort the array in ascending order using the bubble sort;
    4. Print the array;
    5. Sort the array in descending order using the bubble sort;
    6. Print the array.
    Further specification:
    You must use a separate method for each task.
    There must be a method for loading the data into the array.
    There must be one and ONLY one method for printing the contents of the array.
    There must be a method for sorting the array in ascending order.
    There must be a method for sorting the array in descending order.
    If you do not use the bubble sort in the methods which sort the data, do not bother handing your code in to me.

    The list of names is long. Save yourself some aggravation with these two suggestions:
    1. Use the nextLine() method to read the String data from the keyboard into the array.
    2. Do NOT enter all the data each time you test. Start by entering only one character instead of
    each full name. Enter all of the entire names when you are sure all of your sorting and printing
    algorithms are running correctly.


     
    import java.util.*;
     
    public class lab7
    {
        public static void main (String[] args)
        {
     
     
     
        String x = " ";
        String x1 = "";
        x1 = storedList(x);     
        printArray(x1);
     
     
     
        }
     
     
        public static String storedList( String stored)
        {
            Scanner scan = new Scanner(System.in);
     
            String[] list = new String[12];
     
            System.out.println( "Hello user, I will sort your list in asscending and descending order?  ");
            System.out.println( "Please enter your string." );
            for( int i = 0; i <=11; i++)
            {
                list[i] = scan.nextLine();
            }
     
            return stored;
        }
     
        public static void printArray(String[] displayed)
        {
     
         String[] list = new String[12]; 
     
         for (int i = 0; i <= 11; i++)
             {
             System.out.print( list[i] + " " );
             }
     
        }  
     
    }


    What I'm having issues is passing arguments into each method. I was able to figure out how to make one method to accept my array. But I'm having trouble creating a method to print the array. I keep receiving this compiler error. Please refer to screen shot. PLEASE help!

    complier_errro.jpg

    Thanks Again!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,778
    Thanks
    54
    Thanked 2,341 Times in 2,312 Posts

    Default Re: Need help with creating method for Array.

    I keep receiving this compiler error
    Please copy the full text of the error messages and paste it here. Not an image, the text.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    125
    My Mood
    Nerdy
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with creating method for Array.

    method printArray in class lab7 cannot be applied to given types;
    requried:java.lang.String[]; found java.lang.String; reason: actual argument java.lang.String cannot converted to java.lang.String by method invocation conversion

    The operator that you use here cannot be used for the
    typed of value that you are using it for. You are either
    using the wrong type here, or the wrong operator.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,778
    Thanks
    54
    Thanked 2,341 Times in 2,312 Posts

    Default Re: Need help with creating method for Array.

    What happened to the text of the error message. There should be separate lines for lots of it. Parts of the message are missing like the source line and the line number. Without the source lines and line numbers I can't suggest any changes.
    requried:java.lang.String[];
    found java.lang.String
    The compiler wants there to be a String array(required: ) used in that statement which is using a String(found: ) instead.

    Please copy full text of error message and paste it here. Here is a sample:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    Note: The line number and the source line are shown
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    125
    My Mood
    Nerdy
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with creating method for Array.

    I'm using blueJ so I'm sorry it doesn't refer to line. It just flash to the problem code which is this.

    printArray(x1);

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Need help with creating method for Array.

    The error message tells you what the problem is. Look at how the printArray method is defined ---

        public static void printArray(String[] displayed)
        {
     
         String[] list = new String[12]; 
     
         for (int i = 0; i <= 11; i++)
             {
             System.out.print( list[i] + " " );
             }
     
        }

    What is the type of parameter that it's supposed to accept?
    What type is actually passed in by you when you call the method?

    And actually, the error message answers my questions nicely:
    requried:java.lang.String[];
    found java.lang.String;

    Also: What is the purpose of the method storedList()? What is it supposed to get from the user?

  7. #7
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    125
    My Mood
    Nerdy
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with creating method for Array.

    Well I know where my issue is. For some apparent reason after calling my storedList I want to send this array to the print method to print the array. The method should be reusable. But I'm having trouble inputting this array in this method. I keep receiving this compiler I don't see any issues in my code. Does that help?

  8. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Need help with creating method for Array.

    So you understand that the method requires that an array be passed into it, and you're not passing in an array, right? You're passing in a single String. So look to see where you get that single String in the first place. It is here that you should be receiving an array of String. Keep tracing back into your code to see why this error is occurring. If you look carefully, you'll see the error.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,778
    Thanks
    54
    Thanked 2,341 Times in 2,312 Posts

    Default Re: Need help with creating method for Array.

    I'm having trouble inputting this array in this method.
    Could you be more explicit and give the name of "this array" and the name of "this method"?

    The compiler does not think "this array" is an array.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    125
    My Mood
    Nerdy
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with creating method for Array.

    Sure thing,

    There should be a method named storedList

    storedList should ask the user for Strings value(12 to be precise). Each string value should be stored in the array in the storedList method. Once the strings values are stored I want to return the array. And then print the array in the printArray method.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,778
    Thanks
    54
    Thanked 2,341 Times in 2,312 Posts

    Default Re: Need help with creating method for Array.

    Check to make sure that the storedList() method is defined to return an array.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    125
    My Mood
    Nerdy
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with creating method for Array.

    I think that's where I'm having allot of issue at. For some reason I don't think I'm returning an array after running the method. Below is the method that is suppose to ask for the String.

     
        public static String storedList(String[] stored)
        {
            Scanner scan = new Scanner(System.in);
     
            String[] list = new String[12];
     
            System.out.println( "Hello user, I will sort your list in asscending and descending order?  ");
            System.out.println( "Please enter your string." );
            for( int i = 0; i <=11; i++)
            {
                list[i] = scan.nextLine();
            }
     
            return stored;
        }

    I have looked in my book on how store arrays. But it said nothing about storing String or char values into methods. I know how to do with numbers but not Strings. Do you have any suggestions?

  13. #13
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Need help with creating method for Array.

    Quote Originally Posted by Rain_Maker View Post
    I think that's where I'm having allot of issue at. For some reason I don't think I'm returning an array after running the method.
    Your code will tell you what you're returning. What variable are you returning? What type is it? I've been trying to get you to look critically at this method. Please do so. Why are you creating an array, list, and completely ignoring it when the method returns? Why have you changed the parameter to an array, as that makes no sense?

    edit: sorry, the above was posted in frustration. The key is to think through what you're doing before you type in code. Usually in a method like this, you'll create something of interest, here the list, and then *return* that item of interest, again the list. Else you're creating the object for no purpose whatsoever. And you don't want to make random senseless changes to your code like making the parameter an array when it is not supposed to be one.

  14. #14
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    125
    My Mood
    Nerdy
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with creating method for Array.

    Okay I see I have corrected my method.

     
    import java.util.*;
     
    public class lab7
    {
        public static void main (String[] args)
        {
     
        String test, test1;
        test = " ";
        test = storedList(test);
     
        printArray(test);
     
     
     
        }
     
     
        public static String storedList(String stored)
        {
            Scanner scan = new Scanner(System.in);
     
            String[] list = new String[12];
     
            System.out.println( "Hello user, I will sort your list in asscending and descending order?  ");
            System.out.println( "Please enter your string. Press enter for each string value." );
     
            int i; 
            for ( i = 0; i <=12; i++)
            list[i] = scan.nextLine();
     
            return list[i];
        }
     
        public static void printArray(String displayed)
        {
           String list[] = new String[12];
     
           for (int i = 0; i <= 11; i++)
           System.out.print( list[i] + " " );      
        }  
     
    }

    It took me long time to understand what was really the problem. But it seems like after I fix one thing its another problem. When running my code I have another issue. I don't receiving an compiler error but another area when running my terminal.

    Hello user, I will sort your list in asscending and descending order?
    Please enter your string. Press enter for each string value.
    a
    b
    c
    d
    e
    d
    f
    g
    h
    j
    y
    t
    h
    java.lang.ArrayIndexOutOfBoundsException: 12
    at lab7.storedList(lab7.java:38)
    at lab7.main(lab7.java:17)

    I know that for some reason instead of the method ending after storing the array. Its continuing after 12th memory block has been inserted in the array. smh ***sigh***

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,778
    Thanks
    54
    Thanked 2,341 Times in 2,312 Posts

    Default Re: Need help with creating method for Array.

    The terminating condition in the for loops should NOT use hardcoded numbers for going through arrays.
    They should use the length field of the array to get the array's size.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    125
    My Mood
    Nerdy
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with creating method for Array.

    When you say they should use the length field, what are you referring to? I'm a little confuse from what I seen in my text book it uses example which hard coded values are in place to run through the for loop.

    --- Update ---

    Thank you all for being patient with me by the way.

  17. #17
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Need help with creating method for Array.

    Arrays have a length field that should be used:

    // for array myArray...
    for (int i = 0; i < myArray.length; i++) {
     
    }

  18. #18
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    125
    My Mood
    Nerdy
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with creating method for Array.

    I still receive the same error message. I been playing around with it but I receive the same results.

    public class lab7
    {
        public static void main (String[] args)
        {
     
        String test, test1;
        test = " ";
        storedList(test);
     
      //  printArray(test);
     
     
     
        }
     
     
        public static String storedList(String stored)
        {
            Scanner scan = new Scanner(System.in);
     
            String[] list = new String[12];
     
            System.out.println( "Hello user, I will sort your list in asscending and descending order?  ");
            System.out.println( "Please enter your string. Press enter for each string value." );
     
            int i = 0; 
     
                for ( i = 0 ; i <= list.length; i++)
                {
                    list[i] = scan.nextLine();
                }
     
                return list[i];
     
     
        }
     
          // public static void printArray(String displayed)
           // {
           //String list[] = new String[12];
     
           //for (int i = 0; i <= list.length; i++)
          // System.out.println( list[i] + " " );      
          //  }  
     
    }

    Ouput below
    Hello user, I will sort your list in asscending and descending order?
    Please enter your string. Press enter for each string value.
    a
    b
    c
    d
    e
    f
    g
    h
    i
    j
    k
    f
    e

    java.lang.ArrayIndexOutOfBoundsException: 12
    at lab7.storedList(lab7.java:39)
    at lab7.main(lab7.java:17)
    ****sigh****

  19. #19
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Need help with creating method for Array.

    Please look carefully at your for loop vs. mine. There is a critical difference.

  20. #20
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    125
    My Mood
    Nerdy
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with creating method for Array.

    You put less than instead of less than or equal too.

  21. #21
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Need help with creating method for Array.

    Quote Originally Posted by Rain_Maker View Post
    You put less than instead of less than or equal too.
    Exactly. Remember that arrays are 0 based, and so if an array has length of 8, it will hold items in spots using indices 0 to 7. When you loop until i < myArray.length your for loop stops before the index reaches the length of the array, which is what you want. Again if myArray has a length of 8, it will have 8 items going from myArray[0] to myArray[7]. If I used <=, I would be trying to obtain item myArray[8] which doesn't exist and which throws the exception that you are seeing.

  22. #22
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    125
    My Mood
    Nerdy
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with creating method for Array.

    I understand but for some reason when I run the program again I receive the same error.

     
        public static String storedList(String stored)
        {
            Scanner scan = new Scanner(System.in);
     
            String[] list = new String[12];
     
            System.out.println( "Hello user, I will sort your list in asscending and descending order?  ");
            System.out.println( "Please enter your string. Press enter for each string value." );
     
            int i; 
     
            for ( i = 0 ; i < list.length; i++)
            {
            list[i] = scan.nextLine();
            }
     
            return list[i];
     
        }


    (On this line)return list[i];

    java.lang.ArrayIndexOutofBoundsExcepetion

    which mean my return value is returning a value of bounds. But I don't know why its returning out bound when its clearly not out of bounds. The list array has 12 place orders for each string value. So why is it still giving me this error message? I'm doing something but I don't know what.

  23. #23
    Junior Member
    Join Date
    Mar 2013
    Posts
    13
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    When you use the [ ]operators you are asking for one element of the whole array, in your method at the end of the for loop i is equal to list.length (12) which terminates the for loop but then are attempting to reference the 12th element of your String array ie. A single String (not an entire array of them) which is causing OutOfBoundsException... Compare your for loop to curmudgeon's, you'll notice that after his has terminated calling store[i] would not be possible...
    Regardless of all this you're returning the wrong data type still...

  24. The Following User Says Thank You to Hammet27 For This Useful Post:

    Rain_Maker (March 24th, 2013)

  25. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,778
    Thanks
    54
    Thanked 2,341 Times in 2,312 Posts

    Default Re: Need help with creating method for Array.

    Consider how the for loop works. The i++ part increments i, the i < list.length part ends the loop when i gets too big.
    When the loop ends, i is past the end of the array.

    Why do you want the method to return an element in the array instead of the whole array?
    If you don't understand my answer, don't ignore it, ask a question.

  26. #25
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    125
    My Mood
    Nerdy
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with creating method for Array.

    Norm I want to return the whole array. I want the array to have all my string data.

Page 1 of 2 12 LastLast

Similar Threads

  1. help with creating a method and returning a value
    By kungfuthug in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 17th, 2013, 08:22 PM
  2. Creating method to change money?
    By j4ze in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 19th, 2012, 04:21 PM
  3. creating a new method that's not outputting correct result
    By orbin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 23rd, 2012, 01:12 AM
  4. Help Creating a method for my jellyBean gussing game
    By CookiesForRandy in forum What's Wrong With My Code?
    Replies: 11
    Last Post: November 20th, 2010, 09:29 AM
  5. Creating a scaleUp main method in a new class
    By Brainz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2010, 08:58 AM