The user can select a file to be scanned, however none of the file's contents are printed at run-time, any help?
public void readVehicleData(){
FileDialog fileBox = new FileDialog(mainWindow,"Open", FileDialog.LOAD);
fileBox.setVisible(true);
fileBox.setDirectory(".");
String dataFile = fileBox.getFile();
Scanner scanner = new Scanner(dataFile);
while( scanner.hasNext() )
{
String lineOfInput = scanner.nextLine();
System.out.println(lineOfInput);
}
scanner.close();
}
Use the constructor that accepts a File rather than a String as its InputStream source
Scanner scanner = new Scanner(new File(dataFile));
Related
I have a txt file of names and genders that I imported for this java program. The scanners are supposed take a user input (name and gender) and compare it line by line to find it within the text file and then print the line at which the name was found. However, only some names work and not others. I think it may be because the program only reads every other line but im not sure if thats the problem, or how to fix it.
Link to the name file: http://courses.cs.washington.edu/courses/cse142/16au/homework/names.txt
public static void fileSearch() throws FileNotFoundException {
System.out.println("What name are you looking for?");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
String gender = scan.nextLine();
File file = new File("names.txt");
Scanner fileScan = new Scanner(file);
while (fileScan.hasNextLine()) {
String line = fileScan.nextLine();
Scanner lineScan = new Scanner(line);
String nameText = lineScan.next();
String genderText = lineScan.next();
if (name.equalsIgnoreCase(nameText) && gender.equalsIgnoreCase(genderText)) {
System.out.println(line);
}
}
}
}
I have this code
public User createnewproflie() throws IOException
{
FileWriter fwriter = new FileWriter("users.txt",true); //creates new obj that permits to append text to existing file
PrintWriter userfile = new PrintWriter(fwriter); //creates new obj that prints appending to file as the arg of the obj is a pointer(?) to the obj that permits to append
String filename= "users.txt";
Scanner userFile = new Scanner(filename); //creates new obj that reads from file
User usr=new User(); //creates new user istance
String usrname = JOptionPane.showInputDialog("Please enter user name: "); //acquires usrname
userfile.println("USER: "+usrname+"\nHIGHSCORE: 0\nLASTPLAY: 0"); //writes usrname in file
userfile.flush();
usr.setName(usrname); //gives usr the selected usname
return usr;
}
and it doesn't output on the file... can someone help please?
i knew that flush would output all of the buffered text but it doesn't seem to work for some strange reason...
You can use a String with a FileWriter but a Scanner(String) produces values scanned from the specified string (not from a File). Pass a File to the Scanner constructor (and it's a good idea to pass the same File to your FileWriter). And you need to close() it before you can read it; maybe with a try-with-resources
File f = new File("users.txt");
try (FileWriter fwriter = new FileWriter(f,true);
PrintWriter userfile = new PrintWriter(fwriter);) {
// ... Write stuff to userfile
} catch (Exception e) {
e.printStackTrace();
}
Scanner userFile = new Scanner(f);
Finally, I usually prefer something like File f = new File(System.getProperty("user.home"), "users.txt"); so that the file is saved in the user home directory.
Im trying to write a program that will ask for the name of an input file and an output file. It will open the input file and create the output file. It will then read the input file and make a double-spaced copy of the input in the output file.
public class ProgramTest
public static void main (String[] args )
{
Scanner keyboard = new Scanner(System.in);
System.out.println("where to read?");
String in = keyboard.nextLine();
System.out.println("where to write?");
String out = keyboard.nextLine();
Scanner scanner = new Scanner(new File(in));
PrintWriter outputFile = new PrintWriter(out);
}
Thats what I have so far. What I dont know is how to make it do the last part to read the input file and make a double-spaced copy of the input in the output file.
well you can start by reading in the file?
private static void readFile(String inputFile) {
File file = new File(inputFile);
try {
Scanner scan = new Scanner(file);
//for example String s = scan.next(); would store next word
//doulbe d = scan.nextDouble(); would grab next double
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Once you read in the file sore the lines/numbers into variables?? Should not be to hard work with the read file i provided. Also remember what should you read in first ints or strings?
The program takes in one file at a time from the command line and executes it.
Scanner scan = new Scanner(System.in);
while(scan.hasNextLine())
{
fileName = scan.nextLine();
File xmlFile = new File(fileName);
// Do SOMETHING with xmlFile
}
Basically I want to take a list of files from the commandlines unless the user does CTRL+D.
How do i change it?
An alternative to using a Scanner would be to use a stream:
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
Stream lines = br.lines();
Consumer processFile = new Consumer() {
public void accept(Object o) {
File xmlFile = new File(o.toString());
// Do SOMETHING with xmlFile
}
};
lines.forEach(processFile);
Ctrl+D is end-of-stream so it'll just take you out of the loop.
List<String> fileList = new ArrayList<String>();
Scanner scan = new Scanner(System.in);
while(scan.hasNextLine())
{
line = scan.nextLine();
if(line.equals("-1"){
break;
}
fileList.add(line);
}
for(String file: fileList){
File xmlFile = new File(file);
//process
}
Else keep doing as per your original code and use CTRL+C to exit the VM.
Pass files as argument like below :
java XYZClass file1.csv file2.csv
and access that in your code like below ;
URL url = getClass().getResource(args[0]);
File myFile = new File(url.getPath());
InputStream input = new FileInputStream(myFile);
Im trying to read N different CSV files containing stock price data. I want to extract one particular column from each file and showcase those columns in a single CSV file.
The issue is the combined file contains only the written data from the first file I give as input i.e. that data is not being overwritten in the iteration of my loop.
Can someone help? Or suggest a new method?
public static void main(String[] args) throws IOException
{
int filecount=0;
System.out.println("Enter Number of Files");
Scanner stream =new Scanner(new InputStreamReader(System.in));
filecount= Integer.parseInt(stream.next());
File file2 = new File("Combined_Sym.csv");
FileWriter fwriter= new FileWriter("Combined_Sym.csv",true);
PrintWriter outputFile= new PrintWriter(fwriter);
int i;
for(i=0;i<filecount;i++)
{
System.out.println("Enter File name "+i);
String fileName =stream.next();
File file = new File(fileName);
Scanner inputStream = new Scanner(file);
Scanner inputStream2= new Scanner(file2);
if(!inputStream2.hasNext()){
outputFile.println(fileName);
}
else
{ String header=inputStream2.next();
System.out.println(header+","+fileName);
outputFile.println(header+","+fileName);
}
while(inputStream.hasNext())
{
String data= inputStream.next();
String[] values = new String[8];
values = data.split(",");
String sym=values[7];
if(!inputStream2.hasNext())
outputFile.println(sym);
else
{
String data2= inputStream2.next();
outputFile.println(data2+","+sym);
System.out.println(data2+","+sym);
}
}
inputStream.close();
inputStream2.close();
outputFile.close();
}
}
}
Can you try changing :
for(i=0;i<filecount;i++)
{
System.out.println("Enter File name "+i);
String fileName =stream.next();
File file = new File(fileName);
to
File file;
for(i=0;i<filecount;i++)
{
System.out.println("Enter File name "+i);
String fileName =stream.next();
file = new File(fileName);