简体   繁体   中英

Java HashMap Iteration not showing comparison between key and variable

I am trying to iterate through a HashMap and when a duplicate key (key=node coord) is detected I want to give the new node with the same key( coord ) the same nodeID( Hasmap value ). Here is my problem codeblock and my output. As you can see it outputs that the key( keys) and the new key(finishedLine3) have the same value but it will not go into the "if" statement and change the nodeIDs.

HISTORY: I am comparing with " == " here but have also tried if(keys.equal(finishedLine3)) and get the same result.

I also show in the output that nodes key: -86.88642133.035899 nodes value: 00000001 and nodes key: -86.88642133.035899 nodes value: 00000010 are the same but at no time during the iteration does my logic allow these comparison of the two exact keys go into my "if" block.

Here is my code:

    package com.KatsProject.net;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class MakeFile {
    public static void main(String[] args) throws IOException {
         DecimalFormat df = new DecimalFormat("00000000");
         Integer numWords = 0;
         Integer numLines = 1;
         Integer numNodes =0;
         Integer nodeID =0;
         Integer counter=0;
         String nodesID2 ="";
         String newNodeID="";
         String  header = "road_id      #_of_nodes          node_id         node_x_coord        node_y_coord    ... end of list";
         String finishedLine1 = " ";
         String finishedLine2 = " ";
         String finishedLine3 = " ";
         String finishedLine34 = " ";
         String finishedLine = " ";
         String roadID= "";
         String roadID2= "";
         String match=" ";
         String match2=" ";
         String temp=" ";
         BufferedReader br= null;
         BufferedWriter bw= null;
         HashMap<String, String> nodes = new HashMap<String, String>();
         br = new BufferedReader(new FileReader("C:\\Users\\gary\\Desktop\\roads.txt"));
         try {
                bw = new BufferedWriter(new FileWriter("C:\\Users\\gary\\Desktop\\Secretfile"));
            } catch (IOException e) {
                System.out.println("Error writing file");
                e.printStackTrace();
            }
         String line = null;
         while( (line = br.readLine())!= null )
         {   
            if(numLines > 1){
            numWords =0;  
            nodeID +=1; 
            newNodeID= df.format(nodeID);
            nodesID2 = newNodeID;
            String [] tokens = line.split("\\s+");
            roadID = tokens[0]; // save roadID for comparison
            finishedLine1="";
              for(String s :tokens){
                  numWords+=1; 
                  if(numWords == 1){
                     // x.setRoadID(roadID);
                  }  
                  if(numWords ==2 ){
                     match += s;
                  }
                  if(numWords ==3 ){
                      match2 += s;
                      //System.out.println(match2+"this is match2 ,  " + match + "  this is match");
                  }
                  if(numWords == 4 ){
                      finishedLine3 += s;               //for hash table
                      finishedLine3.trim();
                      finishedLine1 += s;
                      finishedLine1 += "      ";
                      //System.out.println(x.getX_coord()+ "   this is x_coord.");
                  }
                  if(numWords == 5){
                      finishedLine3 += s;                 //for hash table
                      finishedLine3.trim();
                      finishedLine1 += s;
                      finishedLine1 += "      ";
                      String key =finishedLine3;
                      //System.out.println("It is    " + nodes.containsKey(key)+ "  that the nodeID for the key :    "+ finishedLine3 + "    is:     " + nodes.get(key)+ "      "+ key );
                      //System.out.println(finishedLine3+ " Is this the line that also includes the roadID #?");
                      //nodes.put(key,newNodeID );
                      Set<String> keySet = nodes.keySet();
                      Iterator<String> keyIterator = keySet.iterator();
                      //System.out.println("Another iteration starts , this is iteration :       "+ nodeID);
                      bw.write("Another iteration starts , this is iteration :       "+ nodeID);
                      bw.write("\n");
                      while (keyIterator.hasNext()) {
                          String keys = keyIterator.next();
                          //System.out.println( keys);
                          //System.out.println(" nodes key: " + keys + " nodes value: " + nodes.get(keys));
                          //System.out.println("Is keys:  "+keys + " equal to finishedLine3 :  " +finishedLine3);
                          if (keys.equals(finishedLine3))
                          {

                              //System.out.println( "This is finishedLine3 :  "+finishedLine3 +" , and it is equal to:  " + keys);  
                              newNodeID = nodes.get(keys);
                              // System.out.println(nodes.get(key) + "     this is nodes.get(key) ");
                              //System.out.println(nodes.get(keys) + "     this is nodes.get(keys) ");
                              //finishedLine2+=nodes.get(finishedLine3);
                              finishedLine3="";
                              }
                      }
                      nodes.put(key,newNodeID );
                      match2="";
                      match="";

                   }
              }
              if ((!(roadID.equals(roadID2)))&&(match.equals(match2)))
              {
                   finishedLine+= roadID;
                   finishedLine+= "              ";
                   finishedLine+=numNodes;
                   finishedLine+= "             ";
                   finishedLine+=finishedLine2;
                   finishedLine+="...end of list \n";
                   bw.write(finishedLine);
                   bw.write("\n");
                   finishedLine=" ";
                   finishedLine2=" ";
                   finishedLine3=" ";
                   numNodes=0;
                   roadID2 = roadID;
                   //roadID2 = x.roadID;
                   match="";
                   match2="";

              }
              if((roadID.equals(roadID2))||roadID2==""){
                  //finishedLine2+= x.getNodeID();
                  finishedLine2+= newNodeID;
                  //System.out.println( " This is finishedLine2 after inserting newNodeID: "+ newNodeID+"   "+ finishedLine2);
                  //System.out.println(newNodeID + "     this is newNodeID copied to finishedLine2 string."+finishedLine2+" checking to see if newNOdeID was changed in string");
                  //System.out.println(" This is finishedLine1: " +finishedLine1);
                  finishedLine2+="          ";
                  finishedLine2 += finishedLine1;
                  finishedLine2+="              ";
                  //System.out.println( " This is finishedLine2: "+ finishedLine2);
                  numNodes+=1;
                  finishedLine1="";
                  match2="";
                  match="";
                  //System.out.println("this is in if statement");
              }
              finishedLine3 = "";
         }
         else{
              System.out.print(header+"\n");
               bw.write(header + "\n");
               numLines+=1;
          }
          roadID2 = roadID;
          match="";
          match2="";

       }    
       br.close(); 
    }    

}

