aspose file tools*
The moose likes Beginning Java and the fly likes Error printing out from a read in file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error printing out from a read in file." Watch "Error printing out from a read in file." New topic
Author

Error printing out from a read in file.

Ryan Tracey
Greenhorn

Joined: Jan 20, 2011
Posts: 6
The error I am having here is that my code is only printing the first and second number from the file and it is set out like this
-------------------
12 2
? ?
? ?
? ?
? ?
? ?
-------------------
Without the ---- and I can only remember the 12 2 because they print, but everything else isn't appearing and it is letting me compile HOWEVER when it runs it catches an error that says "Error: Index 2:, Size: 2"

When I was just reading and printing the file using:

It was working correctly.


But this is the main code and the problem.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

HI Ryan,

Welcome to the Ranch!

You've only shown half your program, and I don't believe the error is coming from this half. Can we see the rest, please?

Also, if you're getting the whole stack trace from that error message , then the top few lines of the trace should tell you exactly on which line of your program the error occurs; if you don't know how to interpret that, you can paste it into your message too and we can explain it to you.


[Jess in Action][AskingGoodQuestions]
Ryan Tracey
Greenhorn

Joined: Jan 20, 2011
Posts: 6
Hey I edited the code above so it shows all the Class. The reason I didn't enter it though is because I made a random For Loop that should of run for plenty of time but stuck at the same error so I didn't want to throw uneccesary code :S.

As for the error I am getting -- The program compiles BUT when I run it the Try/Catch throws me the error from above :S.

I know when I get compile errors to try and hunt it down to the line and fix it NP but I have never really used Try/Catch lol and it's just giving me the error from above.

But as requested mate, the full Class is now there thanks for taking an interest!

/|RT
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Well, I can't say I can quite follow the logic in that last "while" loop, but that's clearly where the problems are coming in. The line

ArrayWP[i] = new Waypoint(AListWP.get(i),AListWP.get(i+1));

makes a lot of assumptions about the relationship between the size of the list and of the array and the value of "i", and what's happening is that one of those assumptions just isn't true. Try adding some "println"s to help you visualize what's happening -- for example



I'll guarantee you'll be surprised by something in the results!">
Ryan Tracey
Greenhorn

Joined: Jan 20, 2011
Posts: 6
Hmm I have an infinite loop lol must be to do with my flag not becoming false due to problem you mentioned :S.

Could you help me write what I meant?

I need a new array of my class Waypoints it should be 1/2 the size of the AListWP because that is where its reading in the info 2 at a time.

Then I made a for loop to populate the Waypoint array. The way I figured the logic on this was to increase i by 2 each time so I can use i to get out info sequentially from the AListWP ArrayList.

Then once j is the same as the size of the ArrayList it should end the while loop.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
That for loop with j + 2 looks a very flaky construction to me. It would appear you are using a loop which creates one Waypoint.
Why don't you create your Waypoints as you read the file?
What format is the file in? Is it text? If so, why don't you use a Scanner to read it? You can use its nextXXX and hasNextXXX methods, and call nextXXX twice to create a Waypoint. Then you can add the Waypoint directly to the list.
If you have a List, why are you using an array as well?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
By the way: you need to ensure you have an even number of entries in your data file, otherwise you will end up creating half a waypoint, and (if you are lucky) suffering an Exception which closes your application.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error printing out from a read in file.