Results 1 to 7 of 7
  1. #1
    Accendo is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default 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:
    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());
        }
      }
    }
    It was working correctly.


    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.

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    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.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    Accendo is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    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...

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    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.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  5. #5
    Accendo is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    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

  6. #6
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    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

    Java Code:
    for (int i = j; i<j+2; i++)
    	{
    	ArrayWP[i] = new Waypoint(AListWP.get(i),AListWP.get(i+1));
    	}
    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.

    Hope you are clear with it now,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    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,

    Java Code:
    e.printStackTrace();
    as that will give you detailed idea about what went wrong at what line.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Printing 2D Array Error
    By noble in forum New To Java
    Replies: 9
    Last Post: 11-09-2010, 05:31 PM
  2. Sorting and Printing file
    By spry.chipper in forum New To Java
    Replies: 3
    Last Post: 12-13-2009, 08:15 PM
  3. Printing text file
    By amitcs1001 in forum NetBeans
    Replies: 2
    Last Post: 11-30-2009, 02:17 AM
  4. Replies: 0
    Last Post: 02-11-2009, 09:53 AM
  5. Job name when printing file
    By pjmorce in forum Advanced Java
    Replies: 3
    Last Post: 12-23-2008, 01:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •