prime factor problem
hey guys I got another java problem (go figure).
anyway heres the basic steps
1. take as input the path to a file containing one integer per line
2.for each integer in the file, output to the console a comma-delimited list of the integer's prime factors
3.the list of integers on each line of the output should multiply to produce the input integer
4.include unit tests for the code
basically im kind of at a loss of where to start. like is the first 1 asking me to ask for just one integer or as many as I put down?
The first one is asking for a path to a file, like:C:\numbers.txtThe file should have one integer on each line.
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
while(line != null)
System.out.println(factor(line));
line = reader.readLine();
}
then
private String factor(String aString){
Integer myInt = //cast the String to an Integer however that's done
Vector myVect = new Vector();
fact(myVect, myInt);
String aString = new String(myVect.get(0));
for(int counter = 1; counter < myVect.size(); counter++)
aString += myVect.get(counter);
return aString;
}
private void fact(Vector aVector, Integer myInt){
for(int x = 2; x < //square root of myInt// ; x++){
if (myInt%x == 0){
myVector.add(new Integer(x));
myInt = myInt/x;
fact(myVector, myInt);
}
return myVector;
}
I can just ask the user to input a set of numbers I put up right? like its not asking me to input every number is it?
I don't understand that question
oh sry guess I typed it to fast. Well what I want to know is does the program want the user to be able to input any number? or can I apply a set of numbers say 1-20 and ask the user for one of those>
well it will work for any number but if you only want to make it work for 1-20 its not that hard
hey guys heres what i got
public class primefactor
{
public primefactor()
{
}
public static void main(String[] args)
{
primefactor factorEx = new primefactor();
factorEx.FindAndPrint(50);
}
public void FindAndPrint(int n)
{
int totalCols=8;
for (int i = 1; i <= n; i++)
{
int countFactor = 0;
for (int j = 2; j <= (i / 2); j++)
{
if ((i % j) == 0)
{
countFactor++;
}
}
for (int j = 0; j < totalCols; j++)
{
if (j == countFactor)
{
System.out.print(i);
} else
{
System.out.print(".");
}
}
System.out.println();
}
}
}
but when i run it nothing happens? something im missing?
if (j <= countFactor)System.out.print(i + ", ");try this inside your last for loop
well i changed that and tried to compile it but then it gave me this message. Class names 'primefactor' are only accepted if annonation processing is explicitly requested. heres the code.
public class primefactor
{
public primefactor()
{
}
public static void main(String[] args)
{
primefactor factorEx = new primefactor();
factorEx.FindAndPrint(50);
}
public void FindAndPrint(int n)
{
int totalCols=8;
for (int i = 1; i <= n; i++)
{
int countFactor = 0;
for (int j = 2; j <= (i / 2); j++)
{
if ((i % j) == 0)
{
countFactor++;
}
}
for (int j = 0; j < totalCols; j++)
{
if (j <= countFactor)
{
System.out.print(i+",");
} else
{
System.out.print(".");
}
}
System.out.println();
}
}
}
still no reply just wondering if this is an easy to fix error?
Your code compiles just fine for me.
ok sorry it compiles now but it doesnt run. it says it has no class definition in the main method or somethin.
What are you using to compile and run the code? Also, could you please post the actual error message instead of what you think it is?
> it says it has no class definition in the main method or somethin.
"or somethin"?
Check the command you are using to run the program. Reply 10 suggested that you were using the wrong command when specifying the java file to compile. It now looks like you are now not specifying the class to run properly.
it compiles it just doesnt run anyway how do i copy the error message if i am doing this inthe command prompt
Right click on it and select "Mark". Highlight what you want to copy and press the enter key to copy it.
thank u. when i run the program i get this messageC:\>java primefactorException in thread "main" java.lang.NoClassDefFoundError: primefactor
http://java.sun.com/docs/books/tutorial/getStarted/problems/index.html
**** now it runs but doesnt return proper results
instead of giving me the factors of the numbers all it does is repeat the number.
26,26,26,.....
27,27,27,.....
28,28,28,28,28,...
29,.......
30,30,30,30,30,30,30,.
31,.......
32,32,32,32,32,...
33,33,33,.....
34,34,34,.....
35,35,35,.....
like that guess i gotta find the error now.
do i have to make a new method so that it displays the individual prime factors of a number?
I hate to say this, but IMHO your prime factor method is all wrong. I'm not sure what it does, but I do know that it doesn't calculate nor display primes, and try as I might, I can't find anything in it that I can salvage or fix.
I recommend that you scrap it totally and start over. The way to figure out the algorithm (and it really is simple once you see it) is to do it on paper first. What I mean is, calculate the prime factors of a couple of small numbers and write down each step that you do to perform this calcuation. Now translate those steps into code and in a short time your routine is done.
A few tips that at least worked for me:
* I stored my factors in an ArrayList of <Integer>
* I used a temp int var to hold n (int temp = n) so that when I found a prime I could factor it out of the temp and use the result to check for more prime factors (temp /= i).
* I stepped through possible ints from 2 to n inclusive (if n is a prime number, then the only factor will be n itself).
* Sometimes a prime factor is needed more than once. A while loop works well to check for this.
Good luck!
/Pete
thanks pete i did scrape it and managed to fix it so that works. Only one last problem is i am clueless on JUnit. i looked at the site explanation and am still lost. Hate to ask for more help than i have but does anyone know how i should go about doing it.
heres my code just in case.
public static void main(String[] args)
{
// Let them enter numbers on the command-line
for (int i=0; i < args.length; i++)
{
int n = Integer.parseInt(args[i]);
System.out.print("The prime factors of " + n + " are: ");
printfactors(n);// prints the prime factors of n
}
if (args.length==0)//command for if nothing is put in the args statement
{
System.out.print("please enter a number after your command");
}
System.exit(0);
}
public static void printfactors(int n)//method for printing factors of n
{
int nstart = n;
// loop through all possible factors, starting at 2
// biggest possible factor is n/2
for (int j = 2; j <= ( nstart / 2); j++)
{
// Find all the multiples of j
while ((n%j) == 0)
{
System.out.print(j);
n= n/j;
if(n > 1)
{
System.out.print(","); // Only print if there are more factors
}
}
// if n is 1, we are done, so can leave the loop
if (n == 1)
{
break;
}
}
// If we never divided, n must be prime
if (n == nstart)
{
System.out.print(n);
}
System.out.print("\n");
}
}
Congrats. Now THAT's a prime factoring routine that works!
A few more comments:
* Looks like you still have to implement the file input portion of your routine,... but you probably already know that.
* Do you have to use JUnit for your unit tests? If so, perhaps the teacher can help you with this? This seems like a heavy-duty solution to what looks like a light-weight problem, and my guess is that you can do a much simpler self-created unit tester. Again, ask your teacher.
* In your printFactors routine, you might want to check that n > 1
yeah i wish i could ask my teacher but he doesnt help me out ever all he does is grade so yeah thats why i have trouble all the time. He said that i should use junit but i just dont understand how to use it very well.
when u make the test method with junit. would i just pretty much insert some numbers to test and then just test those numbers to see if they are correct? idk im just really confused.
Never mind... should learn to look at the second page as well. :)Message was edited by: EvilBro
chrisitain is back for u
06110a at 2007-7-21 20:29:47 >

hi
06110a at 2007-7-21 20:29:47 >

hi
06110a at 2007-7-21 20:29:52 >
