0

hi I have this Java code,

import java.io.*;
import java.util.*;
public class SongWriter  
{ 
   public static void main(String[] args)  
   {
     PrintWriter outputStream = null;  // Scope must be outside the try/catch structure
     try  
     { 
        outputStream = new PrintWriter("Song.txt");  // new
        FileOutputStream("Song.txt")  
     } 
     catch(FileNotFoundException e) 
     { 
        System.out.println("Error opening the file Song.txt."); 
        System.exit(0);  
     } 
     System.out.println("\n classical songs has many lines");
     System.out.println("\nNow enter the three lines of your Song.");
     String line = null;
     Scanner keyboard = new Scanner(System.in);  
     int count; 
     for (count = 1; count <= 3; count++) 
     {
        System.out.println("\nEnter line " + count + ": ");
        line = keyboard.nextLine(); 
        outputStream.println(count + "\t" + line);  
     } 
     outputStream.close();   
     System.out.println("\nYour Song has been written to the file Song.txt.\n");
    } // end of main  
} // end of class

how do I Adjust the program so it first asks for a name of the file to write to. Use the Scanner class and its next() method. Read in the file name as a string variable after informing the reader the file name should end in the suffix .txt Eg:- Song with the file names Haiku1.txt, Haiku2.txt and Haiku3.txt.

Mike Koch
  • 1,540
  • 2
  • 17
  • 23
Sameena
  • 93
  • 6
  • It would be very helpful if you added spaces to make this more readable – Deena Jan 23 '14 at 01:14
  • Have you read the Javadocs for the Scanner class? – Dawood ibn Kareem Jan 23 '14 at 01:14
  • @Deena Hi Sorry, I fixed it. – Sameena Jan 23 '14 at 01:16
  • @Deena If u execute the code, u will be able to enter your song lyrics in line 1 ,2 and 3. And when you enter the song, a song.txt will be created in your folder with the entered lyrics. But if you execute the program again and enter different song lyrics, your previous song lyrics will be erased and replaced by the new ones. So im assuming the question is asking a way to have the song lyrics saved in Haiku1.txt, Haiku2.txt, Haiku3.txt in order to prevent from getting it replaced. Im sorry if im not doing a good job explaining the question. – Sameena Jan 23 '14 at 01:28

2 Answers2

0

You almost had it.

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter first file name:");
String first = keyboard.nextLine();
System.out.println("Enter second file name:");
String second = keyboard.nextLine();
System.out.println("Enter third file name:");
String third = keyboard.nextLine();
//and so on and continue whatever you want to do..

EDIT: After your comment.

First store the 3 lines in a StringBuilder and then ask for the file name to write. Now you have the lyrics and file name.

RP-
  • 5,827
  • 2
  • 27
  • 46
  • What do you by Store the 3 lines in a stringbuilder ? – Sameena Jan 23 '14 at 01:53
  • I understood that first your program asks for lyrics and then file name to write those lyrics into. You need to keep on appending the lyrics to a `StringBuilder` and then you will ask for the file name and write that `StringBuilder` to the file. – RP- Jan 23 '14 at 04:11
0

Using the Scanner class to get input from the user:

String fileName1;
Scanner keyboard = new Scanner(System.in); //creates Scanner object
System.out.print ("Enter the name of the file. The file should end in the suffix .txt") //prompt the user to enter the file name
fileName1 = keyboard.next(); //store the name of the file

You should do this before the try/catch block so that you can use the filename that the user entered instead hardcoding it (like you did here with song.txt).

You can prompt the user this way for as many file names as you need.

Deena
  • 343
  • 1
  • 11
  • fileName1 will be the name of my file that will be created ? So when I execute the code, im getting Enter the name of the file? Can you please explain what happening here? Sorry Im kinda new to java – Sameena Jan 23 '14 at 01:51
  • The program will display a request to the user to enter the name of the file with the .txt extension. You can use the .next() method of the Scanner class to store that in the variable fileName1 (Or whatever you call the variable). Then, instead of hardcoding song.txt as the name of the file, you can use the variable where you stored the file name. – Deena Jan 23 '14 at 02:00
  • Do i also need a outputStream = new PrintWriter("Haiku.txt"); for Haiku1.txt and Haiku2.txt ? – Sameena Jan 23 '14 at 02:16
  • Are you going to be writing to all three text files at the same time, or are you going to run the program three different times and write to a different text file every time? – Deena Jan 23 '14 at 02:20
  • Run the program three different time i guess.I copy pasted what you showed. and this is what I got so far. import java.io.*; import java.util.*; public class SongWriter { public static void main(String[] args) { String fileName1; Scanner keyboard=new Scanner(System.in); System.out.print("Enter filename with extension"); fileName1 = keyboard.next(); PrintWriter outputStream = null; // Scope must be outside the try/catch structure. try { outputStream = new PrintWriter("Song.txt"); // new FileOutputStream("Song.txt") } catch(FileNotFoundException e) { .....etc etc – Sameena Jan 23 '14 at 02:28
  • If you are going to run the program three different times, then you will only need to specify one text file each time. The first time you run the program, the user will enter "Haiku1.txt", and you will enter this as your argument. The second time you run the program, the user will enter "Haiku2.txt", and so on – Deena Jan 23 '14 at 03:03
  • ok so you mean when I get the "Enter filename with extension" prompt? I should type Haiku1? and then continue typing my lines? and then I will be able to see a file name Haiku1 created? – Sameena Jan 23 '14 at 03:21
  • If I understand you correctly (and I'm not sure that I am), this is what should happen. You will run the program, and when prompted, enter "haiku1.txt". This program will run and write to the file called "haiku1.txt". After the program finishes, you will run it again, and this time when prompted, you will enter "haiku2.txt" – Deena Jan 23 '14 at 03:23
  • Yes, but that doesnt happen. When I type haiku1 when prompted, it jumps to the second line without letting me enter anything on the first line. And then after the program has run, a file named "Haiku" is created with the 2nd and 3rd line. Somehow a haiku1.txt is not created. When i re-run the program with Haiku2 when prompted, everything in haiku.txt gets replaced by the newly entered letters. – Sameena Jan 23 '14 at 03:27
  • Make sure that you are correctly writing to the file. [This link should help](http://stackoverflow.com/questions/2885173/java-how-to-create-and-write-to-a-file) – Deena Jan 23 '14 at 03:40