0

So I'm trying to create a program that takes input in the form of a First and Last Name and then printing it to a Output.txt file. I'm sort of new to programming, and I think I'm derping on this.

I just keep getting an error on the last part of my program.

PrintInitials.java:21: error: <identifier> expected
} output.close();
              ^
1 error

Here's my Code

import java.util.Scanner;
import java.io.PrintWriter;
import java.io.Writer;
import java.io.*;

public class PrintInitials

{
   public static void main(String[] args)
   {
  Scanner stdIn = new Scanner(System.in);
  String first; // first name
  String last; // last name
  System.out.print("Enter your first and last name separated by a space: ");
  first = stdIn.next();
  last = stdIn.next();
  File file = new File("Output.txt");
  FileWriter writer = new FileWriter(file, true);
      PrintWriter output = new PrintWriter(writer);
      output.print("Your initials are " + first.charAt(0) + last.charAt(0) +     ".");
    } output.close();
} 
xCanaan
  • 125
  • 5

3 Answers3

1

do it like this:

    Scanner stdIn = new Scanner(System.in);

    System.out.print("Enter your first and last name separated by a space: ");
    String first = stdIn.next(); // first name
    String last = stdIn.next(); // last name

    stdIn.close();

    try (FileWriter writer = new FileWriter(new File("Output.txt"), true); // autocloseable
            PrintWriter output = new PrintWriter(writer)) { // autocloseable

        output.print("Your initials are " + first.charAt(0) + last.charAt(0) + ".");

    } catch (IOException e) {
        e.printStackTrace();
    }

The Writers will be closed automatically.

koem
  • 554
  • 4
  • 24
0

The problem is that before closing the stream, you are closing the method body. so the 'output.close();' is outside the method and into the class body.

Your new code should look like this:

import java.util.Scanner;
import java.io.PrintWriter;
import java.io.Writer;
import java.io.*;

public class PrintInitials{

public static void main(String[] args){
 try{
  Scanner stdIn = new Scanner(System.in);
  String first; // first name
  String last; // last name
  System.out.print("Enter your first and last name separated by a space: ");
  first = stdIn.next();
  last = stdIn.next();
  File file = new File("Output.txt");
  FileWriter writer = new FileWriter(file, true);
  PrintWriter output = new PrintWriter(writer);
  output.print("Your initials are " + first.charAt(0) + last.charAt(0) +     ".");
  output.close();
 }catch(IOException e){
    e.printStackTrace();
 }
   }
}

If you havent already, have a look into Java Basic Syntax and the docs of PrintWriter to check what exceptions are thrown by what method so you can either catch them and handle them like i did above, or just pass them on.

Also, using an IDE such as eclipse, will show you all syntax errors real-time as you code so you dont have to compile every time yourself to check if your syntax is correct. Also most IDE's often come with sugested solutions for perticular errors. Other than this, it will warn you about what method throws what exception so you can catch them.

fill͡pant͡
  • 1,147
  • 2
  • 12
  • 24
  • I just tried it and got a new error PrintInitials.java:18: error: unreported exception IOException; must be caught or declared to be thrown FileWriter writer = new FileWriter(file, true); ^ 1 error – xCanaan Feb 07 '16 at 08:02
-2

try this

BufferedWriter br= new BufferedWriter(new FileWriter(file)){


    br.write(first + " " + last);


}
Faisal
  • 32
  • 2
  • 7