4

I've just started on my college journey ( 'Yay' ). I'm also new to the site so feel free to lecture me on things I may have done wrong as far as asking questions is concerned.

I was given a project, which has already been graded and all, and the program should ==>> first read lines of standard input (Input file name using keyboard) and for each line of input, if the user enters exit, the application terminates; otherwise, the application interprets the line as a name of a text file. The application creates or recreates this file and writes to it two lines of output, the name of the file and the current date and time. The application then closes the file, reopens it for reading, and writes its contents to standard output. The application writes to standard output the name of the file enclosed by square brackets. After writing the file name, the application writes the contents of the file with each line prefixed by its corresponding line number, a full colon, and a space. I have worded it just as my professor did, so I apologize for any unclear statements. Here's what I got for it:

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.io.File;
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;

public class Project1
{
  public static void main() throws IOException
  {       
    String input = "";
    while(!sc.equals("exit"))
    {
        System.out.println("Enter file here!\n Type 'exit' to terminate");
        Scanner sc = new Scanner(System.in);
        input = sc.nextLine();        
        try
        {
            File file = new File (input,".txt"); // Creates pointer to a file.
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
            file.createNewFile();
            file.getAbsolutePath();
            printFileAndDate(file);
        }
        catch(IOException e)
        {
            System.out.print("Something wrong :(");
            e.printStackTrace();
        }
    }
  System.exit(0);
  }
  static void printFileAndDate(File temp)
  {   
     DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
     Calendar cal = Calendar.getInstance();
     System.out.println("[ " + temp.getPath() + " ]");
     System.out.println(dateFormat.format(cal.getTime()));
  }        
}

What I attempted to do there was the following:

-Get User Input => Save Input as a file => Call method "printFileAndDate" and print the file along with the current date and time in the correct format.

However, whenever I run it, it always gives me an exception error, which means the file was never really created or that it isn't able to find it.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Marco Neves
  • 141
  • 15

1 Answers1

1

The list of ISSUEs, I could find :

First, your main method signature is totally wrong

public static void main() throws IOException

change to

 public static void main(String[] args) throws IOException 

Second, it is not a good practice to throws exception inside main method.

The good practice is to use try catch block

Third, you have your Scanner varialbe after the while loop which does not make sense

 while(!sc.equals("exit"))
         {
        System.out.println("Enter file here!\n Type 'exit' to terminate");
        Scanner sc = new Scanner(System.in); <-?!!!!!!

change to

   System.out.println("Enter file here!\n Type 'exit' to terminate");
   Scanner sc = new Scanner(System.in); 
   while(!sc.equals("exit"))
         {

Fourth , you define File variable this way

 File file = new File (input,".txt"); <-- totally wrong

change to

File file = new File ("input.txt"); <-- if you use relative path 

Fifth there is not need for System.exit(0);at the end of main method

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58