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: Array Issue

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Post Array Issue

    I need some assistance with debugging my code.

    Here are my objectives for this program:
    -Randomly input 20 numbers from 1 to 10 into a 2D array of 5 rows and 4 columns
    -Output: the 2D array and the sum of each row

    Here is my code:

    import java.util.Random;
     
    public class Project_3 {
     
    	Random r = new Random(); //For Generating a Random Number Later
     
    	int[] array = new int[5][4]; //Creates the Array
     
    	public static void main(String[] args) {
     
        	Project_3 p3 = new Project_3();
        		p3.run();
        }
     
        public void run() { 
        	Project_3 p3 = new Project_3();
        		p3.randomizeValues();
        		p3.printResults();
        		p3.sumRows();
        }
     
        public int randomizeValues() {
        	int rows = 5;
        	int columns = 4;
     
        	int i,j; //Temp Variables for Rows/Columns respectively
     
        	for(i = 0; i < rows; i++) {
        		for(j = 0; j < columns; j++) {
        			array[i][j] = r.nextInt(11);
        		}
        	}
        }
     
        public int printResults() {
        	int rows = 5;
        	int columns = 4;
     
        	int i,j; //Temp Variables for Rows/Columns respectively
     
        	for(i = 0; i < rows; i++) {
        		for(j = 0; j < columns; j++) {
        			System.out.println(array[i][j] + " ");
        		}
        		System.out.println(" ");
       		}
        }
        public int sumRows() {
        	int sum = 0;
     
        	for(int col = 0; col < array[row].length; col++) {
        		sum = (sum + array[row][col]);
        	}
        	System.out.println("The Sum for row: "+row+"is "+sum);
        }
    }

    Here are my error messages:
    --------------------Configuration: Project 3 - JDK version 1.7.0_03 <Default> - <Default>--------------------
    C:\Users\Name\Documents\JCreator Pro\MyProjects\Project 3\src\Project_3.java:13: error: incompatible types
    int[] array = new int[5][4]; //Creates the Array
    ^
    required: int[]
    found: int[][]
    C:\Users\Name\Documents\JCreator Pro\MyProjects\Project 3\src\Project_3.java:36: error: array required, but int found
    array[i][j] = r.nextInt(11);
    ^
    C:\Users\Name\Documents\JCreator Pro\MyProjects\Project 3\src\Project_3.java:49: error: array required, but int found
    System.out.println(array[i][j] + " ");
    ^
    C:\Users\Name\Documents\JCreator Pro\MyProjects\Project 3\src\Project_3.java:57: error: cannot find symbol
    for(int col = 0; col < array[row].length; col++) {
    ^
    symbol: variable row
    location: class Project_3
    C:\Users\Name\Documents\JCreator Pro\MyProjects\Project 3\src\Project_3.java:57: error: int cannot be dereferenced
    for(int col = 0; col < array[row].length; col++) {
    ^
    C:\Users\Name\Documents\JCreator Pro\MyProjects\Project 3\src\Project_3.java:58: error: cannot find symbol
    sum = (sum + array[row][col]);
    ^
    symbol: variable row
    location: class Project_3
    C:\Users\Name\Documents\JCreator Pro\MyProjects\Project 3\src\Project_3.java:58: error: array required, but int found
    sum = (sum + array[row][col]);
    ^
    C:\Users\Name\Documents\JCreator Pro\MyProjects\Project 3\src\Project_3.java:60: error: cannot find symbol
    System.out.println("The Sum for row: "+row+"is "+sum);
    ^
    symbol: variable row
    location: class Project_3
    8 errors

    Process completed.
    I believe that it may stem from the initial declaration of the array, but I am stumped. Any help would be much appreciated!


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Array Issue

    When I see many errors like this, I have to first comment that your style of coding seems to be off. If you can't use an IDE such as Eclipse, then it is incumbent upon you to compile your code early and often, and most importantly to not add any new code until your current code is free of errors. If not, you risk ending up with a rat's nest of errors.

    Let's look at your first error message:
    C:\Users\Name\Documents\JCreator Pro\MyProjects\Project 3\src\Project_3.java:13: error: incompatible types
    int[] array = new int[5][4]; //Creates the Array
    ^
    required: int[]
    found: int[][]

    The message is telling you exactly what is wrong. You're declaring your array variable, array to be a single dimensional int array, int[], but then you're assigning a 2-dimensional int array to it, int[5][4]. If you want it to hold a 2-dimensional array, you will have to declare it as such:

    int[][] array = new int[5][4];

    See the difference?

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Array Issue

    I'm very new to arrays, which is why I made a mistake like this, thanks for the help!

    After fixing that, these are the errors that are left:
    --------------------Configuration: Project 3 - JDK version 1.7.0_03 <Default> - <Default>--------------------
    C:\Users\Name\Documents\JCreator Pro\MyProjects\Project 3\src\Project_3.java:57: error: cannot find symbol
    for(int col = 0; col < array[row].length; col++) {
    ^
    symbol: variable row
    location: class Project_3
    C:\Users\Name\Documents\JCreator Pro\MyProjects\Project 3\src\Project_3.java:58: error: cannot find symbol
    sum = (sum + array[row][col]);
    ^
    symbol: variable row
    location: class Project_3
    C:\Users\Name\Documents\JCreator Pro\MyProjects\Project 3\src\Project_3.java:60: error: cannot find symbol
    System.out.println("The Sum for row: "+row+"is "+sum);
    ^
    symbol: variable row
    location: class Project_3
    3 errors

    Process completed.
    I don't understand these errors. I thought that since those variables are defined in that method, that it would just take those variables. Is a line of code missing somewhere?

    --- Update ---

    I was able to figure it out. Here is the finished code:

     
    import java.util.Random;
     
    public class Project_3 {
     
    	Random r = new Random(); //For Generating a Random Number Later
     
    	int[][] array = new int[5][4]; //Creates the Array
     
    	public static void main(String[] args) {
     
        	Project_3 p3 = new Project_3();
        		p3.run();
        }
     
        public void run() { 
        	Project_3 p3 = new Project_3();
        		p3.randomizeValues();
        		p3.printResults();
        		p3.sumRows();
        }
     
        public int randomizeValues() {
        	int rows = 5;
        	int columns = 4;
     
        	int i,j; //Temp Variables for Rows/Columns respectively
     
        	for(i = 0; i < rows; i++) {
        		for(j = 0; j < columns; j++) {
        			array[i][j] = r.nextInt(11);
        		}
        	}
        	return columns;
        }
     
        public int printResults() {
        	int rows = 5;
        	int columns = 4;
     
        	int i,j; //Temp Variables for Rows/Columns respectively
     
        	for(i = 0; i < rows; i++) {
        		for(j = 0; j < columns; j++) {
        			System.out.println(array[i][j] + " ");
        		}
        		System.out.println(" ");
       		}
       		return rows;
        }
        public int sumRows() {
        	int sum = 0;
        	for(int row = 0; row < array.length; row++) {
     
        	for(int col = 0; col < array[row].length; col++) {
        		sum = (sum + array[row][col]);
        	}
     
        	System.out.println("The Sum for row: "+row+"is "+sum);
        }
        return sum;
    }
    }

    With a result of:
    1
    6
    6
    8

    1
    4
    6
    4

    1
    4
    6
    4

    7
    2
    8
    1

    7
    3
    3
    7

    The Sum for row: 0is 21
    The Sum for row: 1is 36
    The Sum for row: 2is 51
    The Sum for row: 3is 69
    The Sum for row: 4is 89
    I'm going to tweak the output of the array, but this is a stable, working version.

Similar Threads

  1. frustrating loop/array issue
    By knightsb78 in forum Loops & Control Statements
    Replies: 6
    Last Post: August 11th, 2012, 05:47 PM
  2. LAST issue! Random permutation array!
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 5th, 2012, 11:55 AM
  3. Java Issue / Cache issue
    By VisualPK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2012, 08:43 PM
  4. [SOLVED] Issue when returning an array
    By Broxxar in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 21st, 2012, 10:19 PM
  5. Array issue
    By Mini83 in forum Collections and Generics
    Replies: 5
    Last Post: August 18th, 2011, 09:18 AM

Tags for this Thread