File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes printing and returning arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "printing and returning arrays" Watch "printing and returning arrays" New topic
Author

printing and returning arrays

pawel hiro
Greenhorn

Joined: Aug 25, 2010
Posts: 14
hi I'm very new to Java world. I have task where I define class CandyMachine and create five flavors of candy. I did that and those flavors I sign to Array of Strings then I create constructor(without arguments- part of the task) and now I have to print those flavors(which I have the problem with -[Ljava.lang.String;@9304b1) and then I have to create method which will return those flavors as an Array. Please help.
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi Pawel,

How do you reference the individual flavours in your array when you want to print them?

Do you do something like this:


Or if your flavours are instances of a Flavour class it is easier if you override the method toString()
in the Flavour class. Have a look at this java practice

Have a look at printing arrays as well if you want.


"Eppur si muove!"
pawel hiro
Greenhorn

Joined: Aug 25, 2010
Posts: 14
hi Gian and thank you for an advice, I solved one problem, at least I think I solved any how that's what i wrote


I hope I did it in the right way but I still have to return it as an array- is printing those flavors on the screen equal to returning it?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

No. Java methods have return values, for example:

The above method returns an object of the type String when called. Your method will need to return your array.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
pawel hiro
Greenhorn

Joined: Aug 25, 2010
Posts: 14
Sorry for asking stupid question but what's those [b], [\b] means?
pawel hiro
Greenhorn

Joined: Aug 25, 2010
Posts: 14
never mind I got it!
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

pawel hiro wrote:Sorry for asking stupid question but what's those , means?


My attempt to highlight the pertinent bits of the code - I forgot they wouldn't behave like they normally do (e.g. like this) inside code tags!
pawel hiro
Greenhorn

Joined: Aug 25, 2010
Posts: 14
going back to yesterday. I have this class and now I want to print elements of my Array as a list, one element in one line. Do I do it right? and why is not printing anything? or should I do it differently? I'm guessing it has to be a way. If I have let's say 60 elements I would not write 60 lines System.out.println. right?

public class CandyMachine {
String[] kindsOfCandies = {"czekolada", "mars", "snikers", "payday", "princpolo"};

public CandyMachine(){

}

public void printCandies(){
System.out.println(" Kinds of candy: ");
System.out.println("\t1." + kindsOfCandies[0]);
System.out.println("\t2." + kindsOfCandies[1]);
System.out.println("\t3." + kindsOfCandies[2]);
System.out.println("\t4." + kindsOfCandies[3]);
System.out.println("\t5." + kindsOfCandies[4]);

}
public String[] GetCandies() {
return kindsOfCandies;

}

}
pawel hiro
Greenhorn

Joined: Aug 25, 2010
Posts: 14
when I put System.out.println() in constructor method it does what I'm asking program to do. But when it's in his own method it doesn't want to print anything. Why is that? Is something wrong with my method?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

Do you actually call that method?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
pawel hiro
Greenhorn

Joined: Aug 25, 2010
Posts: 14
I guess It's not a method then . Sorry I'm new to that. Just a hint of what I can do. and If I can write some method which would substitute all those System.out.println with one. Thank you
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You are probably looking for some sort of loop to do this. There are a bunch in the Java language, the Java Tutorial should get you going.
pawel hiro
Greenhorn

Joined: Aug 25, 2010
Posts: 14
Well thank you for help and guidance. So I created a loop which pints on the screen numbers from one to 5 and now I want to assign arguments form my array. Do I need a separate method or do I have to connected them together? how do I get involved my loop with my array? So it can print array as a list?


import java.util.Arrays;


public class CandyMachine {

public String[] kindsOfCandies = {"czekolada", "mars", "snikers", "payday", "princpolo"};

public static void main(String[] args) {
System.out.println(" Kinds of Candy: ");
for(int i = 1; i < 6 ; i++)
System.out.println("\t"+i +".");

}
}
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

To print the values of the array, all you have to do is add kindsOfCandies[i] to your System.out.println() inside the loop, so that you print every element in order.
In java, by default array index starts from 0. So, inside your loop, kindsOfCandies[1] actually gives 'mars' and not 'czekoloda'. Do something accordingly to print the values you want.

OCPJP 6
Kacper Jonczyk
Greenhorn

Joined: Aug 25, 2010
Posts: 9
pawel hiro wrote:Well thank you for help and guidance. So I created a loop which pints on the screen numbers from one to 5 and now I want to assign arguments form my array. Do I need a separate method or do I have to connected them together? how do I get involved my loop with my array? So it can print array as a list?


Hi Pawel,

like guys mentioned above You have to remember that array index in Java begins from 0 and goes up to Array length minus 1.

To print content of Your kindsOfCandies, you use kindsOfCandies[i] as a println argument. Since main is static You have to make kindsOfCandies static as well. Because static method can not access non-static content.

