0

I'm having a huge problem here. Basically what i have to do is have a user enter a number... the program then takes the number, reads if it's negative or positive. If negative it sets the previously false negval to true and moves on. it then reads the number again, checking for leading zeros, if there are any it removes them. It then takes what is left and checks to see if the string is less than the max amount of characters aloud, if it is then it loops to count the spaces, while it counts the spaces it also makes sure they are digits . if the string is bigger than the allowed amount of characters the program stops checks and prints a 0

my problem is that it keeps jumping to the 0 output even though the string length is a valid length

here is what i have.

 String snum;
System.out.print("Please enter a number here : ");
snum = console.next();
System.out.println(getIntnum(" The number entered was: "+snum));

Other generic stuff

    final String maxInt = "2147483648";
    final String minInt = "-2147483648";
    boolean negval = false;

    int n;
    int count;
    int num = 1;
    int value;

    if(snum.charAt(0) == '-' || snum.charAt(0) == '+')
     {
      if(snum.charAt(0) == '-')
      {
        negval = true;
      }
      else
      {
        snum = snum.substring(1, snum.length());
      }
    }

    while (snum.charAt(0) == '0')
    {
      snum = snum.substring(1, snum.length());
    }

    n = snum.length( );
    if (n < 10)
    {
      count = 0;
      if (count < n)
      {
        if (Character.isDigit(snum.charAt(count)))
          count++;
      }
    }


     else
       {
     num = 0;


    }
     if(maxInt.compareTo(snum) >= 0 && minInt.compareTo(snum) <= 0)
     {
       num = Integer.parseInt(snum);
     }
     if(negval == true)
     {
       num = num * -1;
     }
     value = num;
     return value;
}

}
2
  • return value shouldn't be there twice. Commented Dec 3, 2012 at 7:49
  • What does this mean "if it is then it loops to count the spaces, while it counts the spaces it also makes sure they are digits" ... spaces are digits? Commented Dec 3, 2012 at 8:27

4 Answers 4

1

You are comparing Strings which don't work the same way as comparing numbers. For example, "10" < "2" because '1' < '2' and this means that "-1" < "-2147483648" and "3" > "2147483647"

It's difficult to suggest what you should do instead as it's not clear to me why you are doing half of the code you have e.g. Can you say how your code is different from

try {
    return Integer.parseInt(s);
} catch (NumberFormatException ignored) {
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your main problem is that you're using a string comparison to compare numbers. Also, since you already know that the number is positive, there's not much point comparing it to minInt. Lastly, you have the wrong value for maxInt.

2 Comments

Well if they enter a negative number wouldn't i still need it?
You already chopped the minus sign off the front of the number.
0

Simply use Integer.parseInt(snum)

Comments

0
final String maxInt = "2147483648";

MAX_INT is 2147483647.

Second of all, do not use String comparison to compare numbers. Use it like this:

        if (Integer.parseInt(maxInt) >= Integer.parseInt(snum)) {
            num = Integer.parseInt(snum);

        }

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.