Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
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  
  Need Help with Linked List problem PLEASE  (Read 1309 times)
0 Members and 1 Guest are viewing this topic.
Offline markmistry

Junior Devvie





« Posted 2009-04-30 16:13:47 »

hi im having a slight problem trying to understand why my linked list has stopped working

heres some code snipits


   System.out.println(names); // this prints [markmistry ]     this is my LinkedList
   System.out.println(newname); // this prints markmistry    this is a String variable
   locationIndex1 = names.indexOf(newname); locationIndex1 is an int
   System.out.println(locationIndex1); // this prints -1   why?
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2009-04-30 17:08:25 »

It's not entirely clear what's going on because you've not provided the exact code you're using, but I've got a guess.

In a LinkedList, indexOf returns -1 if it does not find what you were looking for.
http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedList.html#indexOf(java.lang.Object)

So you're getting -1 because "markmistry" isn't found in your list. Even though your list prints out "markmistry," the problem is with string comparison.

i.e.

String a = "markmistry";
String b = "markmistry";

if (a == b)
{
     System.out.println("Equal");
}
else
{
     System.out.println("Not Equal");
}

The above would not print out Equal, it would print Not Equal, because using == is a memory address comparison, not a value comparison. What you would want to do is this:

if (a.equals(b))
{
     System.out.println("Equal");
}
else
{
     System.out.println("Not Equal");
}

And that would print Equal.

The indexOf method... wow I was going to say indexOf uses == but then I checked the docs myself... it actually uses .equals. Okay.

Looking further into it, it looks like your names array contains the string "markmistry " but you're looking for "markmistry" . Make sure there is no space in there.

See my work:
OTC Software
Offline markmistry

Junior Devvie





« Reply #2 - Posted 2009-04-30 17:20:48 »

