gdhc21
gdhc21

Reputation: 107

Java - When there is no user input prevent an error message - Homework

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);
        }

    } 
}

Upvotes: 1

Views: 1598

Answers (5)

Stuart Golodetz
Stuart Golodetz

Reputation: 20646

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.

Upvotes: 1

Aniket Thakur
Aniket Thakur

Reputation: 68975

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

    }
}

Upvotes: 0

sravan
sravan

Reputation: 5333

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 >

}

Upvotes: 0

Michele Mariotti
Michele Mariotti

Reputation: 7459

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);

    } 
}

Upvotes: 1

KhAn SaAb
KhAn SaAb

Reputation: 5376

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);
        }

    } 
}

Upvotes: 0

Related Questions