Jump to content
xisto Community
apurva

Where Am I Making Mistake

Recommended Posts

hello i have just started to learn java by myself and i am stuck here with this example
i want to show a file in console please tell me where am i making mistake.

i am getting erroe variable fin maight not be initialised

here is the code

/** Display a text file To use this program,specify the name of file that you want to see */ import java.io.*; class showfile { 	public static void main(String args[])throws IOException 	{ 		int i; 		FileInputStream fin; 		try 			{ 			BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 			System.out.print("Enter file name:"); 			String filename=br.readLine(); 			fin=new FileInputStream(filename); 			 			} 			catch(FileNotFoundException e) 			{ 				System.out.println("File not found"); 				return; 				 			} 			catch(ArrayIndexOutOfBoundsException e) 			{ 				System.out.println("Ussag:Showfile file"); 			} 			//Read characters untill Eof id encountered 			do 			{ 			   i=fin.read(); 				if(i!=-1) 					System.out.println((char)i); 			} 			while(i!=-1); 			fin.close(); 	} }

Share this post


Link to post
Share on other sites

You are getting that error because it is telling you that fin may not be set since your doing that in the try&catch loop, and then you are doing methods of the FileInputStream class with a variable that possibly may be empty. Put your do-while loop and fin.close(); inside the try, or you could try setting fin to an empty file if you want in the two catches, but I'm not sure if that would work or not. Your best bet is to move the do-while loop and your fin.close();

Share this post


Link to post
Share on other sites

try {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter file name:");
String filename=br.readLine();
fin=new FileInputStream(filename);

}
catch(FileNotFoundException e)
{
System.out.println("File not found");
return;

}


You have a couple problems yet. First, you should not declare the file input stream in the same try/catch as the BufferedReader on standard input. Separate these. Also, you are continuously redefining the InputStream in the later part of the code, which will make the program simply run an infinite loop. A corrected version of the code would look something like this (depending upon what you wanted it exactly to do):

/** Display a text fileTo use this program,specify the name of file that you want to see*/import java.io.*;class showfile{	public static void main(String args[])throws IOException	{		int i;		FileInputStream fin;		String filename = "";		try			{			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));			System.out.print("Enter file name:");			filename=br.readLine();									}			catch(FileNotFoundException e)			{				System.out.println("File not found");				return;							}			catch(ArrayIndexOutOfBoundsException e)			{				System.out.println("Ussag:Showfile file");			}			//Open the FileInputStream			try{			fin=new FileInputStream(filename);			}			catch(FileNotFoundException e){			  e.printStackTrace();			  System.out.println("File not found");			  return;			}			//Read characters untill Eof id encountered			do			{			   i=fin.read();				if(i!=-1)					System.out.println((char)i);			}			while(i!=-1);			fin.close();	}}

If you have any questions, just let me know. I hope this helps!

Notice from rvalkass:

Code tags added around code.

Share this post


Link to post
Share on other sites

Even though its not good programing practice, defining BufferedReader in the try was not causing any error because it was not being referenced at all outside of the block. And as for the infinite loop, that wont happen unless the file is infinitely long which is impossible.

Share this post


Link to post
Share on other sites

just try it by initializing the "fin" with null.as like fin=null

Initializing fin to null is no different than initializing it and not setting any value for it. Besides this topic has been resolved.

Well, fin was a file input stream, which cannot be given a value of null. I myself and just learning Java.

Well, fin is really just a class, and any class can be null, it just means it is an empty reference to a class with no data in it.

Share this post


Link to post
Share on other sites

You know, it's much simpler to use a Scanner. Here:import java.util.Scanner;Scanner s=new Scanner(new File("filenamehere.txt"));String temp;while(s.hasNext()){temp=s.next(); //gives you the next token(word)System.out.println(temp);}

Share this post


Link to post
Share on other sites

I think that the variable that was not initiated was i(maybe others too but specially this one), but as it was written before the easiest and practical way to get an input is using java.util.Scanner. As you may know java.util.* brings many helpful classes to avoid the programmer waste time in simple things.[tab][/tab]? As you said before that you where learning java, it would be a good thing if you review some of the java code of the classes that are inside java.util. Greetings.

Share this post


Link to post
Share on other sites

I recommend an extremely simple solution that may or may not work. Try this:

 

= null;

String filename = "";

 

_linenums:0'>/** Display a text fileTo use this program,specify the name of file that you want to see*/import java.io.*;class showfile{ public static void main(String args[])throws IOException { int i; FileInputStream fin <strong class='bbc'>= null</strong>; String filename = "";

Share this post


Link to post
Share on other sites

I recommend an extremely simple solution that may or may not work. Try this:

Please read all of the replies before posting. Here is my reply to someone who suggested the exact same fix:

Initializing fin to null is no different than initializing it and not setting any value for it. Besides this topic has been resolved.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.