Results 1 to 7 of 7
- 01-20-2011, 05:21 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Error printing from Read in file.
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.Java Code:import java.io.*; class FileRead { public static void main(String args[]) { try{ // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream("textfile.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the content on the console System.out.println (strLine); } //Close the input stream in.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } }
But this is the main code and the problem.
Java Code:import java.io.*; import java.util.*; class rrApp { public static void main(String args[]) { boolean flag = true; try { FileInputStream fis = new FileInputStream("waypoints.dat"); DataInputStream dis = new DataInputStream(fis); BufferedReader buf = new BufferedReader(new InputStreamReader(dis)); String strLine; //int j = 0; ArrayList<Integer> AListWP = new ArrayList<Integer>(); while ((strLine = buf.readLine()) != null) { //String strLine; int j = 0; String [] StringWP = strLine.split("\\s"); for (int i = 0; i < StringWP.length; i++) { System.out.println(StringWP[i]); } for (int i = 0; i < StringWP.length; i++) { String k = StringWP[i]; int h = Integer.parseInt(k); AListWP.add(h); System.out.println(h); } while(flag) { Waypoint [] ArrayWP = new Waypoint[AListWP.size()/2]; int count = 0; for (int i = j; i<j+2; i++) { ArrayWP[i] = new Waypoint(AListWP.get(i),AListWP.get(i+1)); } j=j+2; if (AListWP.size()/2 >= j) { flag = false; } } } dis.close(); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); } } }Last edited by Accendo; 01-21-2011 at 12:25 AM.
- 01-20-2011, 05:45 PM #2
The code seems Ok.
What is the full stack trace of error? From where it is getting triggered?
Use the Code Tags in right way.
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-20-2011, 05:57 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
I'm sorry I'm new to many simple concepts in java including what a stack trace is :s - fixed the code tags tho!
If stack trace is where it says symbol:, error:, etc about 4 lines of explanation (this might just be compile ) its not that :s.
Sorry and thank you for not being rude! Worst thing about posting on some forums hoping you dont get an interested troll...
- 01-20-2011, 06:42 PM #4
Yes, stack trace means the full error message that you are getting.
The code that you feel not working is half posted. Can you post the full code? And tell us what problem you are facing with that? Your error message will tell you at what line the things went wrong.
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-21-2011, 12:28 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
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 guessed it was something to do with changing the strings to int's and putting them in an array list I got wrong :S -- (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. Thats all I get, no line or anything just everything I wrote above.
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 (and a headache!)
But as requested mate, the full Class is now there thanks for taking an interest!
/|RT
- 01-21-2011, 06:03 AM #6
The error you are getting is actually a runtime exception IndexOutOfBoundsException when you are trying to access the element of ArrayList which is out of range.
Your for loop
is trying to access the elements at index 0,1 at first iteration and elements at index 1,2 at second iteration. Your ArrayList is storing only 2 elements, which are at index 0 and 1. So when you try to access index 2 the compiler throws the exception as it's out of range.Java Code:for (int i = j; i<j+2; i++) { ArrayWP[i] = new Waypoint(AListWP.get(i),AListWP.get(i+1)); }
Hope you are clear with it now,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-21-2011, 06:07 AM #7
You are getting only this message, "Error: Index: 2, Size: 2" at your console when the things go wrong.
That's because currently you are only printing the message in your catch block, by using,
Java Code:System.err.println("Error: " + e.getMessage());
Have a habit of printing the stack trace in your catch block,
as that will give you detailed idea about what went wrong at what line.Java Code:e.printStackTrace();
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
Similar Threads
-
Printing 2D Array Error
By noble in forum New To JavaReplies: 9Last Post: 11-09-2010, 05:31 PM -
Sorting and Printing file
By spry.chipper in forum New To JavaReplies: 3Last Post: 12-13-2009, 08:15 PM -
Printing text file
By amitcs1001 in forum NetBeansReplies: 2Last Post: 11-30-2009, 02:17 AM -
how to read openproj(Projity) file i.e. ,POD file(Project Management file)
By mahendra.athneria in forum New To JavaReplies: 0Last Post: 02-11-2009, 09:53 AM -
Job name when printing file
By pjmorce in forum Advanced JavaReplies: 3Last Post: 12-23-2008, 01:55 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks