Is this a safe programming practice?

import java.util.*;

class Test{
    private int intToken;       
    public Test(){
        intToken = 0;          
    }
    // A method that returns the last character of a String
    // This here returns a string regardless of input
    public String lastChar(String tok){
        String token = String.valueOf(tok);
        String strToken = token.substring(token.length()-1);
        return strToken;
    }
    public static void main(String[]args){
        Test test = new Test();
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a number to get its last number");
        String value = scan.nextLine();
        System.out.println(test.lastChar(value));  
    }
}

      

The above method lastChar()

works for both Integer

String and String. Since I am in the programming field, I really wanted to create one method that returns either String

or Integer

, but within a method that I would like to determine if the input is Integer

or String

.

Right after creating the methods, I first experimented with

Scanner scan = new Scanner(tok);
scan.hasNextInt(); 

      

returned false

even if in tok

was int

.

I have also tried

tok.contains("0,1,2,3,4,5,6,7,8,9"); 

      

He also returned false

, even when in tok

was int

. I have yet to learn the type char

. but I noticed that I was tok.contains("1");

back true

.

I wanted to use the result boolean

to create separate handlers for String

and Integer

. Well the code still works.

Now my question is, is it safe or good practice to use one method, for example lastChar()

, or should I define two separate methods lastInt()

andlastStr()

+3


source to share


5 answers


I want to know if the String value is an integer (like "1234"), you can try converting the String: if there is an exception, the String value is not an integer:



try{
   Integer.parseInt(tok);
   return true;
} catch(NumberFormatException ex) {
   return false;
}

      

+4


source


Scanner scan = new Scanner("tok');

      

It must be single quotes or double quotes, not both

Also hasNextInt()

determines if the next scanner token is int, of course it will return false since your input has no int

tok.contains("1"); 

      

This checks if it contains the string "1" and not the number 1



Your method returns a string, not a String and Int. Even if you enter "1" it will be treated as a string

You should learn more about methods and what they return.

How can I check input integer or not JAVA?

You can always check the input and use NumberFormatException or MismatchInputException

0


source


Take a look at https://docs.oracle.com/javase/tutorial/java/data/index.html It talks about the differences between numbers and strings in Java. You will need two methods if you want to return two different types of data.

0


source


Small addition to the answers from andynaz and Huang Chen
to prevent catch-based programming, it was intended to use googles Guava
library in the library you have Ints.tryParse (String str)
returns null if str cannot be parsed
and parsed integer from str otherwise

hoped this answered your question.

0


source


If you are programming you can iterate over characters in a string and determine if a digit is a Character

class character

    String x = getString();
    boolean hasNonDigits = false;
    for(char c : x.toCharArray()){
        if(!Character.isDigit(c)){
            hasNonDigits = true;
            break;
        }
    }
    if(hasNonDigits){
        // It not an integer
    }

      

But if you're looking for a more conventional way, use a regex with Pattern

    Pattern p = Pattern.compile("\\D");
    String x = getString();
    boolean isInteger = !p.matcher(x).find();

      

0


source







All Articles