aspose file tools*
The moose likes Beginning Java and the fly likes How to return two values back to the main from a for loop? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to return two values back to the main from a for loop?" Watch "How to return two values back to the main from a for loop?" New topic
Author

How to return two values back to the main from a for loop?

Ashley Kin
Ranch Hand

Joined: Oct 18, 2011
Posts: 36
Is that possible? I'm trying to return int weapon and int cWeapon back to the method, but they go back to the for loop first.
Thanks!

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

It's not clear to me where you are trying to do this...posting 100 lines of code with vague mention of the problem really isn't terribly helpful.

However, a method can only return one thing. an int, a String, a FuBar....

But think of it this way...If I can only hand you one thing, what's to say that one thing doesn't hold other things? For example, I could hand you an egg, or I could hand you an egg carton (both times, I give you one thing), but the egg carton may contain 12 (or more) eggs.

So you may be able to create an object that contains your weapon and cWeapon and return that.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
if i have understood your problem then,
one way to solve it can be to make weapon and cweapon an array, then when the for loop finishes you can retrieve all the values of these variables.
No need of the strange scenario you mentioned.
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

Nomaan Butt wrote:if i have understood your problem then,
one way to solve it can be to make weapon and cweapon an array, then when the for loop finishes you can retrieve all the values of these variables.
No need of the strange scenario you mentioned.


I do not see other way that the idea above.

Array object[]; this should do the work for you.


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Ashley Kin wrote:Is that possible? I'm trying to return int weapon and int cWeapon back to the method, but they go back to the for loop first.

Fred's quite right, but it seems to me that you're thinking about this game very procedurally:
  • get the player's weapon of choice (a character)
  • get the computer's choice (another character)
  • compare the two with a big 'if' statement
  • log the result (win/loss)

  • There's no doubt that you'll probably arrive at a solution, and it may well work in Java, but it won't be Object-Oriented (which presumably is why you're learning Java).
    The same solution could be written in C or COBOL or a whole other raft of languages that don't use objects or classes.

    You're getting too bogged down in the "how do I do this?"; what you need to think about more is why - because that is the essence to solving problems, in any language.

    One tip for you: Java has a nice little interface called Comparable which may be very helpful. Have a think about a class called Weapon that implements Comparable (so you can compare one Weapon to another).

    Alternatively, it has another nice little class called an 'enum', which allows you to create named constants, viz:(I deliberately haven't included any code - and you will need some to solve your problem properly).

    Either of these is likely to produce a much more "Java-like" solution than what you're currently trying for; and there are probably other ways too.

    The idea is to make objects do the work; not big reams of procedural code.

    Winston

    [EDIT] Having thought about it, the 'enum' solution is probably better, as your particular game is a highly unusual use of Comparable that violates transitivity. It will work for the purposes of the game, but not otherwise.


    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19785
        
      20

    An Object[] could do the trick but it means you'll need to cast the elements. A specialized object may be better. AWT does this as well, with classes like java.awt.Point, java.awt.Dimension and java.awt.Rectangle. They're not needed for parameters, because the separate parts can be used as well (in fact, many methods that take one of these objects are overloaded to also take the separate parts). But for returning poinrts / sizes / bounds they sure are useful.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11499
        
      16

    It's also hard to say what's going on, since we don't have all the code. Where is 'weapon' declared? I see a String declared in the userWeapon method, but I don't see how that can be in scope in your main method...

    Can you post a simple example that demonstrates your issue - one that will compile?
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to return two values back to the main from a for loop?