Question

I have to loop through a string in the command line such as (java Lab2 "HELLO WORLD") and print out the substring [HELLO] and [WORLD] by using a loop. Here is what I have so far.

public static void main(String argv[])
{
    if (argv.length() == 0)
    {
        System.out.println("Type in string");
    }

    String Input = argv[0];
    String sub;

    for (int end = 0; end < Input.length(); end++)
    {
        end = Input.indexOf(' ');
        sub = Input.substring(0, end);
        System.out.println("sub = [" + sub + "]");


        if(end > 0)
        {   
            int start = end +1;
            end = Input.indexOf(' ');  
            sub = Input.substring(start,end);
            System.out.println("sub = [" + sub + "]");
        }
    }
}
}

The first word in Input will print out fine. Then after that I will either get an infinite loop or I will be thrown an index array out of bounds exception. The exception is referring to the if statement in the for loop.

Was it helpful?

Solution

It's a way to do it:

if (argv.length == 0) // Length it not a method
{
    System.out.println("Type in string");
    return; // the code should be stopped when it happens
}

String input = argv[0];// Avoid use this kind of name

int idx;
int lastIndex = 0;

// indexOf returns the index of the space
while ((idx = input.indexOf(' ', lastIndex)) != -1)
{
    System.out.println("[" + input.substring(lastIndex, idx) + "]");
    lastIndex = idx + 1;
}

System.out.println("[" + input.substring(lastIndex, input.length()) + "]");

I use indexOf to know the index of every space in the string.. the last line is needed because it can't find the last word. One way to fix it is:

if (argv.length == 0) // Length it not a method

{
    System.out.println("Type in string");
    return; // the code should be stopped when it happens
}

String input = argv[0] + " ";// Avoid use this kind of name

int idx;
int lastIndex = 0;

// indexOf returns the index of the space
while ((idx = input.indexOf(' ', lastIndex)) != -1)
{
    System.out.println("[" + input.substring(lastIndex, idx) + "]");
    lastIndex = idx + 1;
}

I think you notice the + " "; in input line

OTHER TIPS

It seems you are overcomplicating this by trying to split up the string yourself. Most programming languages like Java have a split() function, which will split a string where it sees a certain substring (which in your case is " ") into an array. You can then use a foreach statement to iterate through this array. In Java, foreach is done like:

for (String current : String[] array) { }

And for the split, you need to do:

String[] elements = Input.split(" ");

So altogether, you can do something like:

String[] elements = Input.split(" ");

for (String sub : elements) {
    System.out.println("sub = [" + sub + "]");
}

In the lines,

int start = end + 1;

if(end > 0) {

If start is 6, then end will be 5 here.. then Input.sub string(6,5) is definitely wrong because the beginning index should always be less than end index which is vice versa here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top