Results 1 to 3 of 3
  1. #1
    zythyl is offline Member
    Join Date
    May 2008
    Posts
    1
    Rep Power
    0

    Default How Do I Say *This* In Java?

    How do I say

    if the variable called "example" does not equal an integer
    DO
    System.out.println("wrong");
    ELSE
    Do nothing;

    I know that I shouldn't put an else in at all, but what about the IF statement?

    and how do i say someting in a loop but one part of the sentence changes each time? like saying "you have entered the number for box 1" then after entering another integer it says "you have entered the number for box 2"?

    thanks!
    Last edited by zythyl; 05-13-2008 at 05:14 AM.

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    it would be nice if you try to design a method that has a parameter "String" and will return a boolean that represents true if the value receive is an integer or false otherwise.

    What came first on mind is to put them on try/catch block.

    Java Code:
    boolean isInteger(String test){
         try{
              Integer.parseInt(test);
              return true;
         }catch(Exception e){
              return false;
         }
    }
    then invoke that method like

    Java Code:
    if(isInteger("7852862"))
         do something;
    else
         jump;
    Last edited by sukatoa; 05-13-2008 at 05:30 AM.
    freedom exists in the world of ideas

  3. #3
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    and how do i say someting in a loop but one part of the sentence changes each time? like saying "you have entered the number for box 1" then after entering another integer it says "you have entered the number for box 2"?
    Pseudocode,

    Java Code:
    while( allowed ){
          var = user's input
          if(isInteger(var))
                System.out.printf("You've entered %s \n",var);
          else
                set not allowed;
    }
    freedom exists in the world of ideas

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •