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

Thread: array problem

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default array problem

    hello,
    i am new to java and im trying to solve some problems i found on the net
    my objective is to read a file that contain:
    "5
    3: 5 8 3
    2: 2 99
    7: 5 3 7 3 3 66 88
    2: 2 4
    4: 2 5 7 3"

    the 5 is the number of arrays
    the numbers 3, 2, 7, 2, 4 are the number of elements in each of their respective arrays
    ie: array number 1 has a size of 3 and contain 5 8 3

    i need to write a code that takes first the number as to create 5 arrays, and then to put in each array their respective elements

    in other words my goal is to have 5 arrays with each array their elements
    i did it assuming these numbers were fixed, i assigned each array their element but i want a way to do it if to say i want to do it for another file, that has the same format, so this file in here is not a constant

    i tried to make it by using 2d array but i messed up big time
    so if someone can help me with it i would appreciate it
    also it would be great if 2d array was used as i want to get used to it

    thanks in advance


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: array problem

    post some code of the 2d array you attempted and some code of what you have now. It is hard to help you if we do not know how much you have already done.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: array problem

    import java.util.Arrays;
     
    public class testing
    {
        public static void main (String[] args)
        {
            int[] list1 = {5, 8, 3};
            int[] list2 = {2, 99};
            int[] list3 = {5, 3, 7, 3, 3, 66, 88};
            int[] list4 = {2, 4};
            int[] list5 = {2, 5, 7, 3};
     
            int[] listN = new int [list1.length + list2.length + list3.length + list4.length + list5.length];
     
            for ( int i = 0; i < list1.length; i++)
            {
                listN [i] = list1 [i];
            }
            for ( int i = 0; i < list2.length; i++)
            {
                listN [list1.length + i] = list2 [i];
            }
            for ( int i = 0; i < list3.length; i++)
            {
                listN [list1.length + list2.length + i] = list3 [i];
            }
            for ( int i = 0; i < list4.length; i++)
            {
                listN [list1.length + list2.length + list3.length + i] = list4 [i];
            }
            for ( int i = 0; i < list5.length; i++)
            {
                listN [list1.length + list2.length + list3.length + list4.length + i] = list5 [i];
            }
     
            Arrays.sort(listN);
            System.out.println(Arrays.toString(listN));
        }
    }

    this was when i first tested to prove my logic, and it worked

    and this is when i want it as i mentioned above

    import java.util.*;
    import java.io.*;
    import java.util.Arrays;
     
    public class Problem3
    {
        public static void createArrays (String[] filename) throws FileNotFoundException
        {
             Scanner input = new Scanner (new File (filename[0]));
     
             int N = input.nextInt();
             int arrays [][] = new int [N][];
     
             for ( int i = 0; i < N; i++)
             {
                 arrays [N][i] = new []
                 input.nextLine();
             }
        }
    }

    now as you can see inside the for loop i want the length to be read from the file that i will specify in the parameter

    "arrays [N][i] = new []" i tried to out "input.nextInt()" inside the two empty brackets here but it didnt work, i figured it it did work, i would have it inside and then write "input.nextLine()" to go on to the next line and take the int again and create the length of the second array

    hope this helps
    Last edited by helloworld922; December 18th, 2010 at 10:56 AM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: array problem

    When you want to initialize an array with values, you must use this syntax:
    int[] myArray = new int[] {1, 2, 3}; // initialize a length 3 array with values 1, 2, 3

    However, if you're reading the values from a file, it's better to initialize the array to all 0 and the correct length because you have no idea what those values may be.

    // somehow you got an int variable called length from the file. I suggest using a Scanner object.
    int[] myArray = new int[length];
    Then you can populate the array with the indexing method.
    myArray[0] = 1; // obviously you would replace 1 with a value you read from the array
    myArray[myArray.length - 1] = 2; // the last element in the array

  5. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: array problem

    well but the problem is that i need to write the elements of an array from the file itself, no me assigning each element with its number
    i want it to read the file and put it itself

Similar Threads

  1. Problem with array
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 27th, 2010, 12:17 PM
  2. [SOLVED] Array Problem
    By Cammack in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 5th, 2010, 06:11 PM
  3. [SOLVED] Array Problem
    By KevinGreen in forum Object Oriented Programming
    Replies: 2
    Last Post: January 24th, 2010, 09:07 PM
  4. Problem with 2d array
    By Anyone in forum Collections and Generics
    Replies: 2
    Last Post: November 14th, 2009, 09:32 PM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM