I am working on an assignment that calls upon a text file to read numbers line by line.

If line 1 number is 1, it looks at line 2 number (ex, line 2 = 13), and uses OneNumber class file to do various methods (such as prime or not prime).
If next line number is 2, it uses the TwoNumber class file to do various methods (such as finding the greatest divisible number).

I seem to have an issue implementing the methods into my main application code. It is reading the numbers 1 & 2, but not using the methods from OneNumber or TwoNumber class files.

The text file being used contains the following numbers:
1
13
1
6
8
2
7
1
3
2
11

So should read line 1, run OneNumber, and check for prime on 13. Then reads line 3, runs OneNumber, checks 6 for prime, etc.... When it hits 2, it should run TwoNumber and check for great common factor.

When compiled it just goes:
1
1
2 is not prime
1
2 is not prime
End of file

I understand its because of how I have written my code in the main app, so my problem is I do not know what I have done wrong or in what order I should be placing code when creating my new object or or or or.... (dumbfounded)!!!
Here are the three files:

Main app code:

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;
			int num2=0;
			
			//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);
			
			while (inputFile.hasNext())
				{
				int number = inputFile.nextInt();
					if (number == 1)
					{
						inputFile.hasNextInt();
						object1 = new OneNumber(number);
						OneNumber.isPrime(number);
						System.out.println(number);
						
					} else if (number == 2)
						{
						object2 = new TwoNumber();
							TwoNumber.greatestCommonFactor(num1, num2);
							System.out.println(number + " is not prime.");
						}
				}
			
			//Close the file
			inputFile.close();
			System.out.println("End of file.");
		}	

}

OneNumber class:

package OneNumber;

public class OneNumber
{	
	static int value;
	
	public OneNumber(int value1)
	{
		value = value1;
	}
	//checks whether an int is prime or not.
	
	public static boolean isPrime(int n)
			{
		    //check if n is a multiple of 2
		    if (n % 2 == 0) return false;
		    //if not, then just check the odds
		    	for(int i = 3; i * i <= n; i += 2)
		    		{
		    		if(n % i == 0)
		    			return false;
		    		}
		    		return true;
			}
}

TwoNumber class:

package TwoNumber;

public class TwoNumber 
{
	
	public static int greatestCommonFactor(int a, int b)
	{
		if (b == 0) 
		     return a;
		   else
		     return greatestCommonFactor(b, a % b);
	}

}

First: Are you sure that the text file is correct? After 6 is 8. What method should run? You have only two options if the number is 1 or 2, there is 8. Maybe I'm wrong and I don't understand something.

Two: After you check if the number is 1 or 2 (to know what method to call), when you call the method, what parameters you need?

After the digit 2, it will read in two numbers and run two different methods. I have now added each method so you can kinda see where this is going. Still not sure where my code is wrong in my main application though.

Methods for Class TwoNumber (to clear up any confusion)

• greatestCommonFactor – This method should find the largest number that can be divided into the two numbers read from the file.
• sumBetweenNumbers – This method should add all of the numbers between the two numbers read from the file (include the numbers read). For example: if the numbers were 5 and 9 then the method would add 5 + 6 + 7 + 8 + 9 to get 35.


Updating my class files, which must contain the following.

OneNumber, Two constructors: the default constructor and one constructor with one parameter:

package OneNumber;

public class OneNumber
{	
	static int value;
	
	public OneNumber()
	{
		value = 0;
	}
	
	public OneNumber(int value1)
	{
		value = value1;
	}
	//checks whether an int is prime or not.
	
	public static boolean isPrime()
			{
			int n;
			n = value;
		    //check if n is a multiple of 2
		    if (n % 2 == 0) return false;
		    //if not, then just check the odds
		    	for(int i = 3; i * i <= n; i += 2)
		    		{
		    		if(n % i == 0)
		    			return false;
		    		}
		    		return true;
			}
	
	public static int sumOfDigits()
	{
		int sum = 0;
		int n = value;
		  while (n != 0)
		  {
			  sum += n % 10;
              n /= 10;
		  }
		  return value;
	}
	
	public static boolean divisibleByNine()
	{
		for (int i = value; i >= 0; i += 9)
		{
			return false;
		} 
			return true;
	}
	
}

TwoNumber, Two constructors: the default constructor and one constructor with two parameters:

package TwoNumber;

public class TwoNumber 
{
	static int number1;
	static int number2;
	
	public TwoNumber()
	{
		number1 = 0;
		number2 = 0;
	}
	
