1

I'm writing code that prints you combination of numbers from 1 to n. In console it works perfectly fine, but i need to print it into file. What should i do?

Code:

public class Permutace {

    public static void main(String args[]){

        Scanner sc = new Scanner(System.in);
        System.out.println("your number: ");
        int N = sc.nextInt();
        int[] sequence = new int[N];
        for (int i = 0; i < N; i++) {
            sequence[i] = i + 1;
        }

        permute(sequence, 0);
    }

    public static void permute(int[] a, int k){

        if (k == a.length) {
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i]);
            }
            System.out.println();
        } else {
            for (int i = k; i < a.length; i++) {
                int temp = a[k];
                a[k] = a[i];
                a[i] = temp;

                permute(a, k + 1);

                temp = a[k];
                a[k] = a[i];
                a[i] = temp;
            }
        }
    }
}
Justin Albano
  • 3,809
  • 2
  • 24
  • 51
  • what do you mean by "but i need to print it into file" , what and where is file?? what is the error?? – Vishwa Ratna Jan 15 '19 at 12:50
  • @Dimitris , idk what he mean by file, until then you cant say it is duplicate. – Vishwa Ratna Jan 15 '19 at 12:54
  • 1
    What kind of a file are you talking about? – MS90 Jan 15 '19 at 12:56
  • @Akceptor where in code you see file mentioned, except title?? You can't say it is duplicate. – Vishwa Ratna Jan 15 '19 at 12:56
  • 2
    @CommonMan He says it works in console but he needs to write it to a file, the duplicate shows how to do that. – Mark Jan 15 '19 at 12:57
  • @Op says he wants to print into a file not write into a file, moreover OP is not clear in what he want to say here, you just cant keep saying duplicate to unclear questions. there is a hell lot of difference to write and print. – Vishwa Ratna Jan 15 '19 at 12:59
  • 3
    @CommonMan The question asks to print numbers in a file, the same way that he prints in the console. So unless I miss something it is just a text file in the local. Also, it is possible duplicate. – Dimitris Jan 15 '19 at 13:00
  • 2
    @CommonMan: the OP has all the time in the world to [edit] their question to clarify it. – Mat Jan 15 '19 at 13:00
  • @CommonMan he asks how to writhe the output into file. I assume if someone doesn't know how to do that he won't put that in code – Akceptor Jan 15 '19 at 13:03
  • @Akceptor , question mentioned the word "print" not "write" , there is hell a lot of difference between both implementation, anyways as you all wish do it :). – Vishwa Ratna Jan 15 '19 at 13:05

1 Answers1

0
public static void main(String args[]){

        Scanner sc = new Scanner(System.in);
        System.out.println("your number: ");
        int N = sc.nextInt();
        int[] sequence = new int[N];

        PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");


        for (int i = 0; i < N; i++) {
            sequence[i] = i + 1;
        }

        permute(sequence, 0);
        writer.close();
    }

    public static void permute(int[] a, int k){

        if (k == a.length) {
            for (int i = 0; i < a.length; i++) {
                writer.println(a[i]);
            }
            writer.println();
        } else {
            for (int i = k; i < a.length; i++) {
                int temp = a[k];
                a[k] = a[i];
                a[i] = temp;

                permute(a, k + 1);

                temp = a[k];
                a[k] = a[i];
                a[i] = temp;
            }
        }
    }
Bruno Caceiro
  • 7,035
  • 1
  • 26
  • 45