I have a class file with my method to read for prime numbers in my text document, and then my main java file calls on it using the file input code. I am lost in my code, and not sure what is going wrong, because it just reads out 0 for each line read right now. The text document has 5 rows, with the numbers 3, 5, 7, 11, 13, so it should read out that each line is prime, not "0".

Here is my main

import java.util.Scanner;
import java.io.*;
//import TwoNumber.TwoNumber;
import OneNumber.OneNumber;

public class Ch5Lab {

	public static void main(String[] args) throws IOException
		OneNumber object1;
		//TwoNumber object2;
		int num1= 0;
		object1 = new OneNumber();
		//Creates scanner object for keyboard input
		Scanner keyboard = new Scanner(System.in);
		//Get the input filename
		System.out.print("Enter the filename to open: ");
		String filename = keyboard.nextLine();
		//Open the file;
		File file = new File(filename);
		Scanner inputFile = new Scanner(file);
		//Reads & checks for prime number
		String line = inputFile.nextLine();
		int n;
		n = Integer.parseInt(line);
		for(n = 1; n<=5; n++)
		//Close the file
		System.out.println("Data written to file.");

Here is my OneNumber class

package OneNumber;

public class OneNumber
	static int value;
	public OneNumber()
	public OneNumber(int value1)
		value = value1;
	public void setIsPrime(int value1)
		int num = 0;
		for (value1 = 2; value1 <= num; value1++)
			int check = 1;
			for(int b = 2; b < value1; b++)
			check = check * (value1 % b);
				System.out.print("Not Prime");
	public int getIsPrime()
		return value;

When reading from a file, always check for "hasNextLine()" before calling "nextLine()" method. Also, your method calls "object.setIsPrime()" and the line reading part "nextLine()" should come in a loop which will continue as long as there is a line that can be read. Also, your implementation of prime number checker seems to be off. Read the wikipedia article or many other articles around the internet on the subject matter and try again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.