i noticed the space at the end of the string in the array or list whateveryou call it so i added a space to the string it still didnt work heres the entire class hope i dont get told off for posting this but i am really stuck basically i am trying to send private messages to only the person that indexof finds.
bigger snippet
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  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
    private static class Handler extends Thread {

        private String name;
        private Socket socket;
        private BufferedReader in;
        private PrintWriter out;

        public Handler(Socket socket) {

            this.socket = socket;
        }

        public void run() {

            try {

                in = new BufferedReader(new InputStreamReader(
                    socket.getInputStream()));

                out = new PrintWriter(socket.getOutputStream(), true);

                while (true) {

               out.println("SUBMITNAME");
               name  = in.readLine();
                           
           

                    if (name == null) {
                        return;
                    }

                    synchronized (names) {

                        if (!names.contains(name)) {

                            names.add(name);
                     // indexOf will work here but no where else ??
                     
                     ClientArea.append(name+ "\n");  
                     break;
                        }

                    }

                }


                writers.add(out);

                while (true) {


                    String input = in.readLine();
               messageArea.setCaretPosition(messageArea.getDocument().getLength());
               messageArea.append(name + " : " + input+"\n");
               System.out.println(input);
                    if (input == null) {
                        return;
                    }
               
                    if (input.startsWith("PRIVATE")){
               
                  StringTokenizer st = new StringTokenizer(input,"PRIVATE");
           
                  newname = st.nextToken();
                  newmess = st.nextToken();
                  newname2 = st.nextToken();
                 
                  messageArea.setCaretPosition(messageArea.getDocument().getLength());
                  messageArea.append("Private message intended for "+newname+ " message = "+ newmess+" private from "+newname2+"\n");
// this below doesnt work
                 
                  synchronized (names) {
                 
                     System.out.println(names);
                     System.out.println(newname);
                     locationIndex1 = names.indexOf(newname);
                     System.out.println(locationIndex1);
                 
                     if (names.contains(newname)) {

                        System.out.println("true name was found");
                       
                        try{
           
                           locationIndex1 = names.indexOf(newname);
                           
                           ((PrintWriter)writers.get(locationIndex1)).println("PRIVATE"+":" +newname2+":" +newmess);
                           //System.out.println("Private message intended for "+newname+ " message = "+  newmess+" private from "+newname2);
                        }
                       
                        catch (Exception e){
           
                           System.out.println(e);
                        }
                     }
                  }

               }
               
               else{
               
                  for ( i = writers.iterator(); i.hasNext();){
     
                     if (exitee!=""){

                        ((PrintWriter)i.next()).println("MESSAGE " +":" +  exitee + ":" + "logging out"+"\n"+"MESSAGE " +":" + name + ":"                            + input);
                        //System.out.println(exitee + ": " + "logging out");
               
                     }
                     
                     else{
                     
                        ((PrintWriter)i.next()).println("MESSAGE " +":" + name + ":" + input);
                        //System.out.println("MESSAGE " + name + ": " + input);
                     }
                  }
                 
                  exitee ="";
               }
                }



            }
         
   
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline markmistry

Junior Devvie





« Reply #3 - Posted 2009-04-30 17:51:32 »

better idea i am going to remove private facility its only for a game chat anyway not needed really.
ill figure it out when i get like a few months free time  Grin
Offline ewjordan

Junior Devvie





« Reply #4 - Posted 2009-04-30 18:06:09 »

If you return to this, I'd suggest actually looping through your entire names list and doing comparisons one at a time, printing out both the names and the comparison result.  Maybe something else altogether is happening, and the names are not being properly added or something like that, which you'll only know by inspecting the whole array when you want to do the comparison.  I'd also make sure that your threading is not causing some problems, for instance, make sure you're not storing separate name lists for each thread or something like that.
Offline Json

Junior Devvie


Exp: 7 years



« Reply #5 - Posted 2009-04-30 19:00:08 »

   System.out.println(names); // this prints [markmistry ]     this is my LinkedList
   System.out.println(newname); // this prints markmistry    this is a String variable
   locationIndex1 = names.indexOf(newname); locationIndex1 is an int
   System.out.println(locationIndex1); // this prints -1   why?

Is that just a typo that you have a space on the end of markmistry when you print the actual list out?

[markmistry ] <- Notice the space after the string and before the ]

// Json
Offline markmistry

Junior Devvie





« Reply #6 - Posted 2009-04-30 19:23:43 »

no mate the space is there in the array when i call it but if i add a blank space at the end of the string it still doesnt work and it prints the same result.
the threading issue has crossed my mind several times when i check for "name " or "name" directly instead of the variable reference it still doesnt work..lol its a right head scratcher

for testing i log on as 1 person and tried to send private to myself only 1 name in array i printed it i checked the client code to see if im adding a blank char at end of the name i send when logging on to the gameserver everythings fine...the thing is...it used to work fine..ive recently updated my jre and im starting to think it may be that but no worries i removed the private ..for now i got bigger problems like

1 min i can log on to game via my sql with a name and password thats not there the next min i cant Huh?? wtf?Huh??
Offline markmistry

Junior Devvie





« Reply #7 - Posted 2009-05-01 09:40:28 »

Part of the problem is solved now on to the next part.
I managed to track down the culprit

            kickass.newuser = kickass.newuser.replace('/',' '); 

I use the "/" as a delimiter and i replaced it with ' ' but i had forgot to trim the whitespace from the end

            String mynewstring = kickass.newuser.trim();
            kickass.newuser = mynewstring;

all is to sort out my print writer

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
try{
      locationIndex1 = names.indexOf(newname);

// is ithis correct below as im sure it used to work
      ((PrintWriter)writers.get(locationIndex1)).println("PRIVATE"+": " +newname2+": " +newmess);
}
catch (Exception e){
      System.out.println(e);
}
[\code]
Offline Json

Junior Devvie


Exp: 7 years



« Reply #8 - Posted 2009-05-01 09:59:47 »

First of all

1  
kickass.newuser = kickass.newuser.replace('/',' ');


could probably be replaced with

1  
kickass.newuser = kickass.newuser.replace('/',' ').trim();


or even

1  
kickass.newuser = kickass.newuser.replace("/","");


which would replace the "/" with an empty string.



Second I'm not really sure how your lists writers and names are created and if these are the only places you do add on them, but relying on the list indexes to be the same in names and writers might not be the best. You might be better off having a map for the writers.

1  
2  
3  
4  
5  
6  
7  
8  
    final PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

    final Map<String, PrintWriter> writers = new HashMap<String, PrintWriter>();

    writers.put(newname, out);

    // Later on when you want to get that one just do a get on the name as a key
    final PrintWriter thisUsersPrintWriter = writers.get(newname);



Something like that, I don't know the full story but maybe you need to change the structure a little bit.

// Json
Offline markmistry

Junior Devvie





« Reply #9 - Posted 2009-05-01 10:06:45 »

Thanks i will take a look at that but the ('/','') wont work i tried it compiler complains blank character literal i will try the trim at the end :O) nice to learn new ways of doing something..cheers
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Json

Junior Devvie


Exp: 7 years



« Reply #10 - Posted 2009-05-01 11:41:28 »

Thats why you should use normal quotes and not single quotes in your replace, just like I typed it in the example Cheesy

// Json
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.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (114 views)
2014-11-26 15:22:04

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!