Java Fixing Syntax Issues with Java File Inputs

AI Thread Summary
A user developed a Java program that searches files for key terms using the Scanner class but encountered issues with handling file names that require quotes for proper syntax. The initial approach involved reading the filename from user input, but quotes around the filename caused problems. The user sought a solution to avoid treating the filename as a string literal. A workaround was found by concatenating quotes around the user input when creating the File object, allowing the program to function correctly. The final solution involved reading the filename, wrapping it in quotes, and then creating a File object, which resolved the issue.
TMM
Messages
92
Reaction score
0
So I made a program that finds files and searches in them for key terms using the scanner class in java.util.*;

I've written it something like this:
Scanner filereader = new Scanner(System.in);
String filename = filereader.nextLine();
Scanner searcher = new Scanner(new File(filename));

The problem is with the filename argument. I need quotes around it to fix the syntax, but even when my input contains quotes it doesn't quite work, and when I add them in the program itself they turn my variable, filename, into a string literal. Is there a way to get around this (some type of escape code perhaps?) or a different method altogether? Any help is much appreciated.
 
Technology news on Phys.org
If you do this:

String filename = "file.dat";
File f = File(filename);

then you have created a string literal, stored it in a variable, and passed it to the constructor of File. You can also create a File object by passing it a string literal directly, like this:

File("file.dat")

- Warren
 
Ah I see. Thanks. :D

I got it working with:
Scanner filename = new Scanner(System.in);
String s = filename.nextLine();
File f = new File('"' + s + '"');
Scanner reader = new Scanner(f);
 
Last edited:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
2
Views
2K
Replies
3
Views
3K
Replies
10
Views
1K
Replies
3
Views
2K
Replies
2
Views
2K
Replies
4
Views
3K
Back
Top