Announcement

Collapse
No announcement yet.

Any1 know java code??/

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Any1 know java code??/

    Hi guys my problem is pretty much as follows...

    the following is happening when i try access the code, the first part is good because without having the handbag in inventory i want it to say you are carrying however if i have the handbag i want it to say you are carrying these items in your handbag but the below happens;

    What now?
    get
    torch ok

    What now?
    list
    You are carrying: torch

    What now?
    go
    stairs It's dark in here!

    What now?
    get
    handbag ok

    What now?
    list
    You are carrying these items in your handbag:
    torch
    You are carrying these items in your handbag:
    wallet
    You are carrying these items in your handbag:
    keys
    You are carrying these items in your handbag:
    ring
    You are carrying these items in your handbag:
    USB
    You are carrying these items in your handbag:
    mobile
    You are carrying these items in your handbag:
    handbag
    and here is the code

    Code:
    public void listWhatYouHave()
    {
        for (int i = 0; i < 7; i++)
        {
            if (hasItem[6])
            {
               System.out.println("You are carrying these items in your handbag:");
               switch (i)
               {
                    case 0: 
                        System.out.println("torch");
                        break;
                    case 1:
                        System.out.println("wallet");
                        break;
                    case 2:
                        System.out.println("keys");
                        break;
                    case 3:
                        System.out.println("ring");
                        break;
                    case 4:
                        System.out.println("USB");
                        break;
                    case 5:
                        System.out.println("mobile");
                        break;
                    case 6:
                        System.out.println("handbag");
                        break;
                    default: 
                        System.out.println("invalid item!");
                        break;
               }
            }
            else if (hasItem[i])
            {
                System.out.println("You are carrying:");
                switch (i)
                {
                    case 0: 
                        System.out.println("torch");
                        break;
                    case 1:
                        System.out.println("wallet");
                        break;
                    case 2:
                        System.out.println("keys");
                        break;
                    case 3:
                        System.out.println("ring");
                        break;
                    case 4:
                        System.out.println("USB");
                        break;
                    case 5:
                        System.out.println("mobile");
                        break;
                    case 6:
                        System.out.println("handbag");
                        break;
                    default: 
                        System.out.println("invalid item!");
                        break;
                }
            }
        }
    can you help... thanks (this is java obviously)

    sorry for being vague... pretty much when i dont have the handbag i want it to list what im carrying by saying 'you are carrying' however if i pickup the handbag at a point when thats picked up... i want it to say 'you are carrying these items in your handbag' but currently it prints out you are carrying just once... but you are carrying these items in your handbag is printed in everyline... i only want it once.

  • #2
    Code:
    if (hasItem[6])
    
    and
    
    if (hasItem[i])
    is a true/false statement.

    I'm not exactly sure what you are attempting to do, but if that variable exists and is not null, then the statement will be true (and the if statement's entry condition will be true, triggering its code).

    i.e., I suspect what you THINK that statement does, and what it is actually doing are 2 different things?


    Looks like you're trying to modify code for a MUD or something?



    edit:
    I'm not a JAVA guy per se, but i know how to write code in a few different languages.
    “Crashing is shit for you, shit for the bike, shit for the mechanics and shit for the set-up,” Checa told me a while back. “It’s a signal that you are heading in the wrong direction. You want to win but crashing is the opposite. It’s like being in France when you want to go to England and when you crash you go to Spain. That way you’ll never get to England!” -- Carlos Checa

    Comment


    • #3
      The " System.out.println("You are carrying these items in your handbag:"); " is inside your loop, so for every iteration of the loop it prints.

      My Java is rusty but try moving:

      if (hasItem[6])
      {
      System.out.println("You are carrying these items in your handbag:");

      Outside of the loop. That should only print once then.

      - - - Updated - - -

      Actually, even that won't work entirely as you want it to I don't think.

      Sit down and write the logic out on paper before you start coding...

      Comment


      • #4
        ^ probably it, didn't go through debugging it much.

        the control structure with 2 IFs basically doing the same thing within a counter loop just looks bizzare.


        I second what BenG said, this isn't a java issue, its a program logic problem.
        “Crashing is shit for you, shit for the bike, shit for the mechanics and shit for the set-up,” Checa told me a while back. “It’s a signal that you are heading in the wrong direction. You want to win but crashing is the opposite. It’s like being in France when you want to go to England and when you crash you go to Spain. That way you’ll never get to England!” -- Carlos Checa

        Comment


        • #5
          One if problem is if you have the handbag (item) in your possession and the other if is if you don't have the handbag... so im just trying to make it so when you have the handbag in your possession it will say your carrying in your handbag .. blah blah blah... the if else statement works for it... but the if part doesnt...

          Comment


          • #6
            I suspect you're going to need to re-structure that code (well, to do it cleanly without it being an extremely ugly hack job) - you're trying to hack in support for a container in some other code that was never set up to deal with containers, yes?

            Without knowing what all of your data types are, that's not really something we can advise you on. I suspect you'll need to create a new data type (class in java speak) for "handbag" (or even better, container, and have handbag a subclass of container, so it is easily extendable to backpacks, etc) and within that data structure have an array for its contents.


            Then you could do something like (psuedocode):
            • for all items in inventory
            • print item name
              • if item type = container
              • for all items in container
              • print item name



            edit:
            Also, java is object oriented. like i said, i haven't done much with JAVA itself, but this sort of problem lends itself to object use (I have done a bit with Objective-C and O.O. Pascal way back).

            You could even make "inventory" as subclass of container, and have a method for all objects of that class called "listcontents". And recursively use it, so you'd do something like

            inventory.listcontents

            and listcontents is a method which goes through each object inside the container, prints it's name and calls listcontents. this way you could have containers within containers and they would recursively list their contents using the same method code.

            More work to set up, but then you can deal with backpacks, purses, handbags, briefcases, sea containers, etc, etc. with minimal future changes to the code. With any arbitrary number of levels of containment (e.g., purse within a handbag within a backpack within a suitcase will "just work"). Because you just know if someone else is asking you to code this, you'll get handbags working and then they'll want some other type of container object, and they'll want to put something in something else

            I'd find a good Java / Object Oriented programming book and figure out how to structure your code properly. You'll have a far better time trying to maintain the code in future.


            Unless of course this is just a 5 minute hack job type project and you're not planning to ever do much code in future....
            Last edited by thro; 19-10-2013, 08:00 PM.
            “Crashing is shit for you, shit for the bike, shit for the mechanics and shit for the set-up,” Checa told me a while back. “It’s a signal that you are heading in the wrong direction. You want to win but crashing is the opposite. It’s like being in France when you want to go to England and when you crash you go to Spain. That way you’ll never get to England!” -- Carlos Checa

            Comment


            • #7
              *disclaimer- my Java experience is minimal and rusty, so appologies if anything I write is C specific... but I'm sure you can sort out any minor issues if it doesnt compile!



              Depending on your overall plan for the game/program; To make things simpler, I'd have the handbag as separate variable, or atleast enumerate it as item 0.

              Then you can do a check on that item first, simplify your logic, and write something like :


              Code:
              ...
              
              public void listWhatYouHave()
              { 
              
                System.out.print("You are carrying");        //note, not println  ...we are continuing
               if (hasitem[0])  
                 System.out.println(" in your handbag:");
                else 
                  System.out.println(":");
              
                for (int i=1; i<(MaxItems-1); i++)
                {
                 if (hasItem[i])
                    System.out.println(ItemDescription[i]); 
                }
              }



              ....Where ItemDescription is a static array defined at the start of your code as something like

              Code:
               string[] ItemDescription = new string[] {"Handbag", "purse", "torch" ...}


              and define/initialise initialise
              MaxItems by:

              Code:
               int MaxItems = sizeof( ItemDescription );         //I think this works in Java...:)    
                   //Double check the answer by printing it if you don't use an IDE debugger yet! It should equal the number of total possible items you could ever obtain!



              bit simpler?
              Hope that all makes sense!

              Last edited by xphread; 21-10-2013, 12:50 PM.

              Comment


              • #8
                As mentioned, this is a perfect example of an application suited to object oriented code but I ain't doing your assignment for you. Go read some stuff.

                Your specific issue can be cleaned up by getting your loop nesting right:

                Code:
                public void listWhatYouHave()
                {
                    if (hasItem[6]) {
                       System.out.println("You are carrying these items in your handbag:");
                    } else {
                       System.out.println("You are carrying:");
                    }
                       for (int i = 0; i < 6; i++)
                       {
                           if (hasItem[i]) {
                           switch (i)
                           {
                                case 0: 
                                    System.out.println("torch");
                                    break;
                                case 1:
                                    System.out.println("wallet");
                                    break;
                                case 2:
                                    System.out.println("keys");
                                    break;
                                case 3:
                                    System.out.println("ring");
                                    break;
                                case 4:
                                    System.out.println("USB");
                                    break;
                                case 5:
                                    System.out.println("mobile");
                                    break;
                                case 6:
                                    System.out.println("handbag");
                                    break;
                                default: 
                                    System.out.println("invalid item!");
                                    break;
                           }
                         }
                        }
                    }
                "Once upon a time we would obey in public, but in private we would be cynical; today, we announce cynicism, but in private we obey."

                Comment


                • #9
                  What you want to do is move your print line statements outside of the for loop. Test for what item is being carried if any, and print a line based on that.

                  if (hasItem[6]) {
                  System.out.println("You are carrying these items in your handbag:");
                  } else if (hasItem.length != 0) {
                  System.out.println("You are carrying:");
                  } else {
                  System.out.println("You aren't carrying anything.");
                  }

                  Comment

                  Working...
                  X