2

I'm doing a prime factorization code, and I'm trying to print "x" between each displayed prime factor of the user-inputted number. However, I can't seem to figure out how to make it so that an extra "x" does not appear after the last displayed prime factor.

I've tried concatenating mod after the "x" in the print statement, and I've tried thinking about using an If statement for (startInt % mod == 0), but I'm not too sure what the else statement would be for that.

    System.out.println("Enter an integer to be factored:");
    int startInt = userInput.nextInt();

    if(startInt % 2 == 0 || startInt % 3 == 0 || startInt % 5 == 0 || startInt % 7 == 0 || startInt % 11 == 0 || startInt % 13 == 0)
    {
        for(int mod = 2; mod <= startInt; mod++)
        {
            while(startInt%mod == 0)
            {
                startInt /= mod;
                System.out.print(mod+" x ");
            }
        }
    }

    else
    {
        System.out.println(startInt+" = "+startInt);
        System.out.println(startInt+" is a prime number.");
    }

I expect the output of 784784 to be 2 x 2 x 2 x 2 x 7 x 7 x 7 x 11 x 13 but the actual output is 2 x 2 x 2 x 2 x 7 x 7 x 7 x 11 x 13 x.

  • 1
    print the `x` before the number, but not the first time. Kind of `boolean first = true; for ( ... if (!first) System.out.print("x") else first = false; System.out.print(mod); ...` – user85421 Oct 05 '19 at 20:32
  • Thank you for the reply, but I have a few questions. What does first do/mean? How should I use the System.out.print("x") and System.out.print(mod) to display my intended result? I apologize in advance, I'm new to coding and Stackoverflow. – dan._.theman Oct 05 '19 at 21:30
  • `first` is a boolean variable indicating if it is the first value will be output (if `first == true`), or if it is not the first one, that is, the first was already printed (if `first == false`); you use `System.out.print` the same way as you did in posted code – user85421 Oct 05 '19 at 21:38
  • I tried using what you said, putting my System.out.print(mod+" x "); inside If(first == true), but it still prints the undesired output (extra "x" at the end). I'm still confused on how specifically to format the System.out.print statements with the If statements. – dan._.theman Oct 06 '19 at 15:22
  • I'll put a very simple example as answer, just the idea, must be adapted – user85421 Oct 06 '19 at 16:07

3 Answers3

0

Check this sample (I adapted it to Javascript, but the fixed line is the sample for both languages):

const System = {
     out: {
       println: console.log
     }
   };
   
   System.out.println("Enter an integer to be factored:");
   let startInt = 784784;
   const expectedResult = '2 x 2 x 2 x 2 x 7 x 7 x 7 x 11 x 13'

   if(startInt % 2 == 0 || startInt % 3 == 0 || startInt % 5 == 0 || startInt % 7 == 0 || startInt % 11 == 0 || startInt % 13 == 0) {
        let result = '';
        for(let mod = 2; mod <= startInt; mod++) {
            while (startInt%mod === 0) {
                startInt /= mod;
                // Fix this line
                result += mod + (mod < startInt ? ' x ' : '');
            }
        }
        System.out.println(result);
        if (result !== expectedResult) {
           System.out.println('Fail');
        } else {
           System.out.println('Everything is Ok');
        }
    } else {
        System.out.println(startInt+" = "+startInt);
        System.out.println(startInt+" is a prime number.");
    }

You must avoid adding 'x' in the last cycle:

result += mod + (mod < startInt ? " x " : "");

0

Very simple example using an additional variable first to know if it is the first iteration or not. In this example i > 0 could have been used to know that it is not the first iteration, but using the variable also works if not looping with an index (e.g. while (!collection.isEmpty())).

public class Demo {
    public static void main(String[] args) {
        boolean first = true;
        for (int i = 0; i < 5; i++) {
            if (!first) {
                System.out.print(" x ");
            }
            System.out.print(i);
            first = false;  // not first after this iteration
        }
    }
}
user85421
  • 28,957
  • 10
  • 64
  • 87
0

To simplify your issue, separate concerns - first build a list of prime factors, then print the list

List<String> primeFactors = new ArrayList<>();

// your algorithm, which will instead add prime factors to the above list

String result = String.join(" x ", primeFactors);

String::join unfortunately only takes in list of Strings, if you want to join a list of Integers you can look at this question