aspose file tools*
The moose likes Beginning Java and the fly likes still puzzled about java and life! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "still puzzled about java and life!" Watch "still puzzled about java and life!" New topic
Author

still puzzled about java and life!

Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
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;

public class intisfoo {

public static void main(String[] args){

String s = Stdin.readln(); // read input from console.

int i = 0;

while(i < s.length()){

char ch = s.charAt(i);

if((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <='z') &&
(ch>= 'A' && ch<='Z')){

i = s.indexOf(' ');
String j = s.substring(0,i);

System.out.println("This is an integer.");
}else{
System.out.println("This is not an integer.");
}
i = i + 1;
}
}
}
Any suggestions? Thanks again
Wayan Saryada
Ranch Hand

Joined: Feb 05, 2004
Posts: 105

Hi,

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.

Cheers,
Wayan


Website: Learn Java by Examples
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
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: 39396
    
  28
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!
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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
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.

Layne


Java API Documentation
The Java Tutorial
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
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: 9047
    
  10


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
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
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
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!


Paul M. Santa Maria, SCJP
MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
check this link

http://www.scit.wlv.ac.uk/~jphb/java/basicio.html


God Gave Me Nothing I Wanted<br />He Gave Me Everything I Needed<br /> - Swami Vivekananda
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: still puzzled about java and life!