The Artima Developer Community
Sponsored Link

Java Answers Forum
if statement

8 replies on 1 page. Most recent reply: Apr 17, 2006 7:32 AM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
Jaelyn

Posts: 6
Nickname: tarheel
Registered: Sep, 2005

if statement Posted: Nov 29, 2005 1:52 PM
Reply to this message Reply
Advertisement
I am writing an if statement for a program. If an input is not a number then an error message needs to be displayed. I was wondering if there is something in java that means not equal to letters of the alphabet or symbols?


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: if statement Posted: Nov 29, 2005 11:46 PM
Reply to this message Reply
Use this method: If the String that you pass in
returns false; display your Message. Character also has
methods isLetter which you can use inplace of isDigit
to verify if the String you are passing in below is
consistent of all letters and also has isLetterOrDigit
to check for Alpha-Numeric characters.

public boolean isDigit(String str){
   for(int i = 0; i < str.length(); i++){
      if(!Charecter.isDigit(str.charAt(i))){
         return false;
      }
   }
   return true;
}

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: if statement Posted: Nov 29, 2005 11:49 PM
Reply to this message Reply
Sorry typo, the Class you use is Character and not Charecter.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: if statement Posted: Nov 29, 2005 11:59 PM
Reply to this message Reply
The 'Java' way to do this is in my opinion using the NumberFormatException.

String input = ...; //get the input
Integer result = null; //using Integer instead of int it is easier to check the value later
try {
    result = new Integer(Integer.parseInt(input));
} catch (NumberFormatException e) {
    System.out.println ("This is a error message");
}
if (result != null) {
  //continue your program here
}

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: if statement Posted: Nov 30, 2005 6:01 AM
Reply to this message Reply
Quote:

"I was wondering if there is something in java that means not equal to letters of the alphabet or symbols?"

Explanation included in checking for Alpha-Numeric Types in my
Solution. I'm not sure what is so 'Java' about your solution. If ran on a GUI taking in from a JTextField the String 0x3
would it not crash? 0x3 ideally is a number. My solution would crash too.

Case and point?
there are millions of ways to the solution, both of these in
my humble opinion just about scratch the surface. I don't
think one is more Java than any other.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: if statement Posted: Nov 30, 2005 6:45 AM
Reply to this message Reply
@Kondwani Mkandawire
I wrote my solution a long time before clicking on the "send" button, so I didn't even see your answer, my solution has absolutely nothing to do with your post.

The 'Java' solution means: A input usually is something more then a character. Unexperienced programmers tend to check every character. So I suggested to use the built in methods to do some basic operations like this.
Don't know how exactly it is implemented, but I think (it is ony my opinion, I did not read the code) the parseInt method does more or less the same as you wrote in your example.

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: if statement Posted: Nov 30, 2005 10:07 PM
Reply to this message Reply
> @Kondwani Mkandawire
> I wrote my solution a long time before clicking on the
> "send" button, so I didn't even see your answer, my
> solution has absolutely nothing to do with your post.
>
> The 'Java' solution means: A input usually is something
> more then a character. Unexperienced programmers tend to
> check every character. So I suggested to use the built in
> methods to do some basic operations like this.
> Don't know how exactly it is implemented, but I think (it
> is ony my opinion, I did not read the code) the parseInt
> method does more or less the same as you wrote in your
> example.

Sorry, My baad.

James Bazz

Posts: 1
Nickname: jbazz
Registered: Apr, 2006

Re: if statement Posted: Apr 4, 2006 7:33 PM
Reply to this message Reply
Kondwani,

Did you study in Lilongwe, Malawi? If so I may know you.

- Swamy

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: if statement Posted: Apr 17, 2006 7:32 AM
Reply to this message Reply
2 minor remarks :

The code should have been written in this way :

String input = ...; //get the input
try {
Integer.parseInt(input);
return true;
} catch (NumberFormatException e) {
return false
}

No unnecessary assignments !
No System.out... :)

The second remak is that the apache foundation has proposed a extensive set of utilities to do all those...
Don't re-invent the whell !

\T,

Flat View: This topic has 8 replies on 1 page
Topic: modify static variable Previous Topic   Next Topic Topic: cliet server with time cotrol

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use