-1

I'm reading a file with numbers checking if the number is a prime number then writing next to the prime numbers "is a prime" and printing that out to a different file, I keep getting:

Failed to open file in4.txt Exiting...

This is my code:

import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

public class CheckPrimes {

    public static void checkPrimes(String in_file, String out_file) {
        File temp = new File(in_file);
        Scanner input; 
        try 
        {
           input = new Scanner(temp);
        } 
        catch (Exception e) 
        {
            System.out.printf("Failed to open file %s\n", in_file);
            return;
        }
        while (true) 
        {
          for (int i = 2; i < input.nextInt(); i++) 
          {
           if (input.nextInt() % i != 0) 
           {
             try{
               PrintWriter output = new PrintWriter(out_file);
               output.print( input.nextInt() + " is prime");
               output.close();
                }
             catch(IOException ex)
             {
                 System.out.printf("Error : %s\n",ex);
             }

           }

          }
        }
  {
  public static void main(String[] args)
  {
    checkPrimes("in4.txt", "out4.txt");
    System.out.printf("Exiting...\n");
  }
  }
prosoitos
  • 4,676
  • 5
  • 19
  • 35
chanu
  • 33
  • 6
  • 5
    Check the message from the exception you are getting in your `catch()` block, it will tell you what the problem is – Progman Nov 17 '19 at 00:27
  • This might help: https://stackoverflow.com/questions/4716503/reading-a-plain-text-file-in-java – Mark Santos Nov 17 '19 at 00:46
  • 1
    This line of the code you posted - `input = new Scanner(temp);` - throws `FileNotFoundException` so change the `catch` to catch that exception. And add to the code in the `catch` block: `e.printStackTrace()`. Then [edit] your question and post the stack trace you get. – Abra Nov 17 '19 at 02:03

2 Answers2

-1

Longshot but might work since someone had that problem on this site yesterday. I referred them to this answer on a different topic where the File URL is formatted differently into a path that java seems to accept better that plaintext filepaths.

Sepx3
  • 28
  • 10
-1

For the error you are receiving (Failed to open file in4.txt), just make sure that the file you are reading is on the same file level as your JAR (or file if running in an IDE). Alternatively, you can run the createNewFile() function and edit the created function.

Image showing file level

(IntelliJ runs the file from the base of the project, hence why my files aren't where the class file is).

However, upon running the code myself, I was receiving this error: java.util.NoSuchElementException. I was able to correct this by switching from readInt() to readLine(), and having the in4.txt file structured as shown:

1
3
5
7
9

I believe readInt() not working versus readLine() is due to the problem presented in this problem. Also, be wary of calling readLine/readInt multiple times rather than assigning a variable per loop iteration because every call progresses the scanner (more info here).

Stephen
  • 306
  • 1
  • 3
  • 10