And here is my output:

        road_id     #_of_nodes          node_id         node_x_coord        node_y_coord    ... end of list
This is keys:   -86.88642133.035899and this is the :  1  iteration through the while loop
Is keys:   -86.88642133.035899 equal to finishedLine3 :  -86.88651833.035997? 
This is keys:   -86.88642133.035899and this is the :  2  iteration through the while loop
Is keys:   -86.88642133.035899 equal to finishedLine3 :  -86.88657833.03605? 
This is keys:  -86.88651833.035997and this is the :  3  iteration through the while loop
Is keys:  -86.88651833.035997 equal to finishedLine3 :  -86.88657833.03605? 
This is keys:   -86.88642133.035899and this is the :  4  iteration through the while loop
Is keys:   -86.88642133.035899 equal to finishedLine3 :  -86.88666833.03609? 
This is keys:  -86.88651833.035997and this is the :  5  iteration through the while loop
Is keys:  -86.88651833.035997 equal to finishedLine3 :  -86.88666833.03609? 
This is keys:  -86.88657833.03605and this is the :  6  iteration through the while loop
Is keys:  -86.88657833.03605 equal to finishedLine3 :  -86.88666833.03609? 
This is keys:   -86.88642133.035899and this is the :  7  iteration through the while loop
Is keys:   -86.88642133.035899 equal to finishedLine3 :  -86.88674233.03608? 
This is keys:  -86.88651833.035997and this is the :  8  iteration through the while loop
Is keys:  -86.88651833.035997 equal to finishedLine3 :  -86.88674233.03608? 
This is keys:  -86.88666833.03609and this is the :  9  iteration through the while loop
Is keys:  -86.88666833.03609 equal to finishedLine3 :  -86.88674233.03608? 
This is keys:  -86.88657833.03605and this is the :  10  iteration through the while loop
Is keys:  -86.88657833.03605 equal to finishedLine3 :  -86.88674233.03608? 
This is keys:   -86.88642133.035899and this is the :  11  iteration through the while loop
Is keys:   -86.88642133.035899 equal to finishedLine3 :  -86.88679433.036037? 
This is keys:  -86.88651833.035997and this is the :  12  iteration through the while loop
Is keys:  -86.88651833.035997 equal to finishedLine3 :  -86.88679433.036037? 
This is keys:  -86.88674233.03608and this is the :  13  iteration through the while loop
Is keys:  -86.88674233.03608 equal to finishedLine3 :  -86.88679433.036037? 
This is keys:  -86.88666833.03609and this is the :  14  iteration through the while loop
Is keys:  -86.88666833.03609 equal to finishedLine3 :  -86.88679433.036037? 
This is keys:  -86.88657833.03605and this is the :  15  iteration through the while loop
Is keys:  -86.88657833.03605 equal to finishedLine3 :  -86.88679433.036037? 
This is keys:   -86.88642133.035899and this is the :  16  iteration through the while loop
Is keys:   -86.88642133.035899 equal to finishedLine3 :  -86.88679133.035966? 
This is keys:  -86.88651833.035997and this is the :  17  iteration through the while loop
Is keys:  -86.88651833.035997 equal to finishedLine3 :  -86.88679133.035966? 
This is keys:  -86.88674233.03608and this is the :  18  iteration through the while loop
Is keys:  -86.88674233.03608 equal to finishedLine3 :  -86.88679133.035966? 
This is keys:  -86.88679433.036037and this is the :  19  iteration through the while loop
Is keys:  -86.88679433.036037 equal to finishedLine3 :  -86.88679133.035966? 
This is keys:  -86.88666833.03609and this is the :  20  iteration through the while loop
Is keys:  -86.88666833.03609 equal to finishedLine3 :  -86.88679133.035966? 
This is keys:  -86.88657833.03605and this is the :  21  iteration through the while loop
Is keys:  -86.88657833.03605 equal to finishedLine3 :  -86.88679133.035966? 
This is keys:   -86.88642133.035899and this is the :  22  iteration through the while loop
Is keys:   -86.88642133.035899 equal to finishedLine3 :  -86.88674333.03591? 
This is keys:  -86.88651833.035997and this is the :  23  iteration through the while loop
Is keys:  -86.88651833.035997 equal to finishedLine3 :  -86.88674333.03591? 
This is keys:  -86.88679133.035966and this is the :  24  iteration through the while loop
Is keys:  -86.88679133.035966 equal to finishedLine3 :  -86.88674333.03591? 
This is keys:  -86.88674233.03608and this is the :  25  iteration through the while loop
Is keys:  -86.88674233.03608 equal to finishedLine3 :  -86.88674333.03591? 
This is keys:  -86.88679433.036037and this is the :  26  iteration through the while loop
Is keys:  -86.88679433.036037 equal to finishedLine3 :  -86.88674333.03591? 
This is keys:  -86.88666833.03609and this is the :  27  iteration through the while loop
Is keys:  -86.88666833.03609 equal to finishedLine3 :  -86.88674333.03591? 
This is keys:  -86.88657833.03605and this is the :  28  iteration through the while loop
Is keys:  -86.88657833.03605 equal to finishedLine3 :  -86.88674333.03591? 
This is keys:   -86.88642133.035899and this is the :  29  iteration through the while loop
Is keys:   -86.88642133.035899 equal to finishedLine3 :  -86.88668133.035885? 
This is keys:  -86.88651833.035997and this is the :  30  iteration through the while loop
Is keys:  -86.88651833.035997 equal to finishedLine3 :  -86.88668133.035885? 
This is keys:  -86.88674333.03591and this is the :  31  iteration through the while loop
Is keys:  -86.88674333.03591 equal to finishedLine3 :  -86.88668133.035885? 
This is keys:  -86.88679133.035966and this is the :  32  iteration through the while loop
Is keys:  -86.88679133.035966 equal to finishedLine3 :  -86.88668133.035885? 
This is keys:  -86.88674233.03608and this is the :  33  iteration through the while loop
Is keys:  -86.88674233.03608 equal to finishedLine3 :  -86.88668133.035885? 
This is keys:  -86.88679433.036037and this is the :  34  iteration through the while loop
Is keys:  -86.88679433.036037 equal to finishedLine3 :  -86.88668133.035885? 
This is keys:  -86.88666833.03609and this is the :  35  iteration through the while loop
Is keys:  -86.88666833.03609 equal to finishedLine3 :  -86.88668133.035885? 
This is keys:  -86.88657833.03605and this is the :  36  iteration through the while loop
Is keys:  -86.88657833.03605 equal to finishedLine3 :  -86.88668133.035885? 
This is keys:   -86.88642133.035899and this is the :  37  iteration through the while loop
Is keys:   -86.88642133.035899 equal to finishedLine3 :  -86.88642133.035899? 

Thanks to anyone that can help and hope I was clear and concise enough.

While using the == , you normally imply that the objects are the same. Therefore, avoid using == for objects in favor of Object equals() but use == for primitives. In your case, you should be using keys.equals(finishedLine3) as these are two different objects. In your problem above, you haven't shown initialization of the finishedLine3 which is probably causing the wrong behavior.

I am assuming that you replicated the output of the code you have run. I would suggest you use Object equals() when comparing two string objects. But what I could see in your code is that there is a leading blank space in your key(" -86.88642133.035899" instead of "-86.88642133.035899"). So you could try avoiding this while creating the HashMap or if you are getting your input from some file you could trim the string, here is a link to the documentation-

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim()

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM