Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

wrong int value  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 512
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys so I have been debugging my code now for almost 2 hours,I wanted to try solve the problem myself but after 2 hours I now have come here for help,so basically I created a dat file which consists of three integers and three Strings in the following order 1,hi,2,hello,3,hey.

.I have a Map called list yes I know horrible naming but I'm just doing it while I'm learning wouldn't do so in a proper application,anyway the Map consists of an Integer and a Print object there is three print objects,I structured the dat file to first contain the size of the map,then I wrote the start location of the actual print data and I gave enough space to come back and place the indexes in them before the print data,anyway the code was working fine when I first tested it,it seems to retrieve the correct ID(num) but when I try to print out the number value at the start of the print data it gives me a huge number -419430400 when the number -419430400 but what it should print is 1,2,3 after than it seems to go to the end of the file,I expected this to run three times,

if anybody could look through my code and tell me why this is occurring I would be more than greatful,sorry the code is quite long I couldn't pinpoint where the exact bug is,

thanks




 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the program's output that shows what you are talking about?  Post all of the output, not just one line.

Also post the code that wrote the dat file that is being read.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd start by printing out the value of index.get(i).startByte at line 44.  It's probably wrong. 
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is printed on line 46?
 
Adam Chalkley
Ranch Hand
Posts: 512
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys

heres the code that created the dat file it seems to work ok



also 19 is printed on line 46

here is the output

19
-419430400
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is in list?  Can you post code that builds list?

The same questions for index.
 
Adam Chalkley
Ranch Hand
Posts: 512
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Norman I added



to the main method that created the dat file and recompiled it,seemed to fix the problem but there is still a MAJOR bug,

for some reason when I loop through the index map it only prints the last two indexes and not the first


also I deleted the code that made the list I maybe should have just commented it out I made it to a txt file with a FileWriter then here is the code I used to read it in



then I wrote the info to a dat file


as I said for some reason it won't print the first index or add it to the map which I named list

thanks




 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please make a small, complete program that compiles, executes for testing and shows the problem.
 
Adam Chalkley
Ranch Hand
Posts: 512
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've pinpointed the code that is giving me problems but the only thing is it looks fine but when I look at the console it seems to skip printing the value (number 1)



heres the output of the following code

1
2
2
3
3
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please make a small, complete program that compiles, executes for testing and shows the problem.
 
Adam Chalkley
Ranch Hand
Posts: 512
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Norman

I don't quite understand what you mean

thanks
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make ONE small, complete set of code that compiles, executes and shows the problem.  Post it so it can be copied, compiled and executed for testing.

It is not possible to test with the small random bits of code posted on this thread.
 
Adam Chalkley
Ranch Hand
Posts: 512
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh ok no problem

I'll post it now in 2 secs
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See what was posted about SSCCE in this other thread of yours:

Knute Snortum
Sheriff

   
Chrome  Eclipse IDE  Java  Postgres Database  VI Editor

Adam: you have a habit of posting incomplete code.  Read about what a SSCCE is (that should be a link).  Here's what one would look like in your case:
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget the static blocks, you need a write() and a read() method and a main() which looks something like

 
Adam Chalkley
Ranch Hand
Posts: 512
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry Norm just didn't want to post a lot of code

anyway here is what I wrote

write this code to create a txt file or just create it in a text editor




then read the data into the file and compile it so the variables are intialised



compile again then uncomment or delete the code that created the dat file then add this code but don't compile until all of it is added




that should be the final code

finally create two classes one for index and one for print




sorry about all the code there is quite a lot,but I really appreciate your effort means a lot to me =)

I'm just baffled why it skips the first entry in the index


thanks
 
Adam Chalkley
Ranch Hand
Posts: 512
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Carey,thanks for the advice the only reason I used the static blocks was so I could read and initialise the variables before the main method ran
 
Adam Chalkley
Ranch Hand
Posts: 512
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also change the for loop of the final product to




thanks again really appreciate the help
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked at the hi.txt file written in the Create class with a hex editor to see what is written?
It has binary data mixed with text.   The code in the Main class will not work with binary data.
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:Hi Carey,thanks for the advice the only reason I used the static blocks was so I could read and initialise the variables before the main method ran

Why? I doesn't appear to be necessary to me. How are you going to test it without separate read and write methods?
Also, keep it simple until you get it working. The way you are reading and writing the txt file is wrong. Forget the txt file for now and hard code the initialization.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Work on code to read what was written in the Create class. 
Add some lines of code to that class to read in the contents of the hi.txt file and print it.  Work on that until what is read and printed is the same as what was written.


The rest of the code that was posted is just in the way of working on and solving the problem.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SSCCE stands for Short, Self Contained, Compilable, Example.  The key for you is SHORT!  Remove everything that isn't part of the problem.

Start a new project if you're worried about losing what you've written. 
 
Adam Chalkley
Ranch Hand
Posts: 512
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the solution I spent hours on this,probably because I was committed to solving this problem I guess anyway after written out scratch again I finally got it to produce what I want

If anyone wants to look at the final product and compare it to the code that skipped the first value that would be more than great because I'm still puzzled to why the first value got skipped

ignore the comments they are there just to show you how I created the file and read from the file

anyway it's not short Knute haha but it's just the solution

PS Carey I am now going to rewrite the code without the static block =) thanks for the advice means a lot


 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I mean by an SSCCE.  It's "short" in the sense that there is little that can't be removed.  It is self-contained in that I can copy the code and it will compile on its own.  Notice that I have import statements and the missing classes all in one file.  I also did some internal cleaning up of the code which you can see from the comments.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!