Java - Ignore possible error as in PHP with @


I would like to make a conditional that I can receive a word like the number one or that the number 1 is entered directly.

Having a conditional like the following:

if ($var == "Uno" || @intval($var1) == 1){
    echo "Picked 1";

In PHP, any possibility of error in the variable or function that has a @ before it is omitted, as in the example above.

Returning to Java , if the user types 1 is valid for the first condition, "1"!="One" , then the process continues, until you reach parseInt (var) == 1 .

The problem is when instead of 1 you type one , for the first condition the code is valid, "One" == "One" strong>, but for parseInt ("One") the code is not valid and that's where the error is.

What I have is the following, but it is clear that it is incorrect, since @ is not valid in Java.

String var;
if (var == "Uno" || @parseInt(var) == 1){
    System.out.println("Picked 1");

What would the code look like in Java? I am totally new in the language.

asked by Máxima Alekz 07.03.2017 в 05:50

2 answers


Your question is interesting. I have not tried it, but I think that if you try the parseInt on the value "One" it will give you a exception .

Then, it could be evaluated by calling a siEntero method which will return a true integer if there are only numbers in the string, or null if the string has letters, spaces or symbols :

Complete example evaluating several cases:


public class HelloWorld {

     public static void main(String []args){

        String var1 = "1";

        if (var1.equals("Uno") || siEntero (var1) == 1){
            System.out.println("CORRECTO: Picked 1 porque la cadena pudo convertirse a entero");

        String var2 = "1 ";

        if (var2.equals("Uno") || siEntero (var2) == 1){
            System.out.println("Caso 2: Picked 1");

       String var3 = "  1  ";

        if (var3.equals("Uno") || siEntero (var3) == 1){
            System.out.println("Caso 3: Picked 1");


    private static  Integer siEntero(String text) {
        try {
                return Integer.parseInt(text);

        } catch (NumberFormatException e) {
                System.out.println("ERROR: La cadena '"+text+"' no pudo convertirse a entero\n  Log:"+e);
                return 0;



CORRECTO: Picked 1 porque la cadena pudo convertirse a entero                                                                                        
ERROR: La cadena '1 ' no pudo convertirse a entero                                                                                                   
  Log:java.lang.NumberFormatException: For input string: "1 "                                                                                        
ERROR: La cadena '  1  ' no pudo convertirse a entero                                                                                                
  Log:java.lang.NumberFormatException: For input string: "  1  "
answered by 07.03.2017 / 06:28

To cast the whole String you must invoke the class Integer example:

public class Test { 

   public static void main(String args[]) {
      int x =Integer.parseInt("9");

solving your case:

String var;
if (var == "Uno" || Integer.parseInt(var) == 1){
    System.out.println("Picked 1");

note: you can also parsed other primitive types, such as: double, float, etc ...

answered by 07.03.2017 в 06:15