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

Thread: Reading ints into a multidimensional array

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading ints into a multidimensional array

    My problem occurs during the for loops to read in the values from the file into my scores array. The program reads in and prints the first 6 values or 2 lines of ints, but then I get ArrayIndexOutOfBoundsException: 2

    I have no idea why this is stopping there. If I have j<1000, it will read 17 values. Anyway, the file I'm reading in is below (wasn't sure the formatting for a text file).

    Any help would be appreciated

    Andy Matt Tom
    3 2 3
    4 4 5
    3 3 2
    2 2 2
    2 4 2
    2 3 2
    4 4 5
    2 3 3
    4 3 5
    3 3 6
    2 2 5
    3 3 3
    3 3 2
    3 2 4
    3 2 6
    3 4 3
    2 3 2
    2 2 2
    50 52 62


    import java.io.*;
    import java.util.*;
     
    public class Calc
    {
     
        public static void main( String args[] )
        {
    		try
    		{
     
    		if (args.length<1)
    		{
    			System.out.printf("\n...You must enter the name of a file\n");
    			System.exit(0);
    		}
     
    		Scanner infile  = new Scanner ( new File(args[0]) );
     
     
    		int par= 3;
    		int play= 18;
    		String[] players= new String[play];
    		int k=0;
    		int scores[][]= new int[play-1][par-1];
     
    		while(infile.hasNext())
    		{
    		players[k]=infile.next();
     
    		k++;
     
    		if (k==play)
    		break;
    		}
     
     
    		for(int j=0; j<par; j++)
    		{
    			for (int i=0; i<play; i++)
    			{
    			scores[j][i]=infile.nextInt();
    			System.out.println(scores[j][i]);
    			}
     
    		}
     
    		}
    		catch (FileNotFoundException e)
    		{
    		System.out.println("Bug");
    		System.exit(0);
    		}
     
    	}
     
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Reading ints into a multidimensional array

    You could use System.out.println() to make sure you are reading what you expect and assigning that input to whatever you mean to assign it to.

    while(infile.hasNext())
    {
        players[k]=infile.next();
        System.out.println(players[k] + "read and assigned to players[k]");
     
        k++;
     
        if (k==play)
            break;
    }
    for(int j=0; j<par; j++)
    {
        for (int i=0; i<play; i++)
        {
            scores[j][i]=infile.nextInt();
            System.out.println(scores[j][i] + "read and assigned to scores[" + j + "][" + i + "]");
        }
     
    }

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading ints into a multidimensional array

    There's a different between the methods '.next()' and '.nextLine()'. "Next" only reads up until a space a line break occurs, next line keeps reading until the file has ended. What you wanna do is something like this:
    Scanner reader = new Scanner(new File(filename));
    while(reader.hasNextLine()){
    String line = reader.nextLine();
    if(!line.equals("")){
    //do whatever with that line
    }
    }

Similar Threads

  1. Binary Search for an array of random ints - Can't find the target value
    By mju516 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 30th, 2012, 11:25 AM
  2. [SOLVED] adding to a multidimensional array.
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 27th, 2011, 10:09 AM
  3. multidimensional array 'advancing'?
    By kitube in forum Java Theory & Questions
    Replies: 5
    Last Post: January 25th, 2011, 02:23 PM
  4. Need help in multidimensional array
    By Stefan_Lam in forum Algorithms & Recursion
    Replies: 3
    Last Post: January 14th, 2010, 08:52 PM
  5. Looping through a multidimensional array
    By MysticDeath in forum Loops & Control Statements
    Replies: 2
    Last Post: October 11th, 2009, 05:41 PM