Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Array List issues :P (advise is welcome) I'm new to java....  (Read 1353 times)
0 Members and 1 Guest are viewing this topic.
Offline gmmaster

Senior Newbie





« Posted 2012-02-22 04:04:25 »

Hello,

I am having problems with a method I'm working on to handle adding and removing objects from two array lists.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
public class Player {
   
   public static ArrayList<String> inventory = new ArrayList<String>();
   UserInput input = new UserInput();
   
   public void start() {
     
      if (UserInput.getInput("").equalsIgnoreCase("take book")) {
         if (Room1.room1.contains("book")) {
            inventory.add("book");
            Room1.room1.remove("book");
            getStatus();
            Room1.getStatus();
         }
      } else if (UserInput.getInput("").equalsIgnoreCase("drop book")) {
         if (inventory.contains("book")) {
            inventory.remove("book");
            Room1.room1.add("book");
            getStatus();
            Room1.getStatus();
         }
      } else if (UserInput.getInput("").equalsIgnoreCase("take paper")) {
         if (Room1.room1.contains("paper")) {
            inventory.add("paper");
            Room1.room1.remove("paper");
            getStatus();
            Room1.getStatus();
         }
      } else if (UserInput.getInput("").equalsIgnoreCase("drop paper")) {
         if (inventory.contains("paper")) {
            inventory.remove("paper");
            Room1.room1.add("paper");
            getStatus();
            Room1.getStatus();
         }
      }
      reStart();
   }


It (sorta) does what I want it to do....
But for for some reason I have to type for example "take book" like four times before it actually prints the outcome.
And for each input it seems to be random as to how many times I have to type it in before it actually prints it.

Just to clarify so you can help me better...
the reStart() method just calls the start() method again.
The getStatus() methods from both the Room1 class and Player class just print their equivalent Array list.

This is the GetInput() method...
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public static String getInput(String prompt) {
      BufferedReader stdin = new BufferedReader(
            new InputStreamReader(System.in));
     
      System.out.print(prompt);
      System.out.flush();
     
      try {
         return stdin.readLine();
      } catch (Exception e) {
         return "Error; " + e.getMessage();
      }
   }


If there is more info you need to help, let me know....

Also, If you have any suggestion on a better idea on how I should do this PLEASE let me know Smiley
I am very new to java coding and I am learning, this is a text adventure I am working on....

Thank you!

loopish's brother Cheesy
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #1 - Posted 2012-02-22 04:14:23 »

I don't see anything wrong with the handling of "take book". However, I see a HUGE bug: you are taking user input each time it steps through the if-else ladder. For example, you have to type "drop book" twice for it to work, "take paper" 3 times, and "drop paper" 4 times. You should store the input in a String variable and then test against that variable:
1  
2  
3  
4  
5  
6  
7  
8  
9  
String input = UserInput.getInput("");

if(input.equalsIgnoreCase("....")) {
    ....
}
else if(input.equalsIgnoreCase("....")) {
    ....
}
//etc....

Offline gmmaster

Senior Newbie





« Reply #2 - Posted 2012-02-22 17:46:02 »

huh...
when i do that it gives me this

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
Exception in thread "main" java.lang.StackOverflowError
   at java.util.ArrayList.contains(ArrayList.java:199)
   at org.watersedge.Player.start(Player.java:28)
   at org.watersedge.Player.reStart(Player.java:46)
   at org.watersedge.Player.start(Player.java:42)
   at org.watersedge.Player.reStart(Player.java:46)
   at org.watersedge.Player.start(Player.java:42)
   at org.watersedge.Player.reStart(Player.java:46)
   at org.watersedge.Player.start(Player.java:42)
   at org.watersedge.Player.reStart(Player.java:46)
   at org.watersedge.Player.start(Player.java:42)
   at org.watersedge.Player.reStart(Player.java:46)
   at org.watersedge.Player.start(Player.java:42)
   at org.watersedge.Player.reStart(Player.java:46)
       // this goes on for a long time......


I think the problem is that every time the start() method runs again,
after I assign what the input variable is... It just keeps on looping and
never stops because input = "take book" every time afterwards....
I have tried assigning input to null, input = null, just before the
reStart() method... But it didn't seem to do anything....

loopish's brother Cheesy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #3 - Posted 2012-02-22 18:06:37 »

Why are you calling reStart() if it just calls start? That creates an infinite loop Smiley

Could you show us the new code for all these methods?

Also, one tip, don't create a new BufferedReader object every time in getInput(String), make it an instance variable so you can re-access it and not waste resources. Also that may possibly fix your issue.

Offline UprightPath
« Reply #4 - Posted 2012-02-22 18:08:28 »

Blast! I took too much time and ra4king basically covered what I did, just using more basic terms. xD

First: Recursion is bad unless you have a reason for doing it, and I don't think you really do. What this means, is that it's a bad habit to have a method call another method that will call first. Especially if you have no way for them to break out of it (Not sure if you do, since reStart isn't displayed). In this case, you'll end up having a stack overflow in no time. My advice? If Restart is mainly performing logic that has to occur every time step, place it inside of some sort of loop instead, and include the call to start in your loop.

Second: Error line numbers aren't very helpful if we don't have the source/correct line numbers to match them up to. In this case, it's line being given the an older edition of a book, then being given page numbers from the new, expanded book. We can sort of figure it out to a point, but it's difficult.

Third: Lots of statics calls. To things that seem to be objects (Room1, which has a field room1, and  UserInput). This is more of a pet peeve of mine than anything else.

Ah, to recap: Try to take care of the recursion problem, it could be a big part of your troubles, or it could be obscuring what the real problem is. If the error is popping up in code we don't have, paste those chunks and mark which are causing the problems in the stack trace. Cheesy

Online Danny02
« Reply #5 - Posted 2012-02-22 18:47:23 »

just a quick scribble(if you aren't using Java7 use a if else instead of the switch case)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
...
   String[] command = getInput().split(" ");
   switch(command[0])
  {
     case "take":
       switch(room, inventory, command[1]); break;
     case "drop":
        switch(inventory, room, command[1]); break;
  }
...

private void switch(List<String> a, List<String> b, String object)
{
  if(a.remove(object))
    b.add(object);
}
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (64 views)
2014-04-15 18:08:23

BurntPizza (62 views)
2014-04-15 03:46:01

UprightPath (75 views)
2014-04-14 17:39:50

UprightPath (58 views)
2014-04-14 17:35:47

Porlus (76 views)
2014-04-14 15:48:38

tom_mai78101 (101 views)
2014-04-10 04:04:31

BurntPizza (161 views)
2014-04-08 23:06:04

tom_mai78101 (256 views)
2014-04-05 13:34:39

trollwarrior1 (209 views)
2014-04-04 12:06:45

CJLetsGame (216 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!