The Artima Developer Community
Sponsored Link

Java Answers Forum
Having trouble with a program...

6 replies on 1 page. Most recent reply: Dec 5, 2006 8:03 AM by Laura Leach

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Laura Leach

Posts: 6
Nickname: lyra
Registered: Dec, 2006

Having trouble with a program... Posted: Dec 5, 2006 7:19 AM
Reply to this message Reply
Advertisement
I'm trying to finish the following program. It is supposed to read a number from a data file and find whether the number is perfect, deficient, or abundant. The proper divisors are supposed to be included in the output and tha data file must have 10 numbers.

// Lleachl3.java

import java.io.*;

public class Lleachl3
{
public static void main(String args[]) throws IOException
{

// *************************** Declaration Section *****************************

int num;
int sum;


// ------------------------- Open Data file to read ---------------------------

String file = "dataL3.dat";
FileReader fr = new FileReader(file);
BufferedReader Input = new BufferedReader(fr);

// *********************** Calculation/ Input Section *************************

for(int k=1; k<11; ++k)
{
num = Integer.parseInt(Input.readLine());

// ***************************** Display Section *******************************

sum = 0;
System.out.print(num);
for (int x = 1; x < num; x++)
{
if (num is divisible by x)
{
System.out.print(Lutil.format(x,14));
x is added to sum;
}

}
System.out.print(Lutil.s(16));
if (sum = num)
{
System.out.print("Perfect");
}
else if (sum < num)
{
System.out.print("Deficient");
}
else System.out.print ("Abundant");

Lutil.s();
Lutil.l();
}

} // ******************** End of Void Main *******************************

/*
* ************************* Helper Methods Go Here ****************************
*/

public static void Title_Section()
{
Lutil.l();
System.out.print(Lutil.s(9) + " ");
}

public static void Output_Information()
{
}

}// ************************* End of class AreaofObject ************************


Rajeev Mutalik

Posts: 57
Nickname: rajmutalik
Registered: Sep, 2002

Re: Having trouble with a program... Posted: Dec 5, 2006 7:41 AM
Reply to this message Reply
Can u pl tell whats the problem u r facing? Bcoz, if u can pin-point the problem, then someone can try to answer that.

Rajeev

Laura Leach

Posts: 6
Nickname: lyra
Registered: Dec, 2006

Re: Having trouble with a program... Posted: Dec 5, 2006 7:44 AM
Reply to this message Reply
Yes, I need figure out how to state that "num" is divisible by "x" with no remainder and that "x" is being added to "sum".

Rajeev Mutalik

Posts: 57
Nickname: rajmutalik
Registered: Sep, 2002

Re: Having trouble with a program... Posted: Dec 5, 2006 7:54 AM
Reply to this message Reply
for this part of code:
if (num is divisible by x) { System.out.print(Lutil.format(x,14)); x is added to sum; }

solution is:

if (num % x == 0)
{
System.out.print(Lutil.format(x,14));
sum += num;
}

I am not aware whats this Lutil.format is doin.

Rajeev

Rajeev Mutalik

Posts: 57
Nickname: rajmutalik
Registered: Sep, 2002

Re: Having trouble with a program... Posted: Dec 5, 2006 8:01 AM
Reply to this message Reply
Sorry, in the previous posting instead of sum += num; u can use sum += x; as u want to add x and not num.

Rajeev

Laura Leach

Posts: 6
Nickname: lyra
Registered: Dec, 2006

Re: Having trouble with a program... Posted: Dec 5, 2006 8:02 AM
Reply to this message Reply
Oh, Lutil is a file that my computer teacher has us use to format our programs.

Thanks for your help. It's still not working yet, but it's closer (it's saying all the numbers are abundant). Guess I'll just have to play around with it a bit.

-L

Laura Leach

Posts: 6
Nickname: lyra
Registered: Dec, 2006

Re: Having trouble with a program... Posted: Dec 5, 2006 8:03 AM
Reply to this message Reply
Oh. Okay. Thanks. That works better.

-L

Flat View: This topic has 6 replies on 1 page
Topic: Having trouble with a program... Previous Topic   Next Topic Topic: I have a program

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use