Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

printing and returning arrays

 
pawel hiro
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 979
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pawel hiro
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pawel hiro
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for asking stupid question but what's those [b], [\b] means?
 
pawel hiro
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
never mind I got it!
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you actually call that method?
 
pawel hiro
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kacper Jonczyk
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47292
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Pie
    Posts: 3742
    16
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Vinoth Kumar Kannan
    Ranch Hand
    Posts: 276
    Chrome Java Netbeans IDE
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 3742
    16
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 276
    Chrome Java Netbeans IDE
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 47292
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Trust you to notice, Joanne.
     
    Kacper Jonczyk
    Greenhorn
    Posts: 9
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 14
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 14
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 20388
    46
    Chrome Eclipse IDE Java Windows
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please UseCodeTags. You can edit your post to add them.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 47292
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You have not called the method which prints out the array.
     
    Marimuthu Madasamy
    Ranch Hand
    Posts: 72
    Java Monad Scala
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    pawel hiro
    Greenhorn
    Posts: 14
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    you guys rock!!! Thanks
     
    Shanky Sohar
    Ranch Hand
    Posts: 1051
    Eclipse IDE Firefox Browser
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    lets have a llook at here also just for fun point of view(it is a tricky question anc can come in exam)
     
    pawel hiro
    Greenhorn
    Posts: 14
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 276
    Chrome Java Netbeans IDE
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can very well use a HashMap to store these kind of key-value pairs.
     
    pawel hiro
    Greenhorn
    Posts: 14
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic