1

I have a problem asking me to tell the amount of digits in a given number. I inserted conditional statements, but no matter what number I insert, it gives me "nine" digits. The code is below. I'm a math student and not computer science, so I figure I'm not using the most efficient method.

import java.util.Scanner;

public class E52 {
    public static void main(String[] args) {
        // part 1: import scanner
        Scanner sc = new Scanner(System.in);

        // part 2: ask user for a number
        System.out.println("Please enter a number: ");
        int number = sc.nextInt();

        // part 3: tell how many digits are in each number by using string and conditional statements
        String number1Str;
        if (number >= 0 || number <= 10) number1Str = "one";
        if (number >= 10 || number <= 100) number1Str = "two";
        if (number >= 100 || number <= 1000) number1Str= "three";
        if (number >= 1000 || number <= 10000) number1Str = "four";
        if (number >= 10000 || number <= 100000) number1Str = "five";
        if (number >= 100000 || number <= 1000000) number1Str = "six";
        if (number >= 1000000 || number <= 10000000) number1Str = "seven";
        if (number >= 10000000 || number <= 100000000) number1Str = "eight";
        if (number >= 100000000 || number <= 1000000000) number1Str = "nine";
        else number1Str = "undefined";

        System.out.println("There are " + number1Str + " digits in the given number.");

        sc.close();
    }
}
UkFLSUI
  • 5,509
  • 6
  • 32
  • 47

2 Answers2

1

It would be much easier to create an Array, then get the input's length and pick let it pick the entry:

Scanner sc = new Scanner(System.in);

// part 2: ask user for a number
System.out.println("Please enter a number: ");
Integer number = sc.nextInt();
final String[] numNames = {
            "",
            "one",
            "two",
            "three",
            "four",
            "five",
            "six",
            "seven",
            "eight",
            "nine",
            "ten",
            "eleven",
            "twelve",
            "thirteen",
            "fourteen",
            "fifteen",
            "sixteen",
            "seventeen",
            "eighteen",
            "nineteen"
    };
System.out.println(numNames[number.toString().length()]);
baao
  • 71,625
  • 17
  • 143
  • 203
  • 1
    Well, there are zillions of ways to do that. Very much depends on what his homework expects him to do. In case he has to use if/else ... your solution is pointless. – GhostCat Sep 08 '17 at 17:10
  • Again - I didnt downvote, simply comment. – GhostCat Sep 08 '17 at 17:39
0

This will give you number count no matter how big or small number is. Although there are corner scenarios which may fail the code but i still feel this will be of use to you in some other way around

public class CountNum {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String numAsString = sc.nextLine();
        System.out.println(numAsString.length());
        sc.close();
    }

}
kaza
  • 2,317
  • 1
  • 16
  • 25
  • 1
    And when his assignment asks for using if/else .. your answer doesnt help him. – GhostCat Sep 08 '17 at 17:10
  • Agree.. If the whole point is for teaching if-else and operators, it would not make sense to proceed in this way. :) – Aditya Ajmera Sep 08 '17 at 17:13
  • @GhostCat where does he say the assignment required if/else? He even admitted he's probably using an inefficient method which implies it was of his own choosing. He simply stated he needed to find the number of digits. – Robert Wade Sep 08 '17 at 17:13
  • Note: I didn't downvote. I am just talking out of experience. When newbies focus on a certain pattern - then often because they are asked to do so. – GhostCat Sep 08 '17 at 17:21