0

I am trying to create a program that calculates the product between the 3 digits of an integer in java. Everything works fine until I enter a number that has less than 3 digits, then eclipse throws this error:

Enter a number between 100 and 999
99
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String    index out of range: 3
at java.lang.String.substring(Unknown Source)
at Ex8.main(Ex8.java:23)

I've searched for alternate solutions, so I know how to rewrite my program so that it can run and work, but my question is why isn't my program just saying "number is not valid" instead of just ignoring my if statement? Here is my pice of code, and thank you in advance for answering.

import java.util.Scanner;

public class Ex8 {

public static void main(String[] args) {
    int number, firstDigit, secondDigit, thirdDigit, product;

    Scanner scan = new Scanner(System.in);

    System.out.println("Enter a number between 100 and 999");

    number = scan.nextInt();
    scan.close();

    if (number <= 99 && number> 999){
        System.out.println("number is not valid");
    }
    else{
        firstDigit = Integer.parseInt(Integer.toString(number).substring(0, 1));
        secondDigit = Integer.parseInt(Integer.toString(number).substring(1, 2));
        thirdDigit = Integer.parseInt(Integer.toString(number).substring(2, 3));
        product = firstDigit*secondDigit*thirdDigit;
        System.out.println(product);
    };
}
}
3
  • 1
    You should have - if (number <= 99 || number> 999) there. Commented Feb 12, 2015 at 17:39
  • Yep, faulty logic, that was it. Thank you!! Commented Feb 12, 2015 at 17:48
  • Also you could make the code more efficient by using integer math instead converting the int's to Strings and back. i.e. firstDigit = number/100; then secondDigit = (number%100)/10; and thirdDigit = (number%10); Commented Feb 12, 2015 at 17:52

2 Answers 2

1

You wrote your if statement with &&

number <= 99 && number > 999

when you should actually be using ||

number <= 99 || number > 999

This will fix the code.

Sign up to request clarification or add additional context in comments.

Comments

1

Its fail on this line 99 doesn't have three digit. Based on your code it comes to else part. You have to use || instead of && in if statement.

        thirdDigit = Integer.parseInt(Integer.toString(number).substring(2, 3));

3 Comments

That is not it... the wrong use of && instead of || in the IF statement is the problem.
I know, my question was why does it break when it shouldn't even run that piece of code.
no its come to that line because according to this if (number <= 99 && number> 999){ it says number should be less than or equal to 99 and number should be greater than 999 if both satiesfied it goes to inside if statement in you case 99 not satiesfy both

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.