Results 1 to 5 of 5
  1. #1
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Passing an array as a parameter?

    I am trying to make a class definition file for an ASCII File.
    Ultimately, I want to be able to add methods to allow the image produced by the file to be printed normally, then printed with various manipulations.
    However, for some reason, whenever I try to run the program to test my normalPrint method, it terminates without printing anything.
    I think this is because the array's values width and height are not within the scope of the method. I tried passing the array as a parameter for the method like so:
    Java Code:
    public void normalPrint(char [][] poop){
    //method here
    }
    but it gave me an error that stated
    "The method normalPrint(char[][]) in the type asciiFile is not applicable for the arguments ()"

    Class Definition:

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    public class asciiFile {
    int height;
    int width;
    Scanner input;
    char[][] poop;
    	public asciiFile(File f) throws FileNotFoundException{
    		input = new Scanner(f);
    		int height = input.nextInt();
    		int width = input.nextInt();
    		
    		poop = new char[width][height];
    	}
    
    	public void normalPrint(){
    
    	for (int i = 0; i < height; i++){
    		for(int j = 0; j < width ; j++){
    			int value = input.nextInt();
    			char value2 = (char)value;
    			poop[j][i] = value2;
    			System.out.print(poop[j][i]);
    		
    		}
    		System.out.println("");
    			}	
    		}
    }

    Client Program:
    Java Code:
    import java.util.*;
    import java.io.*;
    public class main {
    public static void main(String[] args)
    throws FileNotFoundException {
    	asciiFile derp = new asciiFile(new File("homer.txt"));
    	derp.normalPrint();
    	}
    
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Passing an array as a parameter?

    The redefined variables 'height' and 'width' are local to the constructor of the asciiFile constructor; make them class instance variables only so your other method 'normalPrint' knows about them; b.t.w. capitalize the name of your class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Passing an array as a parameter?

    That error means you changed the definition of the method to take a char[][], but you didn't change the call to pass a char[][].

    Java doesn't really have multidimensional arrays. It has arrays of arrays. So if you have a char[][] fnord, its length is fnord.length and its width is fnord[0].length. However, you can only be sure of that if you allocated it that way. It's entirely possible for the array to be ragged; that is, fnord[0] could be a different length than fnord[1]. Some of the elements in fnord might even be null.

    And read the link in my sig.
    Get in the habit of using standard Java naming conventions!

  4. #4
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: Passing an array as a parameter?

    Thank you. :) I figured out the problem; it was that I had the "int" modifier twice, so it was creating an entirely new variable instead of modifying the instance variable.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: Passing an array as a parameter?

    Redefining a variable can be a common mistake. If your IDE supports it, I suggest you enable
    warnings about variable shadowing or variable hiding. If you are doing CLI compilation then I
    don't have a good recommendation. I could not find a way for the javac compiler to issue a
    warning about this.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. passing date into parameter
    By tike in forum New To Java
    Replies: 5
    Last Post: 12-08-2011, 05:30 PM
  2. Switch Statement/Parameter Passing
    By spmooney@hotmail.co.uk in forum NetBeans
    Replies: 1
    Last Post: 01-06-2010, 01:50 PM
  3. passing parameter to a thread
    By adammyth in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-02-2010, 07:58 PM
  4. passing a parameter
    By aarthi2learn in forum AWT / Swing
    Replies: 4
    Last Post: 12-22-2008, 05:46 AM
  5. Passing short value as parameter
    By javanewbie83 in forum New To Java
    Replies: 16
    Last Post: 07-16-2008, 05:27 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
  •