Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;

public class Test {
	
	public static void main ( String[] args) {
		
		Scanner input = new Scanner(System.in);
		System.out.println("Enter name of input file: ");
		String fileName = input.nextLine();
	
		
		String text = " ";
		String line = fileName.readLine();
		while ( line != null)
		{
			text += line ;
			line = fileName.readLine();
		}
		
		System.out.print(text);
	}

}


What I have tried:

it says String doesn't have function called readLine()
Posted
Updated 1-Dec-17 11:45am

1 solution

That's because "filename" is defined as a String. You don't do a file operation on a filename. In your case, you use the filename to open a FileInputStream. You then can call readline() on that stream.

Go through this tutorial[^] because you're going to have to rewrite your code a little bit.
 
Share this answer
 
Comments
PIEBALDconsult 1-Dec-17 18:49pm    
"You don't do a file operation on a filename."

Except with an Extension Method in C#. :cool:
Dave Kreskowiak 1-Dec-17 19:46pm    
+10 points for you! I like the concept. A little work up front makes being lazy easier down the road!
PIEBALDconsult 2-Dec-17 0:32am    
I've been writing Extension Methods for ADO.net ... such as

@"<SqlServer Server='localhost' Database='master' />".ExecuteReader ( "SELECT ..." ... )
Dave Kreskowiak 2-Dec-17 10:58am    
Interesting. Save a tons of boilerplate work.

You're passing in XML? Other options in that XML?
PIEBALDconsult 2-Dec-17 11:18am    
At work I access a dozen or more databases of various flavors -- SQL Server, Oracle, Teradata, MySQL, etc.
I use XML because then I can have a config file with many such connections defined and I can refer to them by name.
The Attributes are as appropriate for the particular ADO.net Provider. Plus a Name Attribute is supported by the code that reads the config file into a Dictionary.
The parsing of the XML is handled by Provider-specific Factory classes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900