11 Replies - 4158 Views - Last Post: 28 April 2009 - 07:51 PM Rate Topic: -----

#1 absynthe  Icon User is offline

  • DIC Tease
  • member icon

Reputation: 28
  • View blog
  • Posts: 2,807
  • Joined: 20-September 08

Prompt User For File

Posted 23 April 2009 - 09:48 AM

I'm trying to change this program to prompt the user for the file. The user enters the name of the text file, it reads it and rewrites it to another text file. My issue is getting it to prompt the user and it read the file after the user enters it. Ive got it to prompt but it does nothing after you enter the file name but throw this error:

Exception in thread "main" java.lang.NullPointerException
at Reader.main(Reader.java:46)

Here's the code:
// This program reads strings from a file input.txt and writes into the file 
// output.txt

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;


public class Reader {

		// file to be read from.
		//final String input = "Hello.txt","Goodbye.txt", "SeeYa,txt";
		// file to be written into.
		final String output = "C:/Users/Isabel/Desktop/Java Three/ThreadHomework/helloagain.txt";
		Scanner in; 
		BufferedWriter out; 
		
		public static void main(String[] args) throws IOException {
			// prompt user for filename:

			System.out.print("Enter filename: ");

			// in is a file stream to read from the console:

			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

			// get filename from console:

			String filename = in.readLine();
			
			Reader rw = new Reader(); 
			// Set up scanner to read from file. 
			//rw.in = new Scanner(new File(rw.input));
			
			// Set up BufferedWriter to write into a file
			rw.out = new BufferedWriter(new FileWriter(rw.output)); 
			
			
			// Loop through the input file reading each string and writing it into
			// the output file. 
			while (rw.in.hasNext()) {
				String temp = rw.in.next(); 
				rw.out.write(temp);
				rw.out.newLine(); // go to the next line after each output. 
			}
			// Close the output file
			rw.out.close(); 
			
		}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Prompt User For File

#2 Mikeyp926  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 205
  • Joined: 20-March 09

Re: Prompt User For File

Posted 23 April 2009 - 10:03 AM

It looks like you are reading in the file name ok, but to be sure you could put a System.out.println right after you get the filename and print it out to make sure it matches what you input.
However, if this is working fine, I think there might be some other problems. I will actually put this into eclipse later when I have time, but it seems like you aren't actually doing anything with the filename that the user input.
Hope this helps a bit, if this hasn't been resolved by the time I get back from class I'll try and help more then!
-Michael
Was This Post Helpful? 0
  • +
  • -

#3 absynthe  Icon User is offline

  • DIC Tease
  • member icon

Reputation: 28
  • View blog
  • Posts: 2,807
  • Joined: 20-September 08

Re: Prompt User For File

Posted 23 April 2009 - 10:44 AM

I think thats the problem but Im still lost. Ugh!
Was This Post Helpful? 0
  • +
  • -

#4 absynthe  Icon User is offline

  • DIC Tease
  • member icon

Reputation: 28
  • View blog
  • Posts: 2,807
  • Joined: 20-September 08

Re: Prompt User For File

Posted 23 April 2009 - 01:04 PM

Cool! You're back! Still lost :)
Was This Post Helpful? 0
  • +
  • -

#5 Mikeyp926  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 205
  • Joined: 20-March 09

Re: Prompt User For File

Posted 23 April 2009 - 01:19 PM

Alright, I think I fixed up the code a bit and it should work fine.
//This program reads strings from a file input.txt and writes into the file
//output.txt

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;


public class Reader {

	 // file to be written into.
	 final String output = "output.txt";
	 Scanner in;
	 BufferedWriter out;
	
	 public static void main(String[] args) throws IOException {
		 // prompt user for filename:

		 System.out.print("Enter filename: ");

		 // in is a file stream to read from the console:

		 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

		 // get filename from console:

		 String filename = in.readLine();
		
		 Reader rw = new Reader();
		 // Set up scanner to read from file.
		 rw.in = new Scanner(new File(filename));
		
		 // Set up BufferedWriter to write into a file
		 rw.out = new BufferedWriter(new FileWriter(rw.output));
		
		
		 // Loop through the input file reading each string and writing it into
		 // the output file.
		 while (rw.in.hasNext()) {
			 String temp = rw.in.next();
			 rw.out.write(temp);
			 rw.out.newLine(); // go to the next line after each output.
		 }
		 // Close the output file
		 rw.out.close();
		
	 }
}



Ok, so I uncommented that one line so that you actually initialize rw.in. Notice that in this line when you create the new File within the Scanner creation, you pass in the filename when you are creating the new File. This filename is the input that you took from the user. This way the new File that gets created is the one that the user specifies.
Other than that I didn't make many changes. I changed the file paths. The files I was using to test were in the same folder as the project. If this is the case then you don't need to type the absolute path, you can just type the filename. It's also fine the way you were doing it with the whole path, but remember that if you do it this way when you input the file name you will have to input the whole path.

Other than that this should work fine. Let me know if it works!
-Michael
Was This Post Helpful? 1
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Prompt User For File

Posted 23 April 2009 - 01:42 PM

The DIC HOTTIE is also a nerd??, how can this be :P
Was This Post Helpful? 0
  • +
  • -

#7 absynthe  Icon User is offline

  • DIC Tease
  • member icon

Reputation: 28
  • View blog
  • Posts: 2,807
  • Joined: 20-September 08

Re: Prompt User For File

Posted 23 April 2009 - 01:43 PM

Ha!! I am indeed!
Was This Post Helpful? 0
  • +
  • -

#8 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: Prompt User For File

Posted 23 April 2009 - 01:48 PM

lol mostlymyfriedman is too friendly hahaha
Was This Post Helpful? 0
  • +
  • -

#9 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Prompt User For File

Posted 23 April 2009 - 02:13 PM

Another approach you may want to use is a JFileChooser. Here is an example:
JFileChooser chooser = new JFileChooser();
   		File infile = null;
   		FileReader reader = null;
   		if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
   		{
   			infile = chooser.getSelectedFile();
   			reader = new FileReader(infile);
   		}
		Scanner input = new Scanner(infile);
		while (input.hasNextLine()){//all your code}

and if you choose this, dont forget to throws FileNotFoundExecption and dont forget to import
import javax.swing.JFileChooser;
import java.io.FileNotFOundException;

Hope this helps or enlighten you ;)
Was This Post Helpful? 0
  • +
  • -

#10 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Prompt User For File

Posted 23 April 2009 - 02:22 PM

@fuzzyness you really like that null don't you :lol:

but yeah. personally i would go with JFileChooser.

go for it izzie :P
Was This Post Helpful? 0
  • +
  • -

#11 absynthe  Icon User is offline

  • DIC Tease
  • member icon

Reputation: 28
  • View blog
  • Posts: 2,807
  • Joined: 20-September 08

Re: Prompt User For File

Posted 28 April 2009 - 12:41 PM

What about if I wanted to run multiple files with threads? How would I change my code?
Was This Post Helpful? 0
  • +
  • -

#12 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Prompt User For File

Posted 28 April 2009 - 07:51 PM

hmmm... try setting up a folder darlen and then import the folder instead of just the file.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1