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: Filling an array with user input and a method

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Filling an array with user input and a method

    i need to create a program that asks the user for an array and then
    asks the user to fill in the slots of the array but i have to do it
    in a method can any 1 help i can post the question if it will help


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default re: Filling an array with user input and a method

    What have you tried? Where are you stuck?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default re: Filling an array with user input and a method

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please give your threads useful/meaningful titles. Title changed to be more useful. (Which messed up your link in the chat box.)

  4. #4
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Filling an array with user input and a method

    public class Ex6_02 
    {
     
        public static void main(String[] args) 
        {
            String ans = JOptionPane.showInputDialog("give me a length");
            int Lgth = Integer.parseInt(ans);
            int[] int1 = new int[Lgth];
            int [] arrint = createEmptyIntArray(int1);
            System.out.println(arrint);
            /*
            String [] arrstr = createEmptyStringArray();
     
     
            boolean [] arrboo = createEmptyBooleanArray();*/
            for (int i = 0; i < arrint.length; i++) {
                 System.out.println(int1[i]);
            }
            /*
            for (int i = 0; i < arrstr.length; i++) {
     
            }
     
            for (int i = 0; i < arrboo.length; i++) {
     
            }*/
     
        }
     
     
        /*    private static String[] createEmptyStringArray()
        {
     
     
     
        }
     
        private static boolean[] createEmptyBooleanArray()
        {
     
     
     
        }*/
     
        private static int[] createEmptyIntArray(int[] int1) 
        {
            String ans = JOptionPane.showInputDialog("give me a number");
            int Lgth = Integer.parseInt(ans);
            int[] arr2 = new int [int1.length];
     
            for (int i = 0; i < int1.length; i++)
            {
                if (int1.length == i)
                {
                arr2[i] = int1[i];
                }
     
            }
     
            return arr2;
     
     
     
     
        }

    thats what i go so farm but then i try some thing else it freaks out

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Filling an array with user input and a method

    it freaks out
    If there are error messages, copy the full text and paste it here. Plus the code that caused the error.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Filling an array with user input and a method

    ok yea i know that but what i need to do is make it so that it asks for a number for every open slot in the array and i cant think of how to do that

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Filling an array with user input and a method

    Use a for loop that goes through the array, when there is an open slot ask the user for a value to put in that slot, read the value and put it in the open slot.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Trying to validate a character user input in an array ?
    By lizzy2 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 20th, 2013, 03:04 PM
  2. How to add to an array with user input
    By miller4103 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 12th, 2013, 10:27 PM
  3. Asking for user input in the main method?
    By HeroProtagonist in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 19th, 2012, 07:00 AM
  4. Append data from user input into array
    By brncao in forum Collections and Generics
    Replies: 1
    Last Post: October 11th, 2011, 04:37 AM
  5. [SOLVED] Help with getting user input for my factorial method
    By u-will-neva-no in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 4th, 2011, 01:24 PM

Tags for this Thread