Click here to Skip to main content
13,801,161 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
I have a output in form "1 1 1 2 2 " but I wanted to print the numbers separated in a line by exactly one comma

What I have tried:

<pre lang="java"><pre>public class Main {

    static int a = 0;
    static int b = 0;

    public static void main(String[] args) {
        
        int n = Integer.parseInt(args[0]);
        a = Integer.parseInt(args[1]);
        b = Integer.parseInt(args[2]);
        for (int i = 0; i <= n; i++){
            System.out.print(recursiv(i));}

    }

    public static long recursiv(int n) {

        if (n >= 0 && n <= 2)
            return 1;

        else
            return a * recursiv(n - 2) + b * recursiv(n - 3);

    }

}
Posted 16-May-18 7:54am
Updated 16-May-18 10:00am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Get started with:
for (int i = 0; i <= n; i++){
    System.out.print(recursiv(i));
    System.out.print(",");
}

I let you handle the last number.
  Permalink  
Comments
Member 13817762 16-May-18 15:18pm
   
thank you very much , I have solved it with for- loop .
for (int i = 0; i <= n; i++){
System.out.print(recursiv(i));
if(i<n){
System.out.print(",");}
}
Patrice T 16-May-18 15:49pm
   
Nice to see it
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

As you have a String[] as the input, why don't you just use String.join for the output?


public static void main(String[] args) {
            System.out.print(String.join(",", args);
}
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.181215.1 | Last Updated 16 May 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100