0

My issue is I am asking a user to input a value that is a positive integer. I have made sure that they cannot input a negative integer or zero but I am not sure how to make sure they cannot input a decimal.

package java1;

public class Problem3NPlus1 {

    public static void main(String[] args) {

        int givenInt;
        int givenIntMod;

        System.out.println("Provide a positive integer: ");
        givenInt = TextIO.getlnInt();

        while (givenInt <= 0 || *IF GIVENINT IS A DECIMAL? ) {
            System.out.println("That is not a positive integer, try again: ");
            givenInt = TextIO.getlnInt();
        }

        if (givenInt > 0) {
            while (givenInt != 1) {
                if (givenInt % 2 == 0) {
                    givenIntMod = (givenInt / 2);
                }
                else {
                    givenIntMod = ((givenInt * 3) + 1);
                }

                System.out.println(givenIntMod);
                givenInt = givenIntMod;
            }

        }
    }
}
5
  • 3
    Use modulo operator and check if it has a remainder. Commented Jul 23, 2014 at 19:42
  • 1
    As written, if someone types 11.001 you will only read 11. Is that good enough? Commented Jul 23, 2014 at 19:44
  • 1
    Return type of method getlnInt() is int. So it's not possible to get decimal. Commented Jul 23, 2014 at 19:45
  • @HanletEscaño OK, so like while (... || (givenInt % 1) != 0) That is what makes sense to me, because any decimal % 1 will not have a remainder of 0, but I still get an error. Commented Jul 25, 2014 at 17:36
  • @JesanFafon Actually, I would prefer that this program can account for that and recognize that 11.001 is also a decimal. Would this require being more specific with the type, i.e. something to do with float/double? Forgive me if I am not on the right track. Commented Jul 25, 2014 at 17:40

2 Answers 2

3

As TextIO.getlnInt() returns an int value, whatever is returned will be an int and an int cannot have decimals. You want to look at the TextIO.getlnInt() method. Can you show us how that is working?

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

5 Comments

Hello! OK, forgive me because, as I said, I am very new at this. But I believe I understand what you are saying--since that method will only return an int value, if someone inputs a decimal it would not be accepted. So when I input .5, it does return an 'error in input' -- integer value not found, and it asks to re-enter. But I would rather not have an error, and instead if someone enters a decimal value I want to re-prompt with 'That is not a positive integer, try again.'
Hi @user3870235, yes - you have the right idea. So the part which returns the "error in input -- integer value not found" must actually be in the method which you are calling called TextIO.getlnInt(). That method will only return an int, so it appears if it is something other than an int which is entered, the method is showing you some error message instead and returning something <= 0 causing your program to prompt again. Can you go to the TextIO class, and see the getlnInt() method? It is this method which is handling the problem and not returning (to your part) a value with a decimal.
It's that the method getlnInt() handles it and returns a value <=0, which means that you cannot know exactly what was originally entered, all know in your class is what that method returns. You may change what that method returns to make it return the value including a decimal, which means changing the return type to a double, instead of an int.
I would have a guess that this is the TextIO class you are using is this one: math.hws.edu/javanotes/source/TextIO.java If so, the comments in that document say: Skips whitespace characters and then reads a value of type int from input, discarding the rest of the current line of input (including the next end-of-line character, if any). When using standard IO, this will not produce an error; the user will be prompted repeatedly for input until a legal value is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found.
I assume this is a coursework, and that you may not be expected to make changes to that file though.
3

TextIO.getlnInt() is a function that can only return an integer, even if the user types in a double. Furthermore, since you assign it's output to a variable declared as int, the compiler would throw an error if it could even potentially return something other than an integer.

Comments

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.