	public TwoNumber(int a, int b)
	{
		number1 = a;
		number2 = b;
	}
	
	public static int greatestCommonFactor(int a, int b)
	{
		if (b == 0) 
		     return a;
		   else
		     return greatestCommonFactor(b, a % b);
	}
	
	public static int sumBetweenNumbers()
	{
		int sum;
		int a = number1;
		int b = number2;
	
		sum = 0;
			for(int i = 1; i <= 10; i++)
			{
			b = a%10;
			a = a/10;
			sum=sum + b;
			}
			return sum;
	}

}

and while were at it, here is my updated main app code. Currently it only reads out 1's and 2's instead of the numbers after the 1's and two's... It just skips them.

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;
			int num2=0;
			
			//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);
			
			while (inputFile.hasNext())
				{
				int number = inputFile.nextInt();
					if (number == 1)
					{
						inputFile.hasNextInt();
						object1 = new OneNumber(number);
						OneNumber.isPrime();
						System.out.println(number + " is prime.");
						OneNumber.sumOfDigits();
						System.out.println(number + " is the sum.");
						OneNumber.divisibleByNine();
						System.out.println(number + " is divisible by nine.");
						
					} else if (number == 2)
						{
						object2 = new TwoNumber(num1, num2);
							TwoNumber.greatestCommonFactor(num1, num2);
							System.out.println(number + " is the greatest common factor.");
							TwoNumber.sumBetweenNumbers();
							System.out.println(number + " is the sum between numbers.");
						}
				}
			
			//Close the file
			inputFile.close();
			System.out.println("End of file.");
		}	

}

Let's take this code first:

int number = inputFile.nextInt();
					if (number == 1)
					{
						inputFile.hasNextInt();

						object1 = new OneNumber(number);
						OneNumber.isPrime();
						System.out.println(number + " is prime.");
						OneNumber.sumOfDigits();
						System.out.println(number + " is the sum.");
						OneNumber.divisibleByNine();
						System.out.println(number + " is divisible by nine.");
						
					}

what your code do?
This inputFile.hasNextInt(); returns true. Here you just ask the program is hasNextInt? You don't need this line cause if returns false you don't change anything.

Next line object1 = new OneNumber(number); . What is the value of number?
Right! Is 1, but we need here the second(next) value from our file, which is 13. So here you should create another int variable and assign the next value.

You code will always print that the number is prime and the sum and that is divisible.
What this method returns? OneNumber.isPrime(); Right! True or false. Use an if statement like this:
if (myMethod) print "prime"
else "not prime"

When I try the isPrime method as a boolean, it only returns true or false in my main. When I change it to a int, it returns 0.???

public int isPrime()
			{
			int n;
			n = value;
		    //check if n is a multiple of 2
		    if (n % 2 == 0) return n;
		    //if not, then just check the odds
		    	for(int i = 3; i * i <= n; i += 2)
		    		{
		    		if(n % i == 0)
		    			return n;
		    		}
		    		return value;
			}

OR AS BOOLEAN

public boolean isPrime()
			{
			int n;
			n = value;
		    //check if n is a multiple of 2
		    if (n % 2 == 0) return false;
		    //if not, then just check the odds
		    	for(int i = 3; i * i <= n; i += 2)
		    		{
		    		if(n % i == 0)
		    			return false;
		    		}
		    		return true;
			}

here is my new 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 oneNumInput;
			int num1=0;
			int num2=0;
			
			//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);
			
			while (inputFile.hasNext())
				{
				int number = inputFile.nextInt();
					if (number == 1)
					{
						object1 = new OneNumber();
						oneNumInput = number;
						if (object1.isPrime())
							System.out.println(object1.isPrime() + " is prime.");
						else
							System.out.println(object1.isPrime() + " is not prime.");
						
						object1.sumOfDigits();
						System.out.println(object1.sumOfDigits() + " is the sum.");
						
						if (object1.divisibleByNine())
						System.out.println(object1.divisibleByNine() + " is divisible by nine.");
						else
							System.out.println(object1.divisibleByNine() + " is not divisible by nine.");
					} else if (number == 2)
						{
							int number1 = inputFile.nextInt();
							int number2 = inputFile.nextInt();
							object2 = new TwoNumber(num1, num2);
							number1 = num1;
							number2 = num2;
							object2.greatestCommonFactor(number1, number2);
							System.out.println(object2.greatestCommonFactor(number1, number2) + " is the greatest common factor.");
							object2.sumBetweenNumbers();
							System.out.println(object2.sumBetweenNumbers() + " is the sum between numbers.");
						}
				}
			
			//Close the file
			inputFile.close();
			System.out.println("End of file.");
		}	

}

