I'm seriously having problems with this. The problem is stated below.
Let the sum of the square of the digits of a positive integer S0 be represented by S1. In a similar way, let the sum of the squares of the digits of S1 be represented by S2 and so on. If Si = 1 for some i >= 1, then the original integer S0 is said to be Happy number. A number, which is not happy, is called Unhappy number. For example 7 is a Happy number since 7 -> 49 -> 97 -> 130 -> 10 -> 1 and 4 is an Unhappy number since 4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4.
Input
The input file, called test.dat, will consist of several
test cases, the number of which you are given in the first line of the input. Each test case consists of one line containing a single positive integer N.
Output
For each test case, you must print one of the following messages:
Case #p: N is a Happy number.
Case #p: N is an Unhappy number.
Here p stands for the case number (starting from 1).
You should print the first message if the number N is a happy number. Otherwise, print the second line.
===== Sample input data =============================================
4
7
4
13
5
===== End of input data / Start of corresponding output data ========
Case #1: 7 is a Happy number.
Case #2: 4 is an Unhappy number.
Case #3: 13 is a Happy number.
Case #4: 5 is an Unhappy number.
===== End of output data ============================================
After all that, I managed to create the following code...
// happy numbers by Justin Tom
import java.io.*; // to get file input
public class Happynumbers {
public static void main(
String args[]) {
try {
FileReader reader = new FileReader("test.dat");
// read from the file test.dat
BufferedReader comingin = new BufferedReader(reader); // begin reading
// defining variables
int n;
String input;
int casenumber = 1;
while ((input = comingin.readLine()) != null) {
// keep reading from test.dat line by line until there is nothing left to read
// note: incoming input is read line by line from test.dat as a string
n = Integer.parseInt(input); // converts string into integer
int sum = 0; // initialize the sum as zero when dealing with a number from the incoming data
int digit, x;
while (n>0)// loop as long as the original number is greater than zero
{
digit = n%10;// this will yield a single digit from the number
x = n/10;// divide the original number by 10 to remove the last digit
sum = sum + digit * digit;// the sum plus the square of its digit
}
x = sum;
if (sum == 1)
{ System.out.println("Case #" + casenumber + ": " + n + " is a Happy number."); // display the case number and the Happy number
casenumber++; // increment the casenumber
}
if (sum != 1 && casenumber != 1 )
{
System.out.println("Case #" + casenumber + ": " + n + " is an Unhappy number."); casenumber++; // display the case number and the Unhappy number
}
}}
catch (IOException e) // captures errors if any errors occur
{System.out.println(e); // display a message if an error occured
}}}
I tried putting in another while loop for taking the sum of the square of the digits but nothing will show up either. What did I do wrong? PLEASE FOR THE LOVE OF GOD HELP ME!!!