The withoutcomment.txt file contains :
[general]
"PC", 0x0, 32, RW, reg , 1,
"Program Counter (R15)";
"LR", 0x11, 32, RW, reg , 18, "Link Register (R14)";
"SP", 0x10, 32, RW, reg , 17, "Stack Pointer (R13)";

But i am able to print only first line but its not going to the 2nd line its directly going to the 3rd line and printing till 32 and later its showing "Error: String index out of range: 3"

Can any one help me in solving this problem...

Thanks

import java.io.*;
class FileRead 
{
	static int count = 0;
	static int var_id;
	public static void main(String args[])

	{
		try{
			// Open the file that is the first 
			// command line parameter
			FileInputStream fstream = new FileInputStream("C:\\Documents and Settings\\karthikdatt\\Desktop\\WithOutComment.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
		if(count == 0){
				strLine = strLine.substring(1, (strLine.length()-1));
				
				

					if(strLine.equals("general"))	
					{
						var_id=1;
						System.out.println ("Parameter :" +strLine);
					}
				//	if(strLine.equals("inst-max"))
					//{
						//var_id=2;
				//		System.out.println("Parameter :" +strLine);
				//	}
				}
			/*if(var_id==1)
				{
				strLine = br.readLine();
				
					String[] stringSplit = strLine.split(",");
					System.out.print("Data Type: "+stringSplit[0].trim()+"\n");
					
					System.out.print("Size: "+stringSplit[1].trim().substring(0, stringSplit[1].trim().length()-1));  
				//	System.out.print("\n--------------------");
				//	System.out.print("\n");
					if(strLine.equals("["))
						break;
				}*/	
			strLine = br.readLine();
			if(var_id==1)
			{
				
				
				String[] stringSplit = strLine.split(",");
				System.out.print("Register name is : " +stringSplit[0].trim()+"\n");
				System.out.print("Reg No: "+stringSplit[1].trim().substring(0, stringSplit[0].trim().length()-1)+"\n");
				System.out.print("No of bits: "+stringSplit[2].trim().substring(0, stringSplit[1].trim().length()-1)+"\n");  
				System.out.print("attribute: "+stringSplit[3].trim().substring(0, stringSplit[1].trim().length()-1)+"\n");  
				System.out.print("type: "+stringSplit[4].trim().substring(0, stringSplit[0].trim().length()-1)+"\n");  
				System.out.print("C_No: "+stringSplit[5].trim().substring(0, stringSplit[3].trim().length()-1)+"\n");  
				System.out.print("Description: "+stringSplit[6].trim().substring(0, stringSplit[6].trim().length()-1)+"\n");  
			}
			
			
				count++;
		}
			//Close the input stream
			in.close();
		
		}
		catch (Exception e){//Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}
}

Edited 5 Years Ago by peter_budo: Do not use CODE SNIPPET to posts questions! Use Discussion Thread

replace

String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {

by

String strLine = br.readLine();
//Read File Line By Line
while ((strLine != null) {

If you look closely, you'll notice you call the strLine = br.readLine(); within your loop as well.

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