Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Comparing a string to int

  • 24-03-2004 3:54pm
    #1
    Registered Users, Registered Users 2 Posts: 488 ✭✭


    Can anyone tell me how to compare a string to an int...or is it possible...


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    How do you mean?

    "10" to 10?
    "Ten" to 10?

    What language are you using?


  • Registered Users, Registered Users 2 Posts: 640 ✭✭✭Kernel32


    Of course its possible, generally through a data conversion or type casting. The mechanics of how you do it will depend on the language in question, seeing as you did not provide any information about which language then I can't give any decent answer.


  • Registered Users, Registered Users 2 Posts: 488 ✭✭lad12


    It's in java

    i want to compare a string to an int

    if(string = 0)

    the compareTo method only woks for strings and .equals only works for int's i think...is there anyway around this..


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    You'll need to wrap the int in an Integer class.

    if(yourString == Integer.valueOf(int))

    .compareTo and .equals both only work for Objects. Numeric operators (==, !=, etc) only work for primitive types.


  • Registered Users, Registered Users 2 Posts: 488 ✭✭lad12


    So will this work...

    if(Input == Integer.valueOf(0))


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,934 ✭✭✭egan007


    Originally posted by lad12
    It's in java

    i want to compare a string to an int

    if(string = 0)

    the compareTo method only woks for strings and .equals only works for int's i think...is there anyway around this..

    What are you trying to do - if i may ask

    why has your string a number?

    or
    are you trying to see if the srting is empty or equal to NUll i.e =" "


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    What am I saying...

    It should be:

    if(Input.equals(Integer.valueOf(0))

    He he he... Brains a bit frazzled after looking at C# all day...


  • Registered Users, Registered Users 2 Posts: 488 ✭✭lad12


    I want to check if the input from the user is an number...if it is a number then i want the program to finish

    and the input is supposed to be a string...but if the user inputs a certain number then it should end


  • Registered Users, Registered Users 2 Posts: 4,666 ✭✭✭Imposter


    if(Integer.parseInt(myString) == myInt)

    is what you need.


  • Registered Users, Registered Users 2 Posts: 488 ✭✭lad12


    What am I saying...

    It should be:

    if(Input.equals(Integer.valueOf(0))

    He he he... Brains a bit frazzled after looking at C# all day...
    ___________________________________________________________

    Yeah something like that


  • Advertisement
  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    If you're doing that you should probably convert the string the user entered into a number first
    int num = Integer.parseInt(userInput);
    if (10 == num)
    {
       // return 
    }
    


  • Registered Users, Registered Users 2 Posts: 4,666 ✭✭✭Imposter


    Of course you will also have to catch a NumberFormatException should something other than a number be entered.


  • Registered Users, Registered Users 2 Posts: 488 ✭✭lad12


    and...


  • Registered Users, Registered Users 2 Posts: 2,934 ✭✭✭egan007


    Originally posted by lad12
    I want to check if the input from the user is an number...if it is a number then i want the program to finish

    and the input is supposed to be a string...but if the user inputs a certain number then it should end

    Here is a nice clean way

    getText() - get ur string
    charAt(0) - cut off the first character
    isDigit() - see if it's a number
    isLetter() - or a letter



    :)


  • Registered Users, Registered Users 2 Posts: 19,396 ✭✭✭✭Karoma


    and what? they've answered it for you..



    --Edit:
    Regular Expression:
    if user input is of: regex:[ 0 - 9 ]+ then system.exit..
    else ..keep going or whatever


  • Registered Users, Registered Users 2 Posts: 488 ✭✭lad12


    No they aint cos im getting an error in my program...


  • Registered Users, Registered Users 2 Posts: 19,396 ✭✭✭✭Karoma


    what error? post it up. They have answered your question.


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Care to enlighten us?

    Post both your code and the error... otherwise its a little hard to help.


  • Registered Users, Registered Users 2 Posts: 2,934 ✭✭✭egan007


    ok - well i'm in a good mood so ....


    String s = myTextFiled.getText();
    boolean isNumeric = true;

    try {
    Integer.parseInt(s);
    }
    catch (NumberFormatException nfe) {
    isNumeric = false;
    }

    if (isNumeric) {

    //do numeric work for s
    }
    else {
    //do alphanumeric work
    }


    i better get my name hashed in ;)


  • Registered Users, Registered Users 2 Posts: 488 ✭✭lad12


    But is there just a method i can use

    like ive ive tried

    int num = Integer.parseInt(userInput);

    and

    if(Input.equals(Integer.valueOf(0))

    oh and how can i get a program to terminate using an if statement...i know using a while statement i can use break...


  • Advertisement
  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Originally posted by lad12
    But is there just a method i can use

    Unless you write it yourself... No, no there is not...

    Just post your code and the error you get or the returned stack trace and we'll take a look and tell you whats up...


  • Registered Users, Registered Users 2 Posts: 2,934 ✭✭✭egan007


    Originally posted by lad12
    But is there just a method i can use

    like ive ive tried

    int num = Integer.parseInt(userInput);

    and

    if(Input.equals(Integer.valueOf(0))

    oh and how can i get a program to terminate using an if statement...i know using a while statement i can use break...


    check previous page


  • Registered Users, Registered Users 2 Posts: 488 ✭✭lad12


    It's grand..thanks for yer Help...


  • Registered Users, Registered Users 2 Posts: 19,396 ✭✭✭✭Karoma


    egan007 + exitingfeature:
    
    String s = myTextFiled.getText(); // get the user input
    boolean isNumeric = true; // true if input is a number
    
    try 
    {
      int newInt = Integer.parseInt(s); // convert String to int
    }
    catch (NumberFormatException nfe) 
    { //catch errors when converting..
      isNumeric = false; // and tell everything else that it is _not_ a number
    }
    
    

    Exit when input "is a number" :
    if (isNumeric) 
    {
      System.exit(0);  // exit!
    }
    else 
    {
    //do alphanumeric work
    }
    

    exit if input is a specific number :
    if (newInt == 1) 
    {  //replace 1 with whatever number to test. to test against more than one num:
       // if (newInt == 1 || newInt == ....)
      System.exit(0);  // exit!
    }
    else 
    {
    //do alphanumeric work
    }
    


  • Registered Users, Registered Users 2 Posts: 2,934 ✭✭✭egan007


    the code is there - what do you want - us to run it?

    i could not of written it any clearer


  • Registered Users, Registered Users 2 Posts: 19,396 ✭✭✭✭Karoma


    //declare vars
    int newInt;
    
    //------------ pass in a string and get an int back
    public int smellyStringToInt(String in_userInput)
    {
      try
      {
        newInt = Integer.parseInt(String in_userInput);
      }
      catch(NumberFormatException nfe)
      { //error message }
    
      return newInt;
    }
    
    //-----------------------------------
    
    now: to convert userinput to an int:
    smellyStringtoInt( textfield.getText() );
    
    
    public boolean compareTwoInts(int in_WhatTocheckAgainst, int in_convertedInt)
    {
      boolean isMatch;
    
      if ( in_WhatTocheckAgainst ==  in_convertedInt)
      {//they match
        isMatch = true;
      }
      else
      {//not a match
        isMatch = false;
      }
    
      return isMatch;
    }
    
    compareTwoInts(intToCheckFor , newInt );
    


    i get the feeling the assignment asked you to create methods to do it :)
    post up the assignment... (not saying that i'll do it)


  • Registered Users, Registered Users 2 Posts: 488 ✭✭lad12


    Yeah i got it working...thanks for all yer help


Advertisement