1
public static void main()     {
   String fileName = "cardNumbers.txt";
   String line = null;
   try {
       FileReader fileReader = new FileReader(fileName);
       BufferedReader bufferedReader = new BufferedReader(fileReader);
       while((line = bufferedReader.readLine()) != null)
       {
           CreditCard card = new CreditCard(line);
           if (card.creditCardType().equalsIgnoreCase("Unknown"))
           {
               System.out.println("Card number " + card.getCardNumber() + "is an unknown credit card type.");
            }
            else if (card.isValid())
            {
                System.out.println(card.creditCardType() + " number" + card.getCardNumber() + " is valid.");
            }
            else if (!card.isValid())
            {
                System.out.println(card.creditCardType() + " number " + card.getCardNumber() + " is not valid.");
            }
        }
    }
   catch (FileNotFoundException ex)
   {
       System.out.println("file not found exception thrown");
    }
    catch (IOException ex)
    {
        System.out.println("error while reading the file");
    }
    finally
    {
        System.exit(0);
    }
}

When I run this method it just says ProcessCardNumbers.main(); VM Terminated. Instead of actually printing out the content.

If I add a print at the very start of the function or in the finally block, they are printed.

Im not sure why this is happening or how I can fix it.

Turtle
  • 1,626
  • 16
  • 26
Will-i-am
  • 73
  • 6
  • 1
    Have you tried just calling a println at the very start to check if you're writing to the console you're looking at? You could also add a println in your finally: in your code, if your file is empty, it's the only thing that's gonna get executed. – Turtle Nov 30 '17 at 08:44
  • Yes, start by using more print statements. Beyond that: that finally clause isnt required. Your program is ending anyways. And beyond that: – GhostCat Nov 30 '17 at 08:50
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Use the "edit" link to improve your *question* - do not add more information via comments. Thanks! (we can't repro your issue, because we dont have your CreditCard class at hand --- from looking at this, there is no chance to spot bugs) – GhostCat Nov 30 '17 at 08:51
  • I added a println at the start and it showed up and so did the println I added to the finally. So it seems the while loop just isnt working? – Will-i-am Nov 30 '17 at 08:52
  • You can isolate the problem changing the code. Try changing all the "try" content for a single `System.out.println("TEST");`. Leave the `catch` and `finally` as they are. If `TEST` is not printed then the problem is with the standard output and the VM finalization (the exit). In that case I would try to flush the output and maybe sleeping for half a second before killing the VM or letting the VM die another way. If it is printed then you have another problem. Try debugging it. – aalku Nov 30 '17 at 10:10
  • Try changing the `system.exit(0);` to a simple print. Does your code now print an error? If yes, your problem probably comes from what aaklu said in his answer. If not, it probably comes from mine. – Turtle Nov 30 '17 at 11:41

4 Answers4

0

This seems that in your text file cardNumbers.txt has no data. When this program will execute within while loop bufferedReader.readLine()). will return null. So loop will terminate. After termination you have written System.exit(0); function in finally block which terminate JVM on the spot. So JVM is terminated now that's why you are not able to see anything after working of this code.

If you want to check working, write one SOP statement in finally block. Probably that will execute without termination of JVM.

0

As you told us that:

Adding a println at the start is printed

and

Adding a println in the finally works too

we can deduce that your code is working. It's just that when you reach while((line = bufferedReader.readLine()) != null), line stays null, so you never enter your while.

Why is that? Well, your file may be empty to begin with. If it is not, double-check the encoding of your file: it may not be using the proper returns symbols, hence not having a "completed line".

Turtle
  • 1,626
  • 16
  • 26
0

The problem here is not the bug in your code but the design problem that does not let you see the bug.

You are probably getting an undeclared exception (RuntimeException) and the VM can't print it because you kill it before in the finally.

You have several options:

  1. Remove the System.exit(0); and let it die normally. This may fail if there is another non-daemon thread running. You may try to stop it. You can, for example, cancel a Timer.

  2. Add a catch (RuntimeException e) { section before the finally and print the captured error. e.printStackTrace(); should do the trick.

With any of those you should see the exception on console so you can fix it.

aalku
  • 2,860
  • 2
  • 23
  • 44
-1

Your main method signature must look like this:

public static void main(String[] args)

instead of

public static void main()
JanTheGun
  • 2,165
  • 17
  • 25
  • I think he's running that method manually in BlueJ, not by `java ProcessCardNumbers`. In that case, the signature is ok. – Silly Freak Nov 30 '17 at 09:14
  • That will still work fine in Java, contrairly to C++. But it's a convention, and if he *had to* use it (for example if he was using a special framework), he would've a specific error (like `No main class definition found`) – Turtle Nov 30 '17 at 09:39