Home > front end >  How to print "and" before the last value in addition to ","?
How to print "and" before the last value in addition to ","?

Time:01-23

public static void main(String[] args) {       
        Scanner sc = new Scanner(System.in);
        System.out.println("How old are you?");
        int age = sc.nextInt();
        System.out.println("What is your favorite number?");
        int favnum = sc.nextInt();
        int max, min;
        String sequence ="";
        if (favnum > age) {
            max = favnum;
            min = age;
        } else {
            max = age;
            min = favnum;
        }
        System.out.print("The values between "   max   " and "   min   " that are divisible by 4 are ");
        for (int i = max; i >= min; i--) {
            if (i % 4 == 0) {
                System.out.print(i  ", ");
            }
            
            if(i % 4 == 0 && i / 4 == 1) {
                System.out.print("and "  i  ".");
            }
        }    
   }
}

For example if input values are between 21 and 6. I want it to print "The values between 21 and 6 that are divisible by 4 are 20, 16, 12, and 8. for the last digit to have an and and a period at the end.

This is a small part of my code, but I've been stuck on this part I tried many ways to do it but it doesn't work for me.

This is what it prints:

How old are you?
21
What is your favorite number?
6
The values between 21 and 6 that are divisible by 4 are 20, 16, 12, 8,

What I want it to print:

The values between 21 and 6 that are divisible by 4 are 20, 16, 12, and 8.

CodePudding user response:

You would need to do it differently and add all the divisible numbers to a list and then pretty-printing the list as the following example shows:

public static void main(String[] args) {
    int max = 20;
    int min = 1;
    List<Integer> divisibleBy4 = new ArrayList<>();
    for (int i = max; i >= min; i--) {
        if (i % 4 == 0) {
            divisibleBy4.add(i);
        }
    }

    for (int j = 0; j < divisibleBy4.size(); j  ) {
        if (j == 0) {
            System.out.print(divisibleBy4.get(j));
        } else if (j == divisibleBy4.size() - 1) {
            System.out.print(" and "  divisibleBy4.get(j)  ".");
        } else {
            System.out.print(", "   divisibleBy4.get(j));
        }
    }
}

Adding this to your code would result in the following working code:

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("How old are you?");
        int age = sc.nextInt();
        System.out.println("What is your favorite number?");
        int favnum = sc.nextInt();
        int max, min;
        String sequence ="";
        if (favnum > age) {
            max = favnum;
            min = age;
        } else {
            max = age;
            min = favnum;
        }
        System.out.print("The values between "   max   " and "   min   " that are divisible by 4 are ");
        List<Integer> divisibleBy4 = new ArrayList<>();
        for (int i = max; i >= min; i--) {
            if (i % 4 == 0) {
                divisibleBy4.add(i);
            }
        }

        for (int j = 0; j < divisibleBy4.size(); j  ) {
            if (j == 0) {
                System.out.print(divisibleBy4.get(j));
            } else if (j == divisibleBy4.size() - 1) {
                System.out.print(" and "  divisibleBy4.get(j)  ".");
            } else {
                System.out.print(", "   divisibleBy4.get(j));
            }
        }
    }
}

CodePudding user response:

The difficulty with what you are doing is that you don't know how many multiples of four there are in the sequence before you start your loop. This makes it difficult for you to handle these conditions:

  • no multiples of four present

  • only one multiple of four (hence no need for "and")

  • handling the last multiple differently (as you know)

To solve this, I would refactor your code like so (please excuse any typos - I'm writing this from my phone).

// figure out how many multiples of four exist within the range
int firstMultiple = max - (max % 4);

// handle no fours being present
if (firstMultiple < min) { ... }

int countFours = (firstMultiple - min) / 4;
countFours  ; // add one since we started from a multiple

// loop for printing all but the last one
int stopAt = countFours - 1;
for (int i = 0; i < stopAt; i  ) {
    // print with commas inside this loop
    int thisMultiple = firstMultiple - (4 * i);
}

if (countFours > 1) { /* print and here */ }

// print the final four
int finalFour = firstMultiple - (4 * stopAt); 

CodePudding user response:

For the last number, you have to check that the difference between i and min is less than 4 . Therefore you can add the following in your for loop statement.

public class Swtich {
    public static void main(String[] args) {

        System.out.println("How old are you?");
        Scanner sc = new Scanner(System.in);
        int age = sc.nextInt();
        System.out.println("What is your favorite number?");
        int favnum = sc.nextInt();
        int max, min;
        String sequence = "";
        if (favnum > age) {
            max = favnum;
            min = age;
        } else {
            max = age;
            min = favnum;
        }
        System.out.print("The values between "   max   " and "   min   " that are divisible by 4 are ");
        StringBuilder sb = new StringBuilder();
        for (int i = max; i >= min; i--) {
            if (i % 4 == 0 && (i - min) <= 4) {

                sb.delete(sb.length() - 2, sb.length());
                sb.append(" and "   i   ".");
            } else if (i % 4 == 0) {
                sb.append(i   ", ");

            }

        }
        System.out.println(sb.toString());
    }
}

and the output is as follows :

How old are you?
21
What is your favorite number?
6
The values between 21 and 6 that are divisible by 4 are 20, 16, 12 and 8.

CodePudding user response:

    int counter=0; //add this counter 

    System.out.println("How old are you?");
    int age = sc.nextInt();
    System.out.println("What is your favorite number?");
    int favnum = sc.nextInt();
    int max, min;
    String sequence ="";
    if (favnum > age) {
        max = favnum;
        min = age;
    } else {
        max = age;
        min = favnum;
    }
    System.out.print("The values between "   max   " and "   min   " that are divisible by 4 are ");
    for (int i = max; i >= min; i--) {
        if(i % 4==0) {
            counter  ; //Number of values that are divisible by 4.
        }
    }
    for (int i = max; i >= min; i--) {
        if(i % 4 == 0 && counter==1) {
            System.out.print("and "  i  ".");
        }else
        {
            if (i % 4 == 0) {
                System.out.print(i  ", ");
                counter--;
            }
        }
        
     }

This is the output :

How old are you?
30
What is your favorite number?
4
The values between 30 and 4 that are divisible by 4 are 28, 24, 20, 16, 
12, 8, and 4.
  •  Tags:  
  • Related