I am trying to create waypoints from a given file waypoints.txt

I first created the waypoint class

import java.io.*;
   import java.util.*;
   import java.util.HashMap; 
   import java.util.Iterator;



   public class waypoint {
      int x;
      int y;
      int height;
      int cost;
      int gold;
      int mapX;
      int mapY;
      public waypoint(int myX,int myY,int myHeight,int myCost,int myGold,int myMapX,int myMapY){
		myX=x;
		myY=y;
		myHeight=height;
		myCost=cost;
		myGold=gold;
		myMapX=mapX;
		myMapY=y;

      }
      public int myHashKey(){
         String hashable=(Integer.toString(x)+Integer.toString(y)+Integer.toString(height)+Integer.toString(cost)+Integer.toString(gold)+Integer.toString(mapX)+Integer.toString(mapY));
         return hashable.hashCode();
      }
      
   }

then i tried reading it and creating the waypoints using this...

public void waypoints(String[] args) throws FileNotFoundException {
         Scanner wps = new Scanner(new FileReader("waypoints.txt"));                      
         while(wps.hasNextInt()){
            if(wps.hasNextInt()){
               waypoint w = new waypoint(wps.nextInt(),wps.nextInt(),wps.nextInt(),wps.nextInt(),wps.nextInt(),wps.nextInt(),wps.nextInt());           
               animatePanel.addPermanentDrawable(new Marker((int) w.x,(int) w.y, Color.black));
                  
            }
         }
      }

and it wasn't creating anything so i tried printing w.x to see what it was putting and i just get 0's not sure why, if you could point me in the right direction it would be much aprectiated.

In your constructor, lines 17-23 you have all the assignments the wrong way round.

I have done it both ways but my w.x and w.y..etc still all are coming out to 0 instead of what should be passed from the file.

You need them like
x = myX;
If that still doesn't work put a load of print statements into your code, printing the values of the key variables at each stage so you cen see where it's going wrong.

You need them like
x = myX;
If that still doesn't work put a load of print statements into your code, printing the values of the key variables at each stage so you cen see where it's going wrong.

I changed them to

import java.io.*;
   import java.util.*;
   import java.util.HashMap; 
   import java.util.Iterator;



   public class waypoint {
      int x;
      int y;
      int height;
      int cost;
      int gold;
      int mapX;
      int mapY;
      public waypoint(int myX,int myY,int myHeight,int myCost,int myGold,int myMapX,int myMapY){
		x=myX;
		y=myY;
		height=myHeight;
		cost=myCost;
		gold=myGold;
		mapX=myMapX;
		mapY=myMapY;

      }
      public int myHashKey(){
         String hashable=(Integer.toString(x)+Integer.toString(y)+Integer.toString(height)+Integer.toString(cost)+Integer.toString(gold)+Integer.toString(mapX)+Integer.toString(mapY));
         return hashable.hashCode();
      }
      
   }

and I made a test instance to print stuff..

import java.io.*;
   import java.util.*;
   import java.util.HashMap; 
   import java.util.Iterator;  
	
   public class test{  
      public static void main(String[] args) throws FileNotFoundException {
         Scanner wps = new Scanner(new FileReader("waypoints.txt"));                      
         while(wps.hasNextInt()){
            if(wps.hasNextInt()){


               waypoint w = new waypoint(wps.nextInt(),wps.nextInt(),wps.nextInt(),wps.nextInt(),wps.nextInt(),wps.nextInt(),wps.nextInt());           
            
               System.out.print(w.x+" ");
            	            
            }
         	
         }
      }
   }

anytime i print w.x, or w.y anything from in the waypoint i get a line of 0's
but i know the scanner is working and reading them becasue i also tried

public class test{  
      public static void main(String[] args) throws FileNotFoundException {
         Scanner wps = new Scanner(new FileReader("waypoints.txt"));                      
         while(wps.hasNextInt()){
            if(wps.hasNextInt()){
             int x=wps.nextInt();
             System.out.print(x+" ");
}
}
}

I get the numbers from the file, I dont know why when i try to pass them into waypoint the just get zero'ed out...

I have done it both ways ....

In that case you should try to understand what you are doing... currently you are erasing the values in the arguments you passed to your method waypoint() by the values already present in the member variables by default (in this case 0).

After the corrections JC pointed out, print out the values being read from the file returned by wps.nextInt(), maybe the problem is in the waypoints.txt file ??

--> Too late .. guess should refresh my page more often :P

Edited 5 Years Ago by stephen84s: n/a

In that case you should try to understand what you are doing... currently you are erasing the values in the arguments you passed to your method waypoint() by the values already present in the member variables by default (in this case 0).

After the corrections JC pointed out, print out the values being read from the file returned by wps.nextInt(), maybe the problem is in the waypoints.txt file ??

--> Too late .. guess should refresh my page more often :P

if i do

Sytem.out.print(wps.nextInt()," ");

it prints out all the values in the waypoints.txt correctly. but when i pass them into my waypoint() method they all go to 0...

Yes, I got that. Try the print I suggested.

how would i do that print??

Edited 5 Years Ago by thebiff: n/a

how would i do that print??

Um you really shouldnt be asking that ....

Put this line in your constructor.

System.out.println(x + " " + y + " " + ...);

And just to be sure, have you re-compiled your code after making the changes JC suggested in this first post ?

Um you really shouldnt be asking that ....

Put this line in your constructor.

System.out.println(x + " " + y + " " + ...);

And just to be sure, have you re-compiled your code after making the changes JC suggested in this first post ?

I know how to write it I'm just not sure why I am putting it in my waypoint class...it won't do anything there?

This article has been dead for over six months. Start a new discussion instead.