This says:
false is not prime
0 is sum
false is not divisible by nine
etc...

You can use the methods whatever you want.

int number = inputFile.nextInt();
if (number == 1)
{
 inputFile.hasNextInt();            // true, but why do you put this line?
//object1 = new OneNumber(number);     // here you call the prime method, 
                                            //but for number, which is 1
 object1 = new OneNumber(inputFile.nextInt());   //prime method for the next 
                                                  //value, 13     
OneNumber.isPrime();
System.out.println(number + " is prime.");
}

Use a white paper and a pen and go step by step in your program, to find out how it works.
good luck

So My methods are functional, as I tested them in seperate projects to verify they worked. My issue now is getting the methods to read in two consecutive integers from a text file to run the two methods, being: sum between numbers, and greatest common factor. It should read a 2 in the tex file, and then know to read the next two integers in order to run the method. Right now it returns random numbers like it is not reading both integers in the text file in order to process through my methods correctly... here are the main and 2nd class file:

MAIN

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import OneNumber.OneNumber;
import TwoNumber.TwoNumber;

	public class Ch5Lab {

		public static void main(String[] args) throws IOException
		{			
			OneNumber object1;
			TwoNumber object2;
			
			int num1=0;
			int num2=0;
			
			//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);
			
			while (inputFile.hasNext())
				{
				int number = inputFile.nextInt();
					if (number == 1)
					{
						inputFile.hasNextInt();
						object1 = new OneNumber(inputFile.nextInt());
						if(object1.isPrime())
						{
							System.out.println(object1.getIsPrime() + ", number is prime");
						} else
							System.out.println(object1.getIsPrime() + ", number is not prime");
						
						System.out.println(object1.getSumOfDigits() + " is the sum. ");
						
						if (object1.divisibleByNine())
						System.out.println(object1.getDivisibleByNine() + ", number is divisible by nine.\n");
						else
							System.out.println(object1.getDivisibleByNine() + ", number is not divisible by nine.\n");
					} else if (number == 2)
						{
							inputFile.hasNextInt();
							object2 = new TwoNumber();
							num1 = inputFile.nextInt();
							
							object2 = new TwoNumber();
							num2 = inputFile.nextInt();
							
							System.out.println(object2.getGreatestCommonFactor() + " is the greatest common factor.");
							System.out.println(object2.getSumBetweenNumbers() + " is the sum between numbers.");
						}
				}
			
			//Close the file
			inputFile.close();
			System.out.println("End of file.");
		}	

}
package TwoNumber;

public class TwoNumber 
{
	static int a;
	static int b;
	
	public TwoNumber()
	{
		a = 0;
		b = 0;
	}
	
	public TwoNumber(int number1, int number2)
	{
		number1 = a;
		number2 = b;
	}
	
	public int greatestCommonFactor()
	{
			int n;
				{
				for (n=1; n<3; n++)
					while (b == 0 && b > a) 
					{
						n = b%a;
						n = a;
						return n;
					}
					while (b != 0 && b > a)
					{ 
						n = b%a;
						a = b;
						b = n;
						return n;
					}
					while (a != 0 && a > b)
					{ 
						n = a%b;
						a = b;
						b = n;
						return n;
					}
					while (a != 0 && a > b)
					{ 
						n = a%b;
						a = b;
						b = n;
						return n;
					}
					n = -1;
						return n;
				}
		}
	
	public int sumBetweenNumbers()
	{
		int sum;
		sum = 0;
			for(int i = 1; i <= 10; i++)
			{
			b = a%10;
			a = a/10;
			sum=sum + b;
			}
			return sum;
	}
	
	public int getGreatestCommonFactor()
	{
		return greatestCommonFactor();
	}
	public int getSumBetweenNumbers()
	{
		return sumBetweenNumbers();
	}

}

to be more specific, how should the code by done when reading in two integers on two lines in this section of code:

else if (number == 2)
						{
							inputFile.hasNextInt();
							object2 = new TwoNumber();
							num1 = inputFile.nextInt();
							
							object2 = new TwoNumber();
							num2 = inputFile.nextInt();
							
							System.out.println(object2.getGreatestCommonFactor() + " is the greatest common factor.");
							System.out.println(object2.getSumBetweenNumbers() + " is the sum between numbers.");
						}
This article has been dead for over six months. Start a new discussion instead.