File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes integer verifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "integer verifier" Watch "integer verifier" New topic
Author

integer verifier

Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Hello all I have a problem here that I still can't get to work. It is an
elementary one for you experts though. Here it is I need to write a program
that either prints out this is a valid integer or this is not a valid integer.
And somehow the charAt() string method is useful here. I decided to write a
if/else statement. One path taken will be this is a valid integer the other
path is this is not a valid integer. A integer is 12345 or -0 whereas a non
int is suppose to be hello or 1 2. I have heard of the try / catch thing to
catch a exception however, I wish to complete this task with charAt()
method. I know what I wish to do to solve it but am confused on doing it in
java. My algorithm to solve it is the following.
-check first character to determine if it is a numeric character or a letter
character. If it is a letter character, printout This is not an integer. If
it is a numeric character determine if there is a space at index 1. if there
is display this is not an integer, otherwise it is a integer.. If anyone
could help me that would be great.Thanks in advance. Oh yes one more thing.
If i also have to make a tic tac toe game; would it be possible to do it
with only 1 array. THanks again
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
The thing that baffles me most is how do I compare a numeric character to a letter character. For example, how do I compare a 1 to a h. Do I need to use Integer.parseInt or something like it. I know I can compare strings using the equals() method, but what about individual characters? Thanks again.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

Well, show us what you have done so far. Also, more specific questions may also help. There are many here that will give you good hints to get you going again.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

Originally posted by Mike Smith:
The thing that baffles me most is how do I compare a numeric character to a letter character. For example, how do I compare a 1 to a h. Do I need to use Integer.parseInt or something like it. I know I can compare strings using the equals() method, but what about individual characters? Thanks again.


You can convert a character to it's unicode value simply by casting it... but most likely, you wouldn't want to do that. Instead, you want to just compare a character with the characters that represents the numberic digits.

Comparing characters are done the same way as other primatives, simply uses the "==" operator.

