-1

this class is a test to see if my 2 methods (the "endfinder"s) are working (unfortunately they are not). endfinder() is meant to return the index of the next non space character in the provided string; endfinder2() is meant to return the endpoint of the word starting with the index found by endfinder. They're meant to be used to pull out words from a string with multiple words separated by spaces. I've gone over it for a few hours and there must be some obvious answer but I feel like I'm slowly going insane.

I've removed all of the outputs I used to test the code in many attempts to figure out what was wrong to make it cleaner.

Basically, what I've found is that endfinder() always returns 0 for some reason and endfinder2() runs until it reaches the break (I assume that's what happens as it used to loop infinitely before I added it in).

The result I want is start = 3, end = 6 which would make it output "[one]".

The result I get:

[ one two three] start = 0, end = 17.

Here's my code:

import java.io.*;
import java.util.*;

public class test {

    static String answer, container;
    static int start = 0, end = 1;  

    public static int endfinder(String perspective) {
        int holder = start;
        while (perspective.substring(holder, (holder + 1)) == " ") {
            holder++;
            if (holder == perspective.length())
                break;
        }
        return holder;
    }

    public static int endfinder2(String perspective) {
        int holder = end;
        while (perspective.substring(holder, (holder + 1)) != " ") {    
            holder++;
            if (holder == perspective.length())
                break;
        }
        return holder;
    }

    public static void main(String Args[]) {
        System.out.println();
        System.out.println();
        container = "   one  two three";
        start = endfinder(container);
        end = start + 1;
        end = endfinder2(container);
        System.out.println("[" + container.substring(start, end) + "]");
        System.out.println();
        System.out.println("start: [" + start + "] end: [" + end + "]");
        System.out.println();
    }

}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
syminical
  • 1
  • 3
  • By the way mate, two tips. Firstly you should name your class `Test` (not 'test') and you should try to avoid working in a `static` context. Why not have the first line of your `main` method: `Test test = new Test();`. – vikingsteve Oct 29 '14 at 13:51
  • @syminical look at my answer it is what you want. – brso05 Oct 29 '14 at 13:57

1 Answers1

0

You want something like this:

    static String answer, container;
    static int start = 0, end = 1;  

    public static int endfinder(String perspective) {
        int holder = start;
        while (perspective.charAt(holder) == ' ') {
            holder++;
        }
        return holder;
    }

    public static int endfinder2(String perspective) {
        int holder = end;
        while (perspective.charAt(holder) != ' ') {    
            holder++;
        }
        return holder;
    }

    public static void main(String Args[]) {
        System.out.println();
        System.out.println();
        container = "   one  two three";
        start = endfinder(container);
        end = start + 1;
        end = endfinder2(container);
        System.out.println("[" + container.substring(start, end) + "]");
        System.out.println();
        System.out.println("start: [" + start + "] end: [" + end + "]");
        System.out.println();
}

You should use charAt to compare a character ' ' either != ' ' or == ' '. You should modify your loops to use this comparison shown above. You are also doing extra work more than is needed. I stripped out some of your code removed the breaks from the loop (they will end automatically when condition is not met). Take a look at the code if you have questions just ask away.

brso05
  • 13,142
  • 2
  • 21
  • 40