Showing posts with label Array integers to conditional String. Show all posts
Showing posts with label Array integers to conditional String. Show all posts

ArrayInteger2ConditionalString

Replace array Integers with the conditional text/string

package test;

 

import java.util.Arrays;

 

public class ArrayIntToString {

      

           /*

            *       if i is a multiple of both 3 and 5, print threeAndFive

                    if i is a multiple of 3 but not 5, print threeAndNOTfive

                    if i is a multiple of 5 but not 3, print FiveAndNOTthree

                    if I is not a multiple of 3 or 5, print the value of i

            */

           public static void main(String[] args){

               System.out.println("array = "+Arrays.toString(fizzBuzz(15)));

           }

 

           public static String[] fizzBuzz(int n) {

               String[] arrString = new String[n];

               String word = "";

           for(int j=0;j<n;j++){

              int i=j+1;

               if((i%3==0) &&(i%5==0)){

                   word="threeAndFive";

               } else if(((i%3==0) &&(!(i%5==0)))){

                   word="threeAndNOTfive";

               } else if(((i%5==0) &&(!(i%3==0)))){

                   word="FiveAndNOTthree";

               } else if(((!(i%5==0)) &&(!(i%3==0)))){

                   word=Integer.toString(i);//Enter the value of i

               }

                   arrString[j]= word;

           }

           return arrString ;

           }

 

       }

 

Output:

array = [1, 2, threeAndNOTfive, 4, FiveAndNOTthree, threeAndNOTfive, 7, 8, threeAndNOTfive, FiveAndNOTthree, 11, threeAndNOTfive, 13, 14, threeAndFive]