Home > Blockchain >  Output string after evaluating numbers in int
Output string after evaluating numbers in int

Time:01-08

I'm currently trying to print a string for each instance of 3 specific numbers (3 to Pim, 5 to Pam, 7 to Pum) in a number that might be present after user input (EX: Input: 1052373 Output: PamPimPumPim). I tried converting the number to a string and using .contains method but it isn't working. The other part of the code checks if the number is divisible by 3, 5 or 7 and outputs in the same way, and it works fine. Help's appreciated!

package pimpampum;
import java.util.Scanner;

public class PIMPAMPUM {

    public static void main(String[] args) {
        System.out.println("Introduza um número:");
        Scanner input = new Scanner(System.in);
        int x;
        x = input.nextInt();
        String y = x.toString();
         if(x % 3==0){
             System.out.print("Pim");
        }
         if(x % 5==0){
                 System.out.print("Pam");
        } 
         if(x % 7==0){
                 System.out.print("Pum");
        }
         if(y.contains("3")){
                 System.out.print("Pim");
        }
         if(y.contains("5")){
                 System.out.print("Pam");
        }
         if(y.contains("7")){
                 System.out.print("Pum");
        }
    }
}

CodePudding user response:

In order to print appropriate word for digits 3, 5, or 7 contained in the input number, just iterate the characters upon converting the number into a String:

int x = input.nextInt();
String y = String.valueOf(x);
for (char c : y.toCharArray()) {
    String code = switch(c) {
        case '3' -> "Pim";
        case '5' -> "Pam";
        case '7' -> "Pum";
        default  -> "";
    };
    System.out.print(code);
}
System.out.println();

For the input 1052373, the code above prints:

PamPimPumPim

for ..5.373, the rest digits are ignored.

CodePudding user response:

I think what you need to do is a While to iterate the numbers.

I present my solution to your problem.

From what I understand about your question, the additions I made to your code seem to me to be adequate for that.

I would like to point out that a little Google search would quickly solve your problems.

Anyway, we are here to share and help each other!

If this is the intended Up Vote.

Any questions, ask or comment!

import java.util.Scanner;
import java.util.*;

class Main {
public static void main(String[] args) {
        System.out.println("Introduza um número:");
        Scanner input = new Scanner(System.in);
        int x;
        x = input.nextInt();
        String y = String.valueOf(x);

        LinkedList<Integer> stack = new LinkedList<Integer>();

        while(x > 0){
            stack.push(x % 10);
            x = x / 10;
        }

        while ((!stack.isEmpty())){
            System.out.println(stack.pop());
        }

         if(x % 3==0){
             System.out.print("Pim");
        }
         if(x % 5==0){
                 System.out.print("Pam");
        } 
         if(x % 7==0){
                 System.out.print("Pum");
        }
         if(y.contains("3")){
                 System.out.print("Pim");
        }
         if(y.contains("5")){
                 System.out.print("Pam");
        }
         if(y.contains("7")){
                 System.out.print("Pum");
        }
    }
}

OUTPUT

Introduza um número:
1052373
1
0
5
2
3
7
3
PimPamPumPimPamPum
  •  Tags:  
  • Related