Henry
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
Or you could use the Character.isDigit() method.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
public class intis {

public static void main(String[] args){

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

char ch = s.charAt(0); //find character at position 1.

int bla = s.indexOf(' '); //find blank space.




if(ch==0 && ch==9){
System.out.println("This is an integer.");
}else{
System.out.println("This is not an integer.");
}
This is saying everything is not an integer.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

Mike,

A couple of suggestions. First, you need to check all the characters in the string, not just the first couple. Have you learned loops yet? Take a look at the String.length() method. That should help with determining the range to loop.

Second, the comparison is incorrect. Take a look at the Character class as Marilyn suggested.

Henry
[ October 15, 2005: Message edited by: Henry Wong ]
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Hi Henry I got it, but I didn't use a loop. The way it is works for its purpose but I think a loop would benefit the program. I'll play around with it. Thanks though for the input.

public class intis {

public static void main(String[] args){

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

char c = s.charAt(1);

//determine if character at position one is in the range
//of 0-9.
if(c >='0' && c<='9'){
System.out.println("This is a integer.");
}else{
System.out.println("This is not an integer.");
}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

Mike,

Think about this... Is "12345hello" an integer? If you agree that it is not, can you tell by just checking the first few characters?

Henry
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Well I guess, I will have to implement a loop into the program some how. Thanks for the input. I looked at the Character.getDigit() method on the api library. However, I don't know how to use the api or that method in my program. Is there a tutorial on using the api anywhere. THanks again
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I think there is a very important concept that you are missing here: how data is represented in a Java program. In particular, you need to understand the difference between a digit character and a numeric value. In Java, a character has type char while a "number" has type int, long, float, or double, depending on what kind of number it is.

Let's look at the following line of code:

When you have a number in your code like 0 and 9 in the above, it is called a literal. All literals have a type, just like variables do. In this case, 0 and 9 both have the int type becaus they are integers.

Note that you can have character literals in your code. These are represented by surround a character with single-quotes. For example, 'a' is the character for the lower-case letter a. Digit characters are no different. This means that you should use '0' and '9' to represent character digits.

However, even if you make this change, the above code will not behave the way you expect. Let's read what it says: "if ch equals zero AND ch equals 9". How can a variable have two different values (i.e. 0 AND 9) at the same time? Answer: it can't! I think you mean to say: "if ch is zero OR nine." This still won't quite work, though, because there are a lot more digits than just 0 and 9. So do you have any ideas how to modify this to include ALL digits?

Let me know what you come up with and I'll be glad to help you out.

Layne


Java API Documentation
The Java Tutorial
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Hello,

I got this so far which seems to work, but as henry pointed out I need to add a loop into the program. A loop is needed to help the 1234hello problem of the program saying it is an integer. To get all numbers I did the following c >='0' && c<='9')That read the character is greater or equal to zero and character is less than or equal to 9. But now my problem is how do I get my loop to check all characters in the string. Thanks again for the input.

public class intis {

public static void main(String[] args){

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

char c = s.charAt(1);

//determine if character at position one is in the range
//of 0-9.
if(c >='0' && c<='9'){
System.out.println("This is a integer.");
}else{
System.out.println("This is not an integer.");
}
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Good, you have fixed the if statement to use the correct logic. So lets talk about the looping. First, what kind of loop do you think you should use? Do you know what types of loops are available in Java? (Hint: there are three.) If you know the ones that are available, then which is most appropriate for this particular task?

Once you know the answers to these questions, then you need to decide what portion of the code that you have so far should be inside the loop. To figure this out, ask yourself what portion needs to be repeated multiple times.

Let us know what you figure out and we will keep helping you.

Layne
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Thanks Layne, I know there are three types of loops. However, I've only been introduced to the while loop and for loop. I know the while loop is supposed to be used when you wish for a certain critera to be met. I would go with the for loop, since I know how many times to repeat(the length of the string). However, I understand that the for loop is used if you know how many time you wish to count. I got this so far. But it aint working properly for some reason.

public class intis2 {

public static void main(String[] args){

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

for(int i=0; i< s.length(); i=i+1){

char c = s.charAt(i);

if(c >='0' && c<='9'){

System.out.println("This is a integer.");

}
System.out.println("This is not an integer.");



}


}
}
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Ok now I did it using the while loop but still no luck. I will keep trying to debug it. I am determined to get it.

ublic class intis2 {

public static void main(String[] args){

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

int len = s.length();

int i = 0;

while(i<len){

char c = s.charAt(0);

if(c >='0' && c<='9'){

System.out.println("This is a integer.");

}else{
System.out.println("This is not an integer.");
}
i = i+1;

}


}
}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

Don't be discouraged, it is much better than before...

Here is a hint... If a digit is determined not to be an integer, is the whole number not an integer? If a digit is determined to be an integer, is the whole number an integer?

Henry
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Ok, I don't think I understand the question fully. But I know if the digit is an integer, the space between the string is still not an integer.That is if you use int num = Integer.parseInt(Stdin.readln); you are converting the string to a integer which is stored in variable num, but the spaces between the integer(from the command line, aren't integers).
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
I think that Henry is trying to show you that once you find a non-digit, you're done. You may have something like 1A2 which is not a number. In fact, if you try to use Integer.parseInt on it, you'll get an exception. So in your loop you see '1'. It's a digit. Good. Keep going. Then you see 'A'. Not a digit. If you keep going, you'll get '2' which is a digit. Then you'll try to parse 1A2 which will fail. So you want to stop when you see the 'A' and go to "This is not an integer." because after that point it will never be an integer.
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Still no luck but I have been getting closer I think. I relized something odd. This is what it is.

12345hello
1 This is a integer.
2 This is a integer.
3 This is a integer.
4 This is a integer.
5 This is a integer.
h e l l o Press any key to continue . . .

and

hello12345
h e l l o 1 This is a integer.
2 This is a integer.
3 This is a integer.
4 This is a integer.
5 This is a integer.
Press any key to continue . . .

my code so far is;

public class intis2 {

public static void main(String[] args){

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





for(int i=0; i<s.length(); i = i+1){

char c = s.charAt(i);

System.out.print(c);
if(c >='0' && c<='9'){

System.out.println("This is a integer.");

}
}
//System.out.println("This is not an integer.");






}
}
I don't know how to tell the computer what i wish to do. I wish to compare all the characters in a string.And once a char is found display " This is not an integer. Anyways any help much appreciated. Thanks again
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Ok now I've made an important adjustment. but now I have duplicated read outs on the command line. why is it doing duplicates. I figure it has to do with the way I set up my loop. Any suggestions to fix this problem once and for all. Thanks again

my code

public class intis2 {

public static void main(String[] args){

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





for(int i=0; i<s.length(); i = i+1){

char c = s.charAt(i);

//System.out.print(c);
if(c >='0' && c<='9'){

System.out.println("This is a integer.");

}else if(c>='a' || c>='A' && c<='z' || c<='Z'){


System.out.println("This is not an integer.");
}
}

}
}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

Your previous iteration was actually better... There is actually no real way to hint this so... here it is...

When you find a non-integer, you are done. Printing that you found a non-integer is not enough. You have to break out of the loop and stop checking the rest of the characters.

When you find a integer, you are not done. Don't print that you found an integer, because you didn't, you only found a digit that is an integer. You still have to check the rest of the string.

So when do you find an integer? When you are done with the loop -- meaning when you exit out of the loop without finding a non-integer.

Anyway, have you learned the "break" statement yet? That might help you exit the loop early.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: integer verifier