wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Can I pass character array to new method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can I pass character array to new method?" Watch "Can I pass character array to new method?" New topic
Author

Can I pass character array to new method?

Harry Bowls
Greenhorn

Joined: Sep 21, 2011
Posts: 2
Hello all. This is my first post! I am trying to teach myself a little Java. I have no coding experince.

Anyway, is there a way to pass a character array to a new method? I am trying to create a "virtual 3d box." After the box has been created I want the user to enter a number which will change the box dimensions. (One Number will change both the height, width, and length). How do I pass my "this.box" character array to my growBox method? The character array are the dimensions of the box which I set as [1][1][1. After the user inserts the factor he wants to grow the box by I want to return the new dimensions to the main method. Your help would greatly be appreciated.

public class Box {

private char [][][] box;

public void main(String[] args) {
this.box= new char [1][1][1];
}

public int[][][] growBox(int height, int width, int length){
Scanner boxChange1 = new Scanner(System.in);
System.out.println("By which factor would you like to grow the box?");
int box = boxChange1.nextInt(); //Is this right?
return box
}
}
Sylvain Cloutier
Greenhorn

Joined: Sep 21, 2011
Posts: 1
Hello Harry,
First, I wonder why you use char array if you are going to store integer in it. If you want to have a 3D Box with different width, height and lenght, I would suggest you one thing. Think of you box as an object in the real world. You have you box with properties such as width, height, lenght, peharps color, etc. What you intend with that object, is to set and read those properties. I will not enter in all the details here, but I suggest you read about Object Oriented Programming with Java (OOP). You will find lots of documentation on the internet about it. But, since I wouln't post a reply to say: "Google it!" because I'm not a retard (I think), I will give you an example.



Once you created that object with all "getter" (that reads the properties of the object) and "setter" (that changes those properties), you will be able to have a real representation of your box. You can now, say, in you main function, create you object and modify it



If you want to discuss more about it, feel free to ask me clarifications about that. I know Object Oriented Programming might be hard to understand since there are lots of things to know, but trust me, it's powerfull!
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3594
    
  14

Welcome to CodeRanch, Sylvain and Harry! I hope you enjoy your stay here.

Harry, for your own convenience, I suggest you UseCodeTags. It will make your problem easier to understand, and posters will be more inclined to help you out.
Harry Bowls
Greenhorn

Joined: Sep 21, 2011
Posts: 2
Thank you Sylvain this is exactly what I needed help with! Here is what I have thus far. I created my box. Now I'm wondering on how to pass the dimension's to the grow method. Did I do it right? Please let me know. Also how can I return all three dimensions back to the class. Thank you!!!


fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11155
    
  16

by making the height, width and length member variables, you actually don't have to pass them in, or return them from, the method.

note that this is backwards:


it should be this:

or even

Also note that you should never have gotten this far. The code you posted won't compile. It has TONS of mistakes, so when you compile it for the first time, you are going to freak out.

NEVER write more than 2 or 3 lines before you compile.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I pass character array to new method?
 
Similar Threads
Help with Value Returning Methods
Testing classes
using Arrays to store info
Static Methods Problem
Static Methods Problem