0

i would like to enter a string like ciphercloud and the ranges are 3 to 6 then it will shows like pher***.ranges are must be integers only if you give ranges as character it will never accept and also ranges must be positive integers only not be negative. by satisfying all this i coded like this way

class maskchar
{
    public static void main(String args[])
    {
        String str = args[0];
        int start = Integer.parseInt(args[1]);
        int end = Integer.parseInt(args[2]);
        char[] str1 = str.toCharArray();
        int len = str1.length;
        if((start >= 0 && start <= len) && (end >= 0 && end <= len))
        {
            for(int i = 0; i < start - 1; i++)
            {
                System.out.print("*");
            }
            for(int j = start - 1; j < end; j++)
            {
                System.out.print(str1[j]);
            }
            for(int k = end; k < len; k++)
            {
                System.out.print("*");
            }
        }
    }
}

whenever we run this code it shows expected out put but if i pass character as starting range it will through an exception i.e NumberFormatException how can i handle that Exception and how can i make this as more flexible.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 1
    It obviously throws a `NumberFormatException` because you call the `parseInt` function on a character (which is not an int obviously). If you want to use characters then you need to either catch the exception and assume it has received a character or determine whether the argument is an integer before calling the `parseInt` function. – Ceiling Gecko Apr 10 '14 at 09:11
  • @ifLoop Coding style is subjective, there is no right or wrong. Do not edit people's post to change coding style to your personal preference. Your edit should not have been approved, so I'll do a rollback. – Lundin Apr 10 '14 at 11:07
  • @Alex K. Please read the above comment if you insist on doing edit reviews in the future. – Lundin Apr 10 '14 at 11:08
  • @Dhara Please read the above comment if you insist on doing edit reviews in the future. – Lundin Apr 10 '14 at 11:08
  • @Ashwini Agarwal Please read the above comment if you insist on doing edit reviews in the future. – Lundin Apr 10 '14 at 11:09

2 Answers2

0

It throws the NumberFormatException because you are trying to parse a character into an integer type. To avoid this, you could check the type of the input using the getClass().getName() methods to check the type using an if condition as shown in this question and parsing it into an integer only if it is an integer.

You could also just catch the NumberFormatException using a simple try-catch and print a good error message.

Community
  • 1
  • 1
anirudh
  • 4,116
  • 2
  • 20
  • 35
0

Q: How can I handle that Exception
A: By using a try-catch-block


Q: How can I make this as more flexible.
A: I suggest to add some plausibility checks at the start e.g.:

  • Check if three arguments are passed
  • Check start position and end position

    String str   = "";
    int    start = 0;
    int    end   = 0;
    
    try {
        if (args.length < 3) {
            throw new IllegalArgumentException("Please enter the right number of arguements");
        }
        str   = args[0];
        start = Integer.parseInt(args[1]);
        end   = Integer.parseInt(args[2]);
    
        if (start < 0     || start > str.length() ||
            end   < start || end   > str.length()) {
            throw new IllegalArgumentException("Please enter valid values for <start> and <end>");
        }                
    } catch (NumberFormatException e) {
        System.out.printf("Please enter a vaild");
    }
    
    char[] str1  = str.toCharArray();
    int    len   = str1.length;
    
ifloop
  • 8,079
  • 2
  • 26
  • 35