Results 1 to 5 of 5
  1. #1
    DMiller is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default Two simple array problems.

    Hi, I'm new to Java and was wondering if anyone could help me with these two array problems.

    1:

    I have made this code that works to create multiplication tables with for loops:
    Java Code:
    public class MultiplyTables
    {
       
     
    
    
        public MultiplyTables()
        {
    
        }
    
    
        public void printTable(int x, int y)
    
        {
            int [][] grid = null;
    	    grid = new int [x+1][y+1];
    
    	    for (int row = 1; row < grid.length; row++)
    	    {
    	        for (int col = 1; col < grid[row].length; col++)
    	        {
    	            grid[row][col] = row * col;
    	        }
    	    }
     	    for (int row = 1; row < grid.length; row++)
    	    {
    	        for (int col = 1; col < grid[row].length; col++)
    	        {
    	            System.out.print(grid[row][col] + " \t");
    	        }
    	        System.out.println();
    	    }	
        }
    }
    I'm trying to recreate this with while loops and have got the following which doesn't work:

    Java Code:
    public class MultiplyTables
    {
       
     
    
    
        public MultiplyTables()
        {
    
        }
    
    
        public void printTable(int x, int y)
    
        {
            int [][] grid = null;
            grid = new int [x+1][y+1];
           
            int row = 1;
            
            while (row < grid.length)
            {
                        int col = 1;
                while (col < grid[row].length)
                {
                    grid[row][col] = row * col;
                    col++;
                }  
            }
    
                    row++;
            
            
            int row1 = 0;
            while (row1 < grid.length)
            {
                        int col1 = 1;
                while (col1 < grid[row].length)
                {
                    System.out.print(grid[row1][col1] + " \t");
                    col1++;
                }   
                
                row++;
          
                
                System.out.println();
                
             
            } 
        }
    }
    Any ideas?

    2. I have stored cubed numbers in an array and am trying to call up numbers from that array in the class methods but I am getting Null pointer errors even when I specifically ask for a value that should be there.

    Java Code:
    public class StoredCubes
    {
        private int j = 0;
        int[] cubes;
        private int numberOfCubes = 0;
    
    
        public StoredCubes(int n)
        {
            numberOfCubes = n;
            int[] cubes = new int[numberOfCubes];
            for (int i=0; i<cubes.length; i++)
            {
                cubes[i] = j*j*j;
                j = (j+1);
            }
            System.out.println(cubes[3]);
        }
        
        public void printOneCube(int k)
        {
            
            
                System.out.println(cubes[3]);
            
        }
        
        public void printMCubes(int m)
        {
            int i=0;
            while(cubes[i] != m)
            {
                i++;
            }
            System.out.println(cubes[i]);
        }
    
    
    }
    Any ideas with this? I'm guessing I'm not initialising the array properly and the cubes aren't being stored. The printing of the third value in the constructor is just there to show that it does work initially but the same code later brings up an error.

    Thank you in advance.:confused:

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    in the first problem, the second while loop, you are testing on row1, but incrementing row

    while (row1 < grid.length)
    {
    ....
    row++;

  3. #3
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    for second problem, you are setting the number of cubes 0 (never setting it), and later trying to access the 3rd index
    private int numberOfCubes = 0;

    ....
    System.out.println(cubes[3]);
    when you try to access an array index out of the boundary of the array length, you would get an array index out of bounds exception.

    I don't get how you would get a null pointer exception though.

    also, for the get cubes, you should test the boundary length of the array. and maybe you meant the system.out.printlnof the cubes[i] to be inside the while loop ?

    Java Code:
    public void printMCubes(int m)
    {
            int i=0;
            while(i < cubes.length && cubes[i] != m)
            {
                System.out.println("cube[" + i + "]:" + cubes[i]);
                i++;
            }
    Last edited by travishein; 11-06-2009 at 02:43 AM.

  4. #4
    DMiller is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default

    Hi, Thank you for looking at my code.

    For the first problem:
    changing row to row1 doesn't change anything, it was just a typo. I think the problem is in how I have set out my while loops as the version with for loops works perfectly.

    For the Second:
    The problem is that I can't refer to the values in the array that were set in the constructor from a method.

    if I change the System.out.println to (cubes[i]) and put it underneath the j=j+1 line then it prints out the cubes fine. The problem is just when I try and get values from the array using a method, even just asking for cubes[2] when there are 10 values stored in it doesn't work.

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    Hi,
    One major flaw in ur code...U have initialized globally int[] cubes...again in constructor u have again declated int[] cubes supoose to be a local one restricted within constructor.So,whatever manipulations u are doing will not reach the int [] cubes declalared globally.

    Please got thru sun tutorial for basics and the d..
    Ramya:cool:

Similar Threads

  1. My Simple Array Does Not Work!
    By Simplev_v in forum New To Java
    Replies: 16
    Last Post: 09-07-2009, 03:43 PM
  2. Array problems!
    By Addez in forum New To Java
    Replies: 4
    Last Post: 08-29-2009, 07:56 PM
  3. Array problems..
    By smokeviolent in forum New To Java
    Replies: 1
    Last Post: 04-17-2009, 07:45 AM
  4. Simple array questions
    By jigglywiggly in forum New To Java
    Replies: 6
    Last Post: 02-15-2009, 06:57 AM
  5. Array problems
    By Hosticus in forum New To Java
    Replies: 2
    Last Post: 01-18-2009, 03:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •