Okay, I am still working on this one problem. The aim of it is to determine if an integer is an integer and to print "this is an integer" or to print "This is not an integer" if it isn't an integer. integers are 12345, -0. And non integers are hello, 1 2(that is a 1 then a space), hello12345, and 12345hello. My problem is a few things.I wish to printout the statement once(once a integer can be determined or a non integer can be determined. My program is printing out all the characters which are integers or not integers. I have really though hard about this and can't find a damn solution. Well here is my code;
What is the real purpose of your program, is it just for validating that users entering a valid integer number or a string? Or is there any other purposes?
What about using the java.lang.Integer to validate the input, parse the string to know whether it is a valid integer or not.
I got it working. I used a try catch block. I just wanted to learn it the hard way first but I like this method better. Thanks for the input.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
4
posted
0
The hard way would have been to iterate over every character in the String, use the Character.isDigit() method, check to see whether the first character is a minus, whether the total comes to less than -2147483648 or more then 2147483647, etc etc.
I am sure that Exceptions were not originally designed for that sort of thing, but they're a d*mn site easier to use!
The easiest way (that doesn't require catching a RuntimeException) is to use a regular expression. You might like to look at java.util.regex if you have the time.
What's your definition of "Integer"? Is it a 32-bit two's complement integer, like the primitive type int, or does your integer include 5555555555555555555? [ October 19, 2005: Message edited by: Jeff Albrechtsen ]
There is no emoticon for what I am feeling!
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
posted
0
If you want to revisit your original post, you should ask yourself how you know if the whole String represents an integer? Maybe it is easier to ask what makes it NOT an integer? The obvious answer is that if you find even one character that is not a digit, then it isn't an integer. Otherwise it is. One way to implement this logic is to create a flag that is set as soon as you encounter a non-digit character in the String. After the loop, you check the flag to see whether it is set or not. (Note: a flag is typically a boolean variable.)
If this doesn't make any sense, please let us know and we will be glad to continue to help.
Hello, I follow that logic Layne. However, I just started learning about classes and objects in class and have not been introduced to flags. However, the instructor did mention using flags for this problem. In order to flag something do you need to introduce a new class. for example, this program won't compile for some reason. Thanks in advance again.
public class Intiscon {
public static void main(String [] args){
public boolean isInteger(String s){ for (int i = 0; i < s.length(); i = i + 1){ if(!Character.isDigit(s.charAt(i))) return false; } return true; } } }
Marilyn de Queiroz
Sheriff
Joined: Jul 22, 2000
Posts: 9033
10
posted
0
Hooeee, that's hard to read!
Now I can see that you're trying to put one method inside another. Not cool.
You've used returns here rather than flags. It's one way to do it. Also, by the way, usually in for loops rather than "i = i + 1", you'll see "i++"
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
posted
0
Originally posted by Mike Smith: Hello, I follow that logic Layne. However, I just started learning about classes and objects in class and have not been introduced to flags. However, the instructor did mention using flags for this problem. In order to flag something do you need to introduce a new class....
Huh? Flags have nothing to do with classes and objects. In fact, flags have been around much longer than Object Orient Programming. A flag is simply a boolean variable that is either true or false. That's it! So in this case, you need to create a boolean variable that indicates whether or not you have seen a non-digit char in your String.
Layne
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
posted
0
Originally posted by Mike Smith: ... for example, this program won't compile for some reason.
Well, in order for us to help you, you need to post the compiler errors you get. Please include some indication of which line is causing the error so that we can explain why there is a problem.
Thanks,
Layne
Paul Santa Maria
Ranch Hand
Joined: Feb 24, 2004
Posts: 236
posted
0
Mike - Please do yourself a favor and look into Paul Sturrock's suggestion about regular expressions. I think you'll be impressed at what you learn!