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?

[565 byte] By [hyperkneesa] at [2007-11-27 2:39:57]
# 1
The first one is asking for a path to a file, like:C:\numbers.txtThe file should have one integer on each line.
CaptainMorgan08a at 2007-7-12 3:02:23 > top of Java-index,Java Essentials,New To Java...
# 2

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;

}

jeanberna at 2007-7-12 3:02:23 > top of Java-index,Java Essentials,New To Java...
# 3
i see ill give it a shot
hyperkneesa at 2007-7-12 3:02:23 > top of Java-index,Java Essentials,New To Java...
# 4
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?
hyperkneesa at 2007-7-12 3:02:23 > top of Java-index,Java Essentials,New To Java...
# 5
I don't understand that question
jeanberna at 2007-7-12 3:02:23 > top of Java-index,Java Essentials,New To Java...
# 6
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>
hyperkneesa at 2007-7-12 3:02:24 > top of Java-index,Java Essentials,New To Java...
# 7
well it will work for any number but if you only want to make it work for 1-20 its not that hard
jeanberna at 2007-7-12 3:02:24 > top of Java-index,Java Essentials,New To Java...
# 8

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?

hyperkneesa at 2007-7-12 3:02:24 > top of Java-index,Java Essentials,New To Java...
# 9
if (j <= countFactor)System.out.print(i + ", ");try this inside your last for loop
jeanberna at 2007-7-12 3:02:24 > top of Java-index,Java Essentials,New To Java...
# 10

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();

}

}

}

hyperkneesa at 2007-7-12 3:02:24 > top of Java-index,Java Essentials,New To Java...
# 11
still no reply just wondering if this is an easy to fix error?
hyperkneesa at 2007-7-12 3:02:24 > top of Java-index,Java Essentials,New To Java...
# 12
Your code compiles just fine for me.
CaptainMorgan08a at 2007-7-12 3:02:24 > top of Java-index,Java Essentials,New To Java...
# 13
ok sorry it compiles now but it doesnt run. it says it has no class definition in the main method or somethin.
hyperkneesa at 2007-7-12 3:02:25 > top of Java-index,Java Essentials,New To Java...
# 14
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?
CaptainMorgan08a at 2007-7-12 3:02:25 > top of Java-index,Java Essentials,New To Java...
# 15

> 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.

pbrockway2a at 2007-7-21 20:29:47 > top of Java-index,Java Essentials,New To Java...
# 16
it compiles it just doesnt run anyway how do i copy the error message if i am doing this inthe command prompt
hyperkneesa at 2007-7-21 20:29:47 > top of Java-index,Java Essentials,New To Java...
# 17
Right click on it and select "Mark". Highlight what you want to copy and press the enter key to copy it.
CaptainMorgan08a at 2007-7-21 20:29:47 > top of Java-index,Java Essentials,New To Java...
# 18
thank u. when i run the program i get this messageC:\>java primefactorException in thread "main" java.lang.NoClassDefFoundError: primefactor
hyperkneesa at 2007-7-21 20:29:47 > top of Java-index,Java Essentials,New To Java...
# 19
http://java.sun.com/docs/books/tutorial/getStarted/problems/index.html
CaptainMorgan08a at 2007-7-21 20:29:47 > top of Java-index,Java Essentials,New To Java...
# 20

**** 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.

hyperkneesa at 2007-7-21 20:29:47 > top of Java-index,Java Essentials,New To Java...
# 21
do i have to make a new method so that it displays the individual prime factors of a number?
hyperkneesa at 2007-7-21 20:29:47 > top of Java-index,Java Essentials,New To Java...
# 22

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

petes1234a at 2007-7-21 20:29:47 > top of Java-index,Java Essentials,New To Java...
# 23

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");

}

}

hyperkneesa at 2007-7-21 20:29:47 > top of Java-index,Java Essentials,New To Java...
# 24

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

petes1234a at 2007-7-21 20:29:47 > top of Java-index,Java Essentials,New To Java...
# 25
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.
hyperkneesa at 2007-7-21 20:29:47 > top of Java-index,Java Essentials,New To Java...
# 26
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.
hyperkneesa at 2007-7-21 20:29:47 > top of Java-index,Java Essentials,New To Java...
# 27
Never mind... should learn to look at the second page as well. :)Message was edited by: EvilBro
EvilBroa at 2007-7-21 20:29:47 > top of Java-index,Java Essentials,New To Java...
# 28
chrisitain is back for u
06110a at 2007-7-21 20:29:47 > top of Java-index,Java Essentials,New To Java...
# 29
hi
06110a at 2007-7-21 20:29:47 > top of Java-index,Java Essentials,New To Java...
# 30
hi
06110a at 2007-7-21 20:29:52 > top of Java-index,Java Essentials,New To Java...