1

The program is supposed to take a string input by the user and return it reversed. (ex "Hello" is input "olleH" is the output.) You do this by running the program like java ReverseCL2 Hello However, when no string is input with it, it prints an ArrayOutOfIndexException. I need it to instead print out what the user is supposed to do (Please input a string when running the code.) I have tried what is below and also if (args[0].equals(null)). Thanks in advance for any help :)

public class ReverseCL2
{
    public static void main(String[] args)
    {
        String s = args[0];
        String rev = "";
        if (args[0].isEmpty())
        {
            System.out.println("Input a string to be reversed");
        }
        else
        {
            for (int i=0; i<s.length(); i++) 
            {
                rev = s.charAt(i) + rev;
            }
            System.out.println(rev);
        }

    } 
}

5 Answers 5

1

Check for args.length being zero first, i.e.

if(args.length == 0 || args[0].isEmpty())
{
    // <Handle special case however you like>
}

The ArrayIndexOutOfBoundsException you get is a result of trying to access args[0] when args has size 0.

Sign up to request clarification or add additional context in comments.

Comments

1
public class ReverseCL2
{
    public static void main(String[] args)
    {
        if(args.length == 0 || args[0].isEmpty())
        {
            System.out.println("Input a string to be reversed");
            return;
        }

        String s = args[0];
        String rev = "";

        for(int i = 0; i < s.length(); i++) 
        {
            rev = s.charAt(i) + rev;
        }

        System.out.println(rev);

    } 
}

3 Comments

+1: Fair point - you (i.e. the OP) still need to check for it then being empty as well.
if(args.length == 0 || args[0].isEmpty()) checks for empty array and empty value... this code is absolutely correct. (and this is really a basic problem)
Yes, I know - I was observing that I answered the question too quickly and just showed how to avoid the exception whilst stupidly forgetting the second part of the condition. Hence the upvote I gave you, and the fact that I then corrected myself :)
0

put ! for checking the parameterized array value

if (!args[0].isEmpty() && args.length==0)

See above line in your code.

    public class ReverseCL2
{
    public static void main(String[] args)
    {
        String s = args[0];
        String rev = "";
        if (!args[0].isEmpty() && args.length==0)
        {
            System.out.println("Input a string to be reversed");
        }
        else
        {
            for (int i=0; i<s.length(); i++) 
            {
                rev = s.charAt(i) + rev;
            }
            System.out.println(rev);
        }

    } 
}

Comments

0
It simple arg0 is array so before get data from That check Array size 


if(args.length == 0)
{
    // <Show message to user >
}else{

// <write here Working code >

}

Comments

0

In addition to above answers a more simpler code(reading vise) would be

public static void main(String args[]) {

    switch (args.length) {

        case 0 : System.out.println("Input a string to be reversed");
            break;
        default:    //your logic

    }
}

2 Comments

This unfortunately doesn't do an empty check on args[0] when args.length != 0.
That logic would come in the default case(Non zero arguments). You can always check for empty or null before processing the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.