I made these tweaks in Your Code for You. Try to write a method that will handle printing instead of using that code in main. It'll look better, and will be easier to use and maintain later.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
Welcome to the Ranch

I can suggest at least three possible improvements:
  • 1: Add {} and indentation to the body of the for loop.
  • 2: Use the .length field of the array instead of 5. That will still work if the size of array is changed.
  • 3: Replace the for loop by a for-each loop
  • Joanne Neal
    Rancher

    Joined: Aug 05, 2005
    Posts: 3646
        
      15
    Campbell Ritchie wrote:3: Replace the for loop by a for-each loop


    He won't be able to number each line using a for-each loop.


    Joanne
    Vinoth Kumar Kannan
    Ranch Hand

    Joined: Aug 19, 2009
    Posts: 276

    Joanne Neal wrote:
    Campbell Ritchie wrote:3: Replace the for loop by a for-each loop


    He won't be able to number each line using a for-each loop.

    Some work around can solve that too..
    variable i can be declared out the loop and incremented within.
    Joanne Neal
    Rancher

    Joined: Aug 05, 2005
    Posts: 3646
        
      15
    Vinoth Kumar Kannan wrote:
    Joanne Neal wrote:
    Campbell Ritchie wrote:3: Replace the for loop by a for-each loop


    He won't be able to number each line using a for-each loop.

    Some work around can solve that too..
    variable i can be declared out the loop and incremented within.


    True. But Campbell was suggesting improvements to the code and I don't think that would be an improvement.
    Vinoth Kumar Kannan
    Ranch Hand

    Joined: Aug 19, 2009
    Posts: 276

    Yeah..true! a normal for loop would do the very same thing, then.
    Actually I was about to edit my post and include that comment.. I was refreshing the page for any new comments and ahhh..there comes your post..
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39408
        
      28
    Trust you to notice, Joanne.
    Kacper Jonczyk
    Greenhorn

    Joined: Aug 25, 2010
    Posts: 9
    Thanks for a warm welcome Campbell!

    I've added indents and brackets to the code. Certainly use kindsOfCandies.length() is a way to do it, but since pawel was trying to get it running, i did only essential improvements.

    Regarding 'for-each' i'd have to agree with Joanne, plain old 'for' is doing just fine in this example.
    pawel hiro
    Greenhorn

    Joined: Aug 25, 2010
    Posts: 14
    you guys rock!!

    I'm trying implement my loop in the method all along but writing proper method (instead of using main frame) makes difficult for me. I've been trying read on method and figure out this problem myself. Unfortunately were not able to come up with solution. I have to write a method which will print this array on the screen and than will have to write a method which will return this array.

    Anyhow I going back to work and research on writing those methods. If possible, I would be happy to listen to hints and your advisory. And big thanks again. You guys make difference.
    pawel hiro
    Greenhorn

    Joined: Aug 25, 2010
    Posts: 14
    hi everybody I have a question I have this class





    and no matter what I do I can't make my method to print list of array;

    That's my main :


    Can anyone tell me what's wrong with my program? Why is he not printing things I ask him to print?
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19719
        
      20

    Please UseCodeTags. You can edit your post to add them.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39408
        
      28
    You have not called the method which prints out the array.
    Marimuthu Madasamy
    Ranch Hand

    Joined: Jun 07, 2007
    Posts: 72

    In your main() method, you have just created an object of your class. The behavior you want is in a method which is not called. So you need to call your method.


    - Marimuthu Madasamy
    pawel hiro
    Greenhorn

    Joined: Aug 25, 2010
    Posts: 14
    you guys rock!!! Thanks
    Shanky Sohar
    Ranch Hand

    Joined: Mar 17, 2010
    Posts: 1051

    lets have a llook at here also just for fun point of view(it is a tricky question anc can come in exam)


    SCJP6.0,My blog Ranchers from Delhi
    pawel hiro
    Greenhorn

    Joined: Aug 25, 2010
    Posts: 14
    hi fellows

    I have a question if I have
    String[] fruits=new String[]{"orange","mango","grapes","Banana","Apple"};

    and for example I'd like to start selling those fruits but I would like to keep count of all the fruits(e.g. I start with 100 each) how can I assign Int value to Strin[] ?
    Do I do it separately and create instance variable for each or is a way to create one to keep truck of all fruits?

    Thanks a lot ahead.

    Vinoth Kumar Kannan
    Ranch Hand

    Joined: Aug 19, 2009
    Posts: 276

    You can very well use a HashMap to store these kind of key-value pairs.
    pawel hiro
    Greenhorn

    Joined: Aug 25, 2010
    Posts: 14
    Hi again

    Do you guys have any other suggestion? I'm not familiar with hashMap yet so even after reading documentation I don't really know how to operate on that method.
     
    jQuery in Action, 2nd edition
     
    subject: printing and returning arrays