You want to make some sort of int array in your factor method. Then use that int array to determine how many instances of x number appears. I would advise a List (such as a Vector) since you will not need to worry about size. Here is what you need:
String factor(int n)
{
String s;
//Create Integer Vector
Vector<Integer> numbers = new Vector<Integer>();
int f = 2;
while (n > 1)
{
if (n%f == 0)
{
//Add number to Integer Vector
numbers.add(new Integer(f));
n /= f;
}
else
f++;
}
//Go through Vector. Doing this way because we will remove from the list as we go
while(numbers.size()>0)
{
//Get number and remove it to allow counting
Integer tempNumber = numbers.remove(i);
//Counter Variable
int numberOfTimes = 1;
//I think this works, not entirely sure
while(numbers.indexOf(tempNumber)!=-1)
{
//Remove this number from the Vector
numbers.remove(numbers.indexOf(tempNumber));
//Increment counter
numberOfTimes++;
}
if(numberOfTimes==1)
s+=tempNumber+"*";
else
s+=tempNumber+"^"+numberOfTimes+"*";
}
//Remove the last * symbol
return (S.substring(0,s.length()-1));
}
There is probably a better way, but that is the best I could come up with without putting more effort in. I haven't tested the code, so tell me if it doesnt work.