Home > Software design >  How to break the execution of the code based on a user input
How to break the execution of the code based on a user input

Time:02-28

So I am doing a digit counter thing basically I want it to display 123 and which number is what place value for example 123

------------------------------------
Enter any number: 123
Ones: 3
Tens: 2
Hundreds: 1
------------------------------------

this is my code

import java.util.Scanner;

public class digits {

    public static void main(String[] args) 
    {
        Scanner scann = new Scanner(System.in);
        System.out.print("Enter any number: ");
        
        int number = scann.nextInt();
        
        int num1 = number % 10;
        int num2 = number / 10 % 10;
        int num3 = number / 100 % 10;
        int num4 = number / 1000 % 10;
        int num5 = number / 10000 % 10;
        int num6 = number / 100000 % 10;
        int num7 = number / 1000000 % 10;
        int num8 = number / 10000000 % 10;
        scann.close();
        
        System.out.println("Ones: " num1);
        System.out.println("Tens: " num2);
        System.out.println("Hundreds: " num3);
        System.out.println("Thousands: " num4);
        System.out.println("Ten-Thousands: " num5);
        System.out.println("Hundred-Thousands: " num6);
        System.out.println("Millions: " num7);
        System.out.println("Ten-Millions: " num8);

        }
    }

How do I stop it from printing the rest if I only type 123?

Output I got

--------------------------
Enter any number: 123
Ones: 3
Tens: 2
Hundreds: 1
Thousands: 0
Ten-Thousands: 0
Hundred-Thousands: 0
Millions: 0
Ten-Millions: 0
------------------------

Output I want

--------------------------
Enter any number: 123
Ones: 3
Tens: 2
Hundreds: 1
------------------------

CodePudding user response:

You need to introduce a condition (or conditions) in your code.

You can achieve that with a chain of if statements. But the better way to do it is by utilizing a loop. Because that will allow you to get rid of the intermediate variables (num1, num2, etc) and to avoid duplicating the line of code that prints the remainder on the consol. That will make the code more readable and concise.

In order to be able to apply the loop for this problem, you need to create an array of strings that will store all quantifiers ("Ones: ", "Tens: ", etc).

It can be done like that:

public static final String[] quantifiers =
           {"Ones: ", "Tens: ", "Hundreds: ", "Thousands: ",
            "Ten-Thousands: ", "Hundred-Thousands: ", "Millions: ", "Ten-Millions: "};
public static void main(String[] args) {
    Scanner scann = new Scanner(System.in);
    int number = scann.nextInt();

    for (int i = 0; i < quantifiers.length && number > 0; i  ) {
        System.out.println(quantifiers[i]   number % 10);
        number /= 10; // does the same as number = number / 10;
    }
}

output for input 123

Ones: 3
Tens: 2
Hundreds: 1

CodePudding user response:

It might help your question. Because of hardcoded terms such as 'tens' or 'hundreds', it is not generic enough.

public static void main(String[] args) {
    Scanner scann = new Scanner(System.in);
    System.out.print("Enter any number: ");

    int number = scann.nextInt();

    int length = String.valueOf(number).length();

    for(int i=0; i< length; i  ){

        if(i == 0){
            System.out.println("Ones: "  number % 10);
        }else if(i == 1)
            System.out.println("Tens: "   number / 10 % 10);
        else if(i == 2)
            System.out.println("Hundreds: "   number / 100 % 10);
        else if(i == 3)
            System.out.println("Thousands: "  number / 1000 % 10);
        else if(i == 4)
            System.out.println("Ten-Thousands: "  number / 10000 % 10);
        else if(i == 5)
            System.out.println("Hundred-Thousands: "  number / 100000 % 10);
        else if(i == 6)
            System.out.println("Millions: "  number / 1000000 % 10);
        else if(i == 7)
            System.out.println("Ten-Millions: "   number / 10000000 % 10);
    }
    scann.close();

}

CodePudding user response:

Your code is printing all the numbers because that's what you wrote:

System.out.println("Ones: " num1);

...and so on.

If you never want to print e.g. thousands, just remove the println for thousands. If you only want to print them if someone actually enters thousands, add an if statement:

if (num4 > 0) {
   System.out.println("Thousands: " num4);
}

Repeat for the others.

  •  Tags:  
  • java
  • Related