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

Thread: Trouble with converting strings to integers, 2D arrays

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Trouble with converting strings to integers, 2D arrays

    Eventually I would like to write create a minimum spanning tree using Prim's algorithm. However, I am have having trouble with reading data from a csv text file and converting those strings into integers. Here is the input text file contents:

    A,B,C
    ∞,1,∞
    ∞,5,∞

    I can get my program to save these values into a 2D String array just fine. Now what I want to do is create two seperate arrays to represent this data. The first holds A,B,C and will be a 1D array. The second will hold:

    ∞,1,∞
    ∞,5,∞

    and will be a 2D array; however, and here is the caveat, I need to convert the string representations of 1 and 5 into integers and I need to convert the ∞ symbol into a large number, in this case 999999.

    Here is my attempt at doing so:

    public static void primsAlgorithm(String[][] array) {
     
            System.out.println();
            System.out.println("Finding the minimum spanning tree for the following weighted graph: ");
            System.out.println();
     
            String[] vertexArray = new String[array.length];
     
            int[][] adjMatrix = new int[array.length - 1][array.length];
     
     
            for (int i = 0; i < array.length; i++) {
     
                for (int j = 0; j < array[i].length; j++) {
     
                    vertexArray[j] = array[0][j];
     
                    if (array[i + 1][j].equals("\u221E")) {
     
                        adjMatrix[i][j] = 999999;      //if inf symbol is seen replace with high value weight
                    } else {
                        adjMatrix[i][j] = Integer.parseInt(array[i + 1][j]);
                    }
     
                    System.out.print("\t" + array[i][j]);
                }
                System.out.println();
            }
     
            System.out.println("Vertex array: ");
            System.out.println(Arrays.toString(vertexArray));
     
            System.out.println("adjMatrix: ");
            for (int i = 0; i < adjMatrix.length; i++) {
     
                for (int j = 0; j < adjMatrix[i].length; j++) {
     
                    System.out.print("\t" + adjMatrix[i][j]);
                }
                System.out.println();
            }
     
        }

    I have been messing with this trying different things for about an two hours now and I am lost. I don't know why it isn't doing what I want. Here is the log from an attempt at running the program:

    run:
    Please select the algorithm you wish to run: 1 for Prim's algorithm, 2 for Kruskal's algorithmor 3 for Floyd-Warshall’s algorithm
    1
    You chose Prim's algorithm.

    Finding the minimum spanning tree for the following weighted graph:

    A B C
    ∞ 1 ∞
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at graphalgorithms.GraphAlgorithms.primsAlgorithm(Gra phAlgorithms.java:104)
    at graphalgorithms.GraphAlgorithms.main(GraphAlgorith ms.java:36)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)

    Obviously, this has something to do with the sizing of my arrays but what? Any ideas as to what I am doing wrong? A more simple solution maybe? #headaches

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trouble with converting strings to integers, 2D arrays

    what I want to do is create two seperate arrays to represent this data. The first holds A,B,C and will be a 1D array. The second will hold:
    Are you saying that the first line of the input file should go into a 1D array.
    If the first row of the 2D array is what you want to separate out you could do it with an assignment statement:
    String[] savedFirstRow = array[0] ;// save the first row

    For the remaining lines, test contents
    for infinity and set high value
    or convert String to int and save

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at graphalgorithms.GraphAlgorithms.primsAlgorithm(Gra phAlgorithms.java:104)
    The code at line 104 used an index past the end of the array. The array has less than 4 elements.
    Check the logic to make sure it does not go past the end of the array.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with converting strings to integers, 2D arrays

    Quote Originally Posted by Norm View Post
    Are you saying that the first line of the input file should go into a 1D array.
    If the first row of the 2D array is what you want to separate out you could do it with an assignment statement:
    String[] savedFirstRow = array[0] ;// save the first row

    For the remaining lines, test contents
    for infinity and set high value
    or convert String to int and save


    The code at line 104 used an index past the end of the array. The array has less than 4 elements.
    Check the logic to make sure it does not go past the end of the array.
    Yes I haven't had too much trouble with separating row 0 data. The trouble is with saving the following rows into a separate array. Specifically, this is how I am attempting to ignore the first row but still collect data from the rest of the 2D array to save it into an alternate 2D array:

    if (array[i + 1][j].equals("\u221E")) {
     
                        adjMatrix[i][j] = 999999;      //if inf symbol is seen replace with high value weight
                    } else {
                        adjMatrix[i][j] = Integer.parseInt(array[i + 1][j]);
                    }

    As you can see I am going to get an index out of bounds because of the i+1 statements but how else can I ignore the first row?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trouble with converting strings to integers, 2D arrays

    Why the +1?
    If you have two arrays, one String and one int, is the logic supposed to convert the String value in the first array to an int value in the second array? The code would use the same indexes into the two arrays so that the int values corresponded with the String values.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with converting strings to integers, 2D arrays

    Quote Originally Posted by Norm View Post
    Why the +1?
    If you have two arrays, one String and one int, is the logic supposed to convert the String value in the first array to an int value in the second array? The code would use the same indexes into the two arrays so that the int values corresponded with the String values.
    So I guess what I am trying to avoid is this. Lets say I set up my code like:

    if(array[i][j] != array[0][j]){
     
                        if (array[i][j].equals("\u221E")) {
     
                            adjMatrix[i][j] = 999999;      //if inf symbol is seen replace with high value weight
                        } else {
                            adjMatrix[i][j] = Integer.parseInt(array[i][j]);
                        }
                    }

    Then when I run my output is:

    run:
    Please select the algorithm you wish to run: 1 for Prim's algorithm, 2 for Kruskal's algorithmor 3 for Floyd-Warshall’s algorithm
    1
    You chose Prim's algorithm.

    Finding the minimum spanning tree for the following weighted graph:

    A B C
    ∞ 1 ∞
    ∞ 5 ∞
    Vertex array:
    [A, B, C]
    adjMatrix:
    0 0 0
    999999 1 999999
    999999 5 999999

    Note that there is an extra row of zeros. So I thought that if I did i + 1 it would skip that first row.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trouble with converting strings to integers, 2D arrays

    Take a piece of paper and write some rows for the first array
    and another set of rows with one fewer row for the second array.
    Now work how to set the values of the indexes to start at the second row of the first/longer array and the first row of the second/shorter array
    and how to detect the end of the arrays. How many rows will be scanned? That will be the number of times to go around the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with converting strings to integers, 2D arrays

    Honestly, I thought that's what I was doing with my first condition statement namely:

    if(array[i][j] != array[0][j])

    That should say hey ignore the first row.

    Now if I set the second smaller array to be:

    int[][] adjMatrix = new int[array.length-1][array.length];

    That, in this case, will be a 2x3 matrix (the smaller matrix).

    So now when I try to place the remaining elements from the larger matrix which should also be 2x3 matrix (because I ignored the first row) into the smaller matrix I get an error. Index out of bounds, how... why? It doesn't make sense to me.

    If I say okay dont make the other array smaller ie:

    int[][] adjMatrix = new int[array.length][array.length];

    Then all of a sudden it works but it gives me 3 zeros in the first row. I dont know if I am just going crazy or what but I am super confused by this.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trouble with converting strings to integers, 2D arrays

    I get an error. Index out of bounds, how... why? It doesn't make sense to me.
    Ok simple case: array 1 has 3 rows, array 2 has 2 rows
    The loop needs to go around 2 times (size of array 2)
    indexes:
    array1 / array2
    1 (0+1) / 0
    2 (1+1) / 1
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with converting strings to integers, 2D arrays

    Quote Originally Posted by Norm View Post
    Ok simple case: array 1 has 3 rows, array 2 has 2 rows
    The loop needs to go around 2 times (size of array 2)
    indexes:
    array1 / array2
    1 (0+1) / 0
    2 (1+1) / 1
    Okay so instead of running the loop the size of the original array I need to run it for the size of the second array, is that what your saying? This worked but apparently my attempt at neglecting data from the first row of the larger matrix is not working because although I am no longer outputting a 3x3 its now a 2x3 but the first row is all zeros and the last row is getting chopped off.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trouble with converting strings to integers, 2D arrays

    need to run it for the size of the second array,
    Yes, that looks right.
    And the index for the rows of the larger array need +1 so they go from the second row.

    not working
    Please post the new code and the output it creates so we can see what is going on.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Converting primitive data types to read integers with decimal
    By SANA4SPA in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 14th, 2014, 01:09 AM
  2. Merge two arrays of integers
    By jajavava in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 22nd, 2013, 09:19 PM
  3. Replies: 6
    Last Post: June 18th, 2012, 04:54 AM
  4. Converting letter casing in strings
    By SeanEE89 in forum Java Theory & Questions
    Replies: 9
    Last Post: October 7th, 2011, 11:25 AM
  5. [SOLVED] Java program to convert and compare integers
    By luke in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 18th, 2009, 06:26 PM