• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

still puzzled about java and life!

 
Mike Smith
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
IntelliJ IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike Smith
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike Smith
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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++"
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
MInu
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check this link

http://www.scit.wlv.ac.uk/~jphb/java/